Hello, my problem consists that i have to realize a program with the use of the LCD screen 16x02 lets me create a menu and select between 3 options to exectue different programs: control the led intensity with a potentiometer, control led intensity with buttons and read the voltage of the potentiometer. i have the code that makes the menu and the submenus of each program but i have a few errors: it doesnt let me go down in the beggining until i press the enter button, when i press the enter button it lets me control the first option but it doesnt let me go back to the main menu if i press the back button, here is my code until now.
#include <LiquidCrystal.h>
#define LED 3
#define potenciometro A0
#define botonArriba 4
#define botonAbajo 5
#define botonEnter 6
#define botonAtras 7
int ledBrillo;
int led2Brillo;
int estado;
int cambio;
float voltaje;
int botonSuma;
int botonResta;
boolean pressArriba=0;
boolean pressAbajo=0;
boolean pressEnter=0;
boolean pressBack=0;
boolean conf=0;
int menu=1;
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);
actualizarMenu();
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(botonAbajo)==LOW)pressAbajo=1;
if(digitalRead(botonArriba)==LOW)pressArriba=1;
if(digitalRead(botonEnter)==LOW)pressEnter=1;
if(digitalRead(botonAtras)==LOW)pressBack=1;
if(digitalRead(botonEnter)==HIGH && pressEnter==1)
{
pressEnter=0;
conf=!conf;
if(conf)
{
ejecutarTarea();
}
else
{
lcd.clear();
actualizarMenu();
}
}
if(digitalRead(botonAbajo)==HIGH && pressAbajo==1)
{
pressAbajo=0;
if(conf)
{
menu++;
actualizarMenu();
}
//delay(100);
//while(!digitalRead(botonAbajo));
}
if(digitalRead(botonArriba)==HIGH && pressArriba==1)
{
pressArriba=0;
if(conf)
{
menu--;
actualizarMenu();
}
//delay(100);
//while(!digitalRead(botonArriba));
}
if(digitalRead(botonAtras)==HIGH && pressBack==1)
{
pressBack=0;
if(conf)
{
actualizarMenu();
}
//delay(100);
//while(!digitalRead(botonAtras));
}
}
void actualizarMenu()
{
switch(menu)
{
case 0:
menu = 1;
break;
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;
case 4:
menu = 3;
break;
}
}
void ejecutarTarea()
{
while(botonAtras!=HIGH && pressBack!=1)
{
switch(menu)
{
case 1:
ledA();
break;
case 2:
ledB();
break;
case 3:
volt();
break;
}
}
}
void ledA()
{
if(botonAtras==HIGH&&pressBack==1)
{
actualizarMenu();
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(">Brillo pot: ");
led2Brillo = analogRead(potenciometro)/ 4;
analogWrite(LED,led2Brillo);
lcd.setCursor(0, 1);
lcd.print(led2Brillo);
delay(100);
}
}
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()
{
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);
delay(100);
}
i have implemented a new code which controls the menu well and when i press the enter it executes the action correctly but if i press the back button it doesnt return to the submenu, heres my code now:
#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();
}
}
with each button function i have conditions for the debouncing and depending of the button it executes a different action if the button up or down is pressed it moves the main menu if i press the enter depending of the option im selecting it goes to the submenu with the option selected and it executes the actions but if i press the back button it doesnt return to the main menu
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();
}
}
these are the functions for my buttons "botonArribaF" and "botonAbajoF" lets me iterate through the menu and botonEnterF lets me enter the submenu Function the problem is that if i have a while inside the programs so that it lets me run them appropiately if i try to press the "botonAbajo" it doesnt return to the main menu