I'm working on a simple menu & sub menu using 3 buttons (up/down/select)
up/down controls main menu unless I press select, then it controls the sub menu instead,
I can't seem to figure out how to make the select button take me back to controlling the main menu, any ideas?
https://www.youtube.com/watch?v=0M3XRUYQLEg
#include <SPI.h>
#include <ST7789_t3.h> // Hardware-specific library
// pin definition for teensy 4.0 + ST7739
#define TFT_CS 10
#define TFT_MOSI 11
#define TFT_MISO 12
#define TFT_SCK 13
#define TFT_RST 14
#define TFT_DC 15
// For 1.9" TFT with ST7789
ST7789_t3 tft = ST7789_t3(TFT_CS, TFT_DC, TFT_RST);
volatile byte menuPage = 0; // stores the current page position.
volatile byte oldMenuPage = 0; // stores the last page position.
volatile byte subPage = 0; // stores the current preset position.
volatile byte oldsubPage = 0; // stores the last preset position.
// Button reading, including debounce without delay function declarations
const byte upButton = 23; // Up Button
const byte downButton = 22; // Down Button
const byte selectButton = 21; // Select Button
byte oldButtonState1 = HIGH; // assume switch open because of pull-down resistor
byte oldButtonState2 = HIGH; // assume switch open because of pull-down resistor
byte oldButtonState3 = HIGH; // assume switch open because of pull-down resistor
const unsigned long debounceTime = 100; // milliseconds
unsigned long buttonPressTime; // when the switch last changed state
boolean buttonPressed = 0; // a flag variable
volatile boolean mainMenu = true;
//volatile boolean subMenu = false;
//******************************************************************************************
void setup() {
// button section of setup
pinMode (upButton, INPUT_PULLUP); // setup the button pins
pinMode (downButton, INPUT_PULLUP);
pinMode (selectButton, INPUT_PULLUP);
tft.init(172, 320); // initialize a ST7789 chip, 172x320 pixels
tft.setRotation(1);
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(3);
// tft.setFont(&TypoHoopRegular20pt7b);
tft.setCursor(5, 146);
tft.print("<");
tft.setCursor(302, 146);
tft.print(">");
tft.setCursor(130, 5);
tft.print("PAGE 1");
}
//******************************************************************************************
void loop() {
menuList();
subPageList();
readSelectButton();
readUpButton();
readDownButton();
// **** to do **** if select button pressed, flip-flop between presetList() and menuList()
// **** to do **** if mainMenu active, up/down buttons control mainMenu only
// **** to do **** if subMenu active, up/down buttons control subMenu only
} // end of loop
//******************************************************************************************
void readUpButton() {
// UP BUTTON
byte buttonState2 = digitalRead (upButton);
if (buttonState2 != oldButtonState2) {
if (millis () - buttonPressTime >= debounceTime) { // debounce
buttonPressTime = millis (); // when we closed the switch
oldButtonState2 = buttonState2; // remember for next time
if (buttonState2 == LOW && mainMenu == true) { // if button up pressed & main menu is active
menuPage ++; //increment the current menu page's position count
if (menuPage > 2) {
menuPage = 0; //0-2 (3 menu pages)
}
}
else if (buttonState2 == LOW && mainMenu == false) { // if button up pressed & sub menu is active
subPage ++;
if (subPage > 5) {
subPage = 0; ///0-5 (6 sub menu pages)
}
} // end of else if
} // end if debounce time up
} // end of state change
} // end of function
void readDownButton() {
// Down Button
byte buttonState1 = digitalRead (downButton);
if (buttonState1 != oldButtonState1) {
if (millis () - buttonPressTime >= debounceTime) { // debounce
buttonPressTime = millis (); // when we closed the switch
oldButtonState1 = buttonState1; // remember for next time
if (buttonState1 == LOW && mainMenu == true) { // if button down pressed & main menu is active
// "Button Pressed"
menuPage --; //decrement the current menu page's position count
if (menuPage > 200) {
menuPage = 2; ///0-2 (3 menu pages)
}
}
else if (buttonState1 == LOW && mainMenu == false) { // if button up pressed & sub menu is active
subPage --;
if (subPage > 200) {
subPage = 5; ///0-5 (6 sub menu pages)
}
} // end of else if
} // end if debounce time up
} // end of state change
} // end of function
void readSelectButton() {
// Select Button
byte buttonState3 = digitalRead (selectButton);
if (buttonState3 != oldButtonState3) {
if (millis () - buttonPressTime >= debounceTime) { // debounce
buttonPressTime = millis (); // when we closed the switch
oldButtonState3 = buttonState3; // remember for next time
if (buttonState3 == LOW) {
// "Button Pressed"
mainMenu = false; // if pressed again make true
//subMenu = true;
}
} // end if debounce time up
} // end of state change
} // end of function
void menuList() {
if (oldMenuPage != menuPage) {
oldMenuPage = menuPage;
// 3 menu pages
switch (menuPage) {
case 0: // Page 1
tft.fillScreen(ST7735_BLACK);
tft.setCursor(5, 146);
tft.print("<");
tft.setCursor(302, 146);
tft.print(">");
tft.setCursor(130, 5);
tft.print("PAGE 1");
// load subPage() if selected<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
break;
case 1: // Page 2
tft.fillScreen(ST7735_BLACK);
tft.setCursor(5, 146);
tft.print("<");
tft.setCursor(302, 146);
tft.print(">");
tft.setCursor(130, 5);
tft.print("PAGE 2");
// load subPage() if selected<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
break;
case 2: // Page 3
tft.fillScreen(ST7735_BLACK);
tft.setCursor(5, 146);
tft.print("<");
tft.setCursor(302, 146);
tft.print(">");
tft.setCursor(130, 5);
tft.print("PAGE 3");
// load subPage() if selected<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
break;
}
}
}
void subPageList() {
if (oldsubPage != subPage) {
oldsubPage = subPage;
// Program List:
switch (subPage) {
case 0: // Preset 0
//delete old number
tft.fillScreen(ST7735_BLACK);
//print new text
tft.setCursor(150, 136);
tft.print("1");
break;
case 1: // Preset 1
//delete old number
tft.fillScreen(ST7735_BLACK);
//print new text
tft.setCursor(150, 136);
tft.print("2");
break;
case 2: // Preset 2
//delete old text
tft.fillScreen(ST7735_BLACK);
//print new text
tft.setCursor(150, 136);
tft.print("3");
break;
case 3: // Preset 3
//delete old text
tft.fillScreen(ST7735_BLACK);
//print new text
tft.setCursor(150, 136);
tft.print("4");
break;
case 4: // Preset 4
//delete old text
tft.fillScreen(ST7735_BLACK);
//print new text
tft.setCursor(150, 136);
tft.print("5");
break;
case 5: // Preset 5
//delete old text
tft.fillScreen(ST7735_BLACK);
//print new text
tft.setCursor(150, 136);
tft.print("6");
break;
}
}
}