I am trying to make menu, for select button, up and down is working but when i make back button it is not working. Where did it go wrong?
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 48
#define TFT_CS 53
#define TFT_MOSI 51
#define TFT_CLK 52
#define TFT_MISO 50
#define TFT_RST 49
#define TFT_RST -1 // Set TFT_RST to -1 if display RESET is connected to ESP32 board RST
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
int upButton = 3;
int downButton = 2;
int selectButton = 4;
int backButton = 5;
int menu = 1;
int menu2 = 1;
void setup() {
//Initialize TFT LCD
tft.begin();
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
pinMode(backButton, INPUT_PULLUP);
updateMenu();
//Rotate the screen to right direction
//tft.setRotation(1);
}
void loop() {
if (!digitalRead(downButton)){
menu++;
updateMenu();
delay(100);
while (!digitalRead(downButton));
}
if (!digitalRead(upButton)){
menu--;
updateMenu();
delay(100);
while(!digitalRead(upButton));
}
if (!digitalRead(selectButton)){
executeAction();
// updateMenu();
delay(100);
while (!digitalRead(selectButton));
}
test();
}
void updateMenu() {
switch (menu) {
case 0:
menu = 1;
break;
case 1:
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
//
tft.fillRect(0,100,250, 40,ILI9341_GREEN);
tft.setCursor(20,93+20);
tft.print(">AUTOMATIC");
//
tft.fillRect(0,150,250, 40,ILI9341_RED);
tft.setCursor(20,145+20);
tft.print("MANUAL");
//
tft.fillRect(0,200,250, 40,ILI9341_CYAN);
tft.setCursor(20,193+20);
tft.print("CALIBRATION");
break;
case 2:
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
//
tft.fillRect(0,100,250, 40,ILI9341_GREEN);
tft.setCursor(20,93+20);
tft.print("AUTOMATIC");
//
tft.fillRect(0,150,250, 40,ILI9341_RED);
tft.setCursor(20,145+20);
tft.print(">MANUAL");
//
tft.fillRect(0,200,250, 40,ILI9341_CYAN);
tft.setCursor(20,193+20);
tft.print("CALIBRATION");
break;
case 3:
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
//
tft.fillRect(0,100,250, 40,ILI9341_GREEN);
tft.setCursor(20,93+20);
tft.print("AUTOMATIC");
//
tft.fillRect(0,150,250, 40,ILI9341_RED);
tft.setCursor(20,145+20);
tft.print("MANUAL");
//
tft.fillRect(0,200,250, 40,ILI9341_CYAN);
tft.setCursor(20,193+20);
tft.print(">CALIBRATION");
break;
case 4:
menu = 3;
break;
}
}
void executeAction() {
switch (menu) {
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
}
}
void test() {
if (!digitalRead(backButton)){
menu2++;
action1();
delay(100);
while(!digitalRead(backButton));
}
}
void action1() {
switch (menu2) {
case 0:
menu2 = 1;
break;
case 1:
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0,20,250, 40,ILI9341_GREEN);
tft.setCursor(10,15+20);
tft.print(">Executing #1");
break;
case 2:
updateMenu();
break;
case 4:
menu2 = 3;
break;
}
}
void action2() {
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0,20,250, 40,ILI9341_GREEN);
tft.setCursor(10,15+20);
tft.print(">Executing #2");
}
void action3() {
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(0,20,250, 40,ILI9341_GREEN);
tft.setCursor(10,15+20);
tft.print(">Executing #3");
}