hola, tengo que hacer un menu de opciones con una LCD 16x02 que me permita seleccionar entre 3 opciones que al presionar el boton de enter me lleve al submenu de dicho programa y ejecute la accion, 1. controlar intensidad led con potenciometro 2. controlar intensidad led con botones y 3. medir voltaje con potenciometro. Si imprime el menu y con los botones puedo seleccionar la opcion deseada y con una condicion que mientras el boton enter no este presionado se queda ejecutando el programa en cuestion, el problema es que ahora al presionar el boton atras no regresa al menu principal. Aqui les dejo mi codigo hasta el momento:
#include <LiquidCrystal.h>
#define LED 3
#define potenciometro A0
#define botonArriba 4
int botonArribaState;
unsigned long botonArribaDown;
#define botonAbajo 5
int botonAbajoState;
unsigned long botonAbajoDown;
#define botonEnter 6
int botonEnterState;
unsigned long botonEnterDown;
#define botonAtras 7
int botonAtrasState;
unsigned long botonAtrasDown;
int ledBrillo;
int led2Brillo;
int estado;
int cambio;
float voltaje;
int botonSuma;
int botonResta;
int mod=0;
int menu=1;
boolean menuChange=false;
boolean subMenuChange=false;
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode(botonArriba, INPUT_PULLUP);
pinMode(botonAbajo, INPUT_PULLUP);
pinMode(botonAtras, INPUT_PULLUP);
pinMode(botonEnter, INPUT_PULLUP);
pinMode(potenciometro, INPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
ledBrillo = 0;
led2Brillo = 0;
lcd.begin(16, 2);
Menu();
}
void loop() {
botonAbajoF();
botonArribaF();
botonEnterF();
botonAtrasF();
if(menuChange)
{
menuChange=false;
Menu();
}
if(subMenuChange)
{
subMenuChange=false;
subMenu();
}
}
void botonAbajoF()
{
////////////boton abajo/////////////////////
botonAbajoState = digitalRead(botonAbajo);
if(botonAbajoState == LOW){
Serial.println("Boton Abajo presionado");
//botonAbajoDown = millis(); //record the time the button went down
while(digitalRead(botonAbajo)==LOW) {
Serial.println("boton aun presionado, esperando...");
//wait until the button is released
}
Serial.println("Boton abajo soltado");
menu++;// increment the page to display
if (menu>3) menu=1;
menuChange=true;
botonAbajoDown=millis();
}
}
void botonArribaF()
{
/////////////////boton arriba/////////////////////////////
botonArribaState = digitalRead(botonArriba);
if(botonArribaState == LOW){
//botonArribaDown = millis(); //record the time the button went down
while(digitalRead(botonArriba)==LOW) {
Serial.println("boton aun presionado, esperando...");
}
Serial.println("Boton arriba soltado");
//wait until the button is released
menu--; // increment the page to display
if (menu<0) menu=3;
menuChange=true;
botonArribaDown=millis();
}
}
void botonEnterF()
{
//////////////////boton enter//////////////////////////////
botonEnterState = digitalRead(botonEnter);
if(botonEnterState == LOW){
Serial.println("Boton enter presionado");
//botonEnterDown = millis(); //record the time the button went down
while(digitalRead(botonEnter)==LOW) {
Serial.println("boton enter aun presionado esperando...");
//wait until the button is released
}
Serial.println("Boton enter soltado");
menuChange=false;
subMenuChange=true;
botonEnterDown=millis();
}
}
void botonAtrasF()
{
//////////boton back///////////////////////
botonAtrasState = digitalRead(botonAtras);
if(botonAtrasState == LOW){
//botonAtrasDown = millis(); //record the time the button went down
Serial.println("Boton atras presionado");
while(digitalRead(botonAtras)==LOW) {
//wait until the button is released
Serial.println("Boton aun presionado, esperando...");
}
Serial.println("Boton soltado");
menuChange=true;
subMenuChange=false;
botonAtrasDown=millis();
}
}
void Menu()
{
switch(menu)
{
case 1:
lcd.clear();
lcd.print(">LED A");
lcd.setCursor(0, 1);
lcd.print(" LED B");
break;
case 2:
lcd.clear();
lcd.print(" LED A");
lcd.setCursor(0, 1);
lcd.print(">LED B");
break;
case 3:
lcd.clear();
lcd.print(" LED B");
lcd.setCursor(0, 1);
lcd.print(">VOLT");
break;
}
}
void subMenu()
{
switch(menu)
{
case 1:
ledA();
break;
case 2:
ledB();
break;
case 3:
volt();
break;
}
}
void ledA()
{
while(botonEnterState==LOW)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(">Brillo pot: ");
led2Brillo = analogRead(potenciometro)/ 4;
analogWrite(LED,led2Brillo);
lcd.setCursor(0, 1);
lcd.print(led2Brillo);
//botonAtrasF();
if(botonAtrasState==HIGH)
{
menuChange=true;
}
}
}
void ledB()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Brillo: ");
if(ledBrillo>0)
{
ledBrillo-=5;
analogWrite(LED, ledBrillo);
}
else if(ledBrillo<255)
{
ledBrillo+=5;
analogWrite(LED, ledBrillo);
}
lcd.setCursor(0, 1);
lcd.print(ledBrillo);
delay(100);
}
void volt()
{
while(botonEnterState==LOW)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Voltaje: ");
led2Brillo = analogRead(potenciometro);
voltaje = led2Brillo * (5.0 / 1023.0); // mide voltaje de 0 a 5.}
lcd.setCursor(0,1);
lcd.print(voltaje);
botonAtrasF();
}
}