Menu using two or three tactile button in st7735 1.8 inch display

Hi,

I want to create a simple menu using arduino mega in st7735 1.8 inch display. I want to have 3 layers of menu.

  1. Main menu
  2. Submenu (inside main menu)
  3. SubSubmenu (inside 2. submenu)

1st Main menu is opening using one button and than submenu which should appear only when 2nd button is pressed and than another SubSubmenu inside above mentioned submenu which should work when 3rd button is pressed. I also want to use 1st button as back and 3rd button as ok option in case of 1st submenu. I tried to write down a code for that using switch and if else but there is some problem. My 2nd if condition is not working inside switch. can someone please help me.

thanks in advance. I am attaching the code file here.

#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <Bounce2.h>
#include <SPI.h>

#define TFT_DC 8
#define TFT_RST 9
#define TFT_CS 10
#define CARD_CS 11
#define TFT_SCLK 50
#define TFT_MOSI 51
#define TFT_MISO 52
//define Vin 5V
//define GND GND
//define LITE 3.3V

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

#define tftPrintTest()

// Define Buttons//

int MainMenuButton;
int SubMenuButton;
int OKMenuButton;

Bounce debouncer1 = Bounce();

const byte MainMenuButtonPin = 2;
const byte SubMenuButtonPin = 3;
const byte OKMenuButtonPin = 4;

int x = -1;
int y = -1;
int z = -1;

void setup(void)
{
Serial.begin(9600);

tft.initR(INITR_BLACKTAB);
Serial.println(“Initialized”);
uint16_t time = millis();
tft.fillScreen(ST7735_YELLOW);
time = millis() - time;
Serial.println(time, DEC);
delay(500);
tftPrintTest();
delay(400);

pinMode(MainMenuButtonPin, INPUT);
pinMode(SubMenuButtonPin, INPUT);
pinMode(OKMenuButtonPin, INPUT);
debouncer1.attach(SubMenuButtonPin);
debouncer1.interval(5);

tft.clear();
tft.fillScreen(ST7735_WHITE);
tft.setCursor(5, 40);
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.println(“WELCOME NANOBIOS LAB”);
tft.setCursor(20,80);
tft.println(“press button 6”);
delay(500);
}

void loop()
{
MainMenuButton = digitalRead(MainMenuButtonPin);
SubMenuButton = digitalRead(SubMenuButtonPin);
OKMenuButton = digitalRead(OKMenuButtonPin);

if (MainMenuButton == HIGH)
{
x = x + 1;
delay(50);
switch (x)
{
case 0:

TFT_Main_Menu();
break;
delay(500);

case 1:

TFT_Main_Menu();
tft.fillRect(2, 20, 2, 17, ST7735_BLUE);
tft.fillRect(115, 20, 2, 17, ST7735_BLUE);
tft.fillRect(2, 20, 115, 2, ST7735_BLUE);
tft.fillRect(2, 37, 115, 2, ST7735_BLUE);

debouncer1.update();
SubMenuButton = debouncer1.read();

if (SubMenuButton == HIGH)

{
z = z+1;
delay(50);
switch (z)
{
case 0:

TFT_Sub_Menu();
break;
delay(500);

Serial.println(“switch is working”);
}
}
else
{
Serial.println(“switch is not working”);
}
break;
delay(500);

case 2:

TFT_Main_Menu();
tft.fillRect(2, 43, 2, 17, ST7735_BLUE);
tft.fillRect(115, 43, 2, 17, ST7735_BLUE);
tft.fillRect(2, 43, 115, 2, ST7735_BLUE);
tft.fillRect(2, 60, 115, 2, ST7735_BLUE);

if (SubMenuButton == HIGH)
{
z = z + 1;
delay(50);
switch (z)
{
case 0:

TFT_Sub_Menu();
break;
delay(500);

Serial.println(“switch is working”);
}
}
else
{
Serial.println(“switch is not working”);
}

break;
delay(500);

case 3:

TFT_Main_Menu();
tft.fillRect(2, 69, 2, 17, ST7735_BLUE);
tft.fillRect(115, 69, 2, 17, ST7735_BLUE);
tft.fillRect(2, 69, 115, 2, ST7735_BLUE);
tft.fillRect(2, 85, 115, 2, ST7735_BLUE);

if (SubMenuButton == HIGH)
{
Serial.println(“switch is working”);
}
else
{
Serial.println(“switch is not working”);
}

break;
delay(500);

case 4:

TFT_Main_Menu();
tft.fillRect(2, 92, 2, 17, ST7735_BLUE);
tft.fillRect(115, 92, 2, 17, ST7735_BLUE);
tft.fillRect(2, 92, 115, 2, ST7735_BLUE);
tft.fillRect(2, 109, 115, 2, ST7735_BLUE);

if (SubMenuButton == HIGH)
{
Serial.println(“switch is working”);
}
else
{
Serial.println(“switch is not working”);
}

break;
delay(500);

case 5:

TFT_Main_Menu();
tft.fillRect(2, 115, 2, 17, ST7735_BLUE);
tft.fillRect(115, 115, 2, 17, ST7735_BLUE);
tft.fillRect(2, 115, 115, 2, ST7735_BLUE);
tft.fillRect(2, 132, 115, 2, ST7735_BLUE);

if (SubMenuButton == HIGH)
{
Serial.println(“switch is working”);
}
else
{
Serial.println(“switch is not working”);
}

break;
delay(500);

default:
TFT_Main_Menu();
tft.fillRect(2, 20, 2, 17, ST7735_BLUE);
tft.fillRect(115, 20, 2, 17, ST7735_BLUE);
tft.fillRect(2, 20, 115, 2, ST7735_BLUE);
tft.fillRect(2, 37, 115, 2, ST7735_BLUE);

if (SubMenuButton == HIGH)

{
Serial.println(“switch is working”);
}
else
{
Serial.println(“switch is not working”);
}
x=1;
break;
delay(500);

}
}
}

void TFT_Main_Menu() {
tft.fillScreen(ST7735_WHITE);
tft.setCursor(5, 5);
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.println(“Date”);
tft.setCursor(95, 5);
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.println(“Time”);
tft.setCursor(0, 25);
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.println(" 1. ALBUMIN");
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.println(" “);
tft.println(” “);
tft.println(” 2. CREATININE");
tft.setCursor(113, 150);
tft.setTextColor(ST7735_BLACK);
tft.setTextSize(1);
tft.println(“OK”);
tft.setCursor(0, 150);
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.println(“DOWN”);
}
void TFT_Sub_Menu()
{
tft.setCursor(0, 25);
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.println(" 1. Perform Test");
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.println(" “);
tft.println(” “);
tft.println(” 2. Perform Calibration");
tft.setTextColor(ST7735_RED);
tft.setCursor(113, 150);
tft.setTextColor(ST7735_BLACK);
tft.setTextSize(1);
tft.println(“OK”);
tft.setCursor(0, 150);
tft.setTextColor(ST7735_RED);
tft.setTextSize(1);
tft.println(“DOWN”);
}

Menu_1.ino (8.35 KB)