Backbutton menu no working

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");
}

why isn't the "backButton" checked in loop() like the 3 other buttons?

I put it in a different command and it can only be accessed in a different menu so in certain menus the button can be pressed

I want action 1, action 2 and action 3 to be used as a loop so when it's already in action 1 then test() and action1() can run so the buttons in the loop are no longer used because the page has moved, what should I change?

i think there are numerous problems in the code, too many to explain

please study this

# include "SPI.h"
# include "Adafruit_GFX.h"
# include "Adafruit_ILI9341.h"

byte PinsBut [] = { 3, 2, 4, 5 };
byte PinsLed [] = { 9, 10, 11, 12 };

#define N_BUT   sizeof(PinsBut)

byte butLst [N_BUT];

#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);

#define Nmenu 3
int menu;

// -----------------------------------------------------------------------------
void
tftDisp (
    const char *s0,
    const char *s1,
    const char *s2 )
{
    tft.fillScreen (ILI9341_BLACK);
    tft.setTextColor (ILI9341_WHITE);
    tft.setTextSize (2);

    if (s0) {
        tft.fillRect (0,100,250, 40,ILI9341_GREEN);
        tft.setCursor (20,93+20);
        tft.print (s0);
    }

    if (s0) {
    }
        tft.fillRect (0,150,250, 40,ILI9341_RED);
        tft.setCursor (20,145+20);
        tft.print (s1);

    if (s0) {
        tft.fillRect (0,200,250, 40,ILI9341_CYAN);
        tft.setCursor (20,193+20);
        tft.print (s2);
    }
}


// -----------------------------------------------------------------------------
enum { B_Menu, B_Sel, B_Down, B_Up };

int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(PinsBut); n++)  {
        byte but = digitalRead (PinsBut [n]);

        if (butLst [n] != but)  {
            butLst [n] = but;

            delay (10);     // debounce

            if (LOW == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case B_Menu:
        updateMenu ();
        break;

    case B_Sel:
        executeAction ();
        break;

    case B_Down:
        if (Nmenu <= ++menu)
            menu = 0;
        updateMenu ();
        break;

    case B_Up:
        if (0 > --menu)
            menu = Nmenu -1;
        updateMenu ();
        break;
    }
}

// -----------------------------------------------------------------------------
void updateMenu () {
    switch (menu) {
    case 0:
        tftDisp (">AUTOMATIC", " MANUAL", " CALIBRATION");
        break;

    case 1:
        tftDisp (" AUTOMATIC", ">MANUAL", " CALIBRATION");
        break;

    case 2:
        tftDisp (" AUTOMATIC", " MANUAL", ">CALIBRATION");
        break;
    }
}

// ---------------------------------------------------------
void executeAction () {
    switch (menu) {
    case 0:
        action0 ();
        break;

    case 1:
        action1 ();
        break;

    case 2:
        action2 ();
        break;
    }
}

// ---------------------------------------------------------
void action0 () {
    tftDisp ("Execute #0", NULL, NULL);
}

void action1 () {
    tftDisp ("Execute #1", NULL, NULL);
}

void action2 () {
    tftDisp ("Execute #2", NULL, NULL);
}

// -----------------------------------------------------------------------------
void setup ()
{
    //Initialize TFT LCD
    tft.begin ();
    //tft.setRotation (1);

    for (unsigned n = 0; n < sizeof(PinsBut); n++)  {
        pinMode (PinsLed [n], OUTPUT);
        pinMode (PinsBut [n], INPUT_PULLUP);
        butLst [n] = digitalRead (PinsBut [n]);
    }

    tftDisp ("Ready", NULL, NULL);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.