//code by cefuve.com
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#define clk 2
#define dt 3
#define btn 4
#define led 10
String opciones[] = {"Gain:", "V_off:", "V_aref:", "out_man:","setPoint:","man:"};
int max_opciones = sizeof(opciones)/sizeof(opciones[0]);
int state_clk_old;
int state_btn_old;
int count = 0;
bool hz_1 = false;
bool mono = false;
unsigned long tiempo_actual = 0;
unsigned long tiempo_actual2 = 0;
int Gain = 0;
int Gain_ant = 0;
int V_off = 0;
int V_aref = 0;
int out_man = 0;
int setPoint = 0;
bool man = 0;
bool config=1;
int paso = 0;
void setup() {
Serial.begin(9600);
// Init LCD
lcd.init();
lcd.backlight();
pinMode(led, OUTPUT);
pinMode(clk, INPUT);
pinMode(dt, INPUT);
pinMode(btn, INPUT_PULLUP);
state_clk_old = digitalRead(clk);
state_btn_old = digitalRead(btn);
mostrar_menu();
}
void loop() {
int state_btn = digitalRead(btn);
encoder();
if(state_btn_old == HIGH && state_btn == LOW){
config = 1;
}
run_option();
// pruebas Serial.println()
Serial.println((String) state_btn);
// va siempre al final
state_btn_old = state_btn;
}
void run_option(){
if(count == 0){
// paso = Gain;
gain(); // opcion de Gain
}
if(count == 1){
// opcion de V_off
}
if(count == 2){
// opcion de V_aref
}
if(count == 3){
// opcion de out_man
}
if(count == 4){
// opcion de setPoint
}
if(count == 5){
// opcion de man
}
config = 0;
}
void encoder(){
int state_clk = digitalRead(clk);
int state_dt = digitalRead(dt);
if (config){
if(state_clk_old == HIGH && state_clk == LOW){
if(state_dt == LOW){
count--;
}
else{
count++;
}
if(count < 0) count = max_opciones - 1;
if(count > max_opciones-1) count = 0;
mostrar_menu();
}
}
delay(5);
state_clk_old = state_clk;
}
void mostrar_menu(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Menu:");
lcd.setCursor(0,1);
lcd.print(opciones[count]);
}
void modif_var(var){
if(state_clk_old == HIGH && state_clk == LOW){
if(state_dt == LOW){
var--;
}else{
var++;
}
}
void gain(){
modif_var(Gain);
lcd.setCursor(6,1);
lcd.println(Gain);
}