Hello! I'm building a weather station with an Arduino Mega board and a TFT Display. I'm utilizing 4 buttons. So far every button displays something else on the display.
My goal is to have 2 submenus. One that's for weather stuff (temperature, humidity) and one that is for date stuff (date, time). I figured a good way to do this, is, when for example button 1 is pressed it'll open the submenu options. If you then press button 1 again, it won't open the submenu options again but instead display the temperature.
I've tried to do this with a switch, but no success.
Could anyone point me in the right direction to creating submenus?
Thanks in advance!
This is my code:
const int switchOnePin = 29;
const int switchTwoPin = 27;
const int switchThreePin = 25;
const int switchFourPin = 23;
int switchOneState = 0;
int lastSwitchOneState = 0;
int switchTwoState = 0;
int lastSwitchTwoState = 0;
int switchThreeState = 0;
int lastSwitchThreeState = 0;
int switchFourState = 0;
int lastSwitchFourState = 0;
void setup(void) {
Serial.begin(9600);
int switchOneState = 0;
int lastSwitchOneState = 0;
pinMode(switchOnePin, INPUT);
pinMode(switchTwoPin, INPUT);
pinMode(switchThreePin, INPUT);
pinMode(switchFourPin, INPUT);
tft.setCursor(0, 0);
tft.setTextSize(2);
tft.fillScreen(BLACK);
tft.println("Screen after turning it on");
}
void loop(void) {
tft.setCursor(0, 0);
tft.setTextSize(2);
switchOneState = digitalRead(switchOnePin);
switchTwoState = digitalRead(switchTwoPin);
switchThreeState = digitalRead(switchThreePin);
switchFourState = digitalRead(switchFourPin);
if (switchOneState != lastSwitchOneState && switchOneState == HIGH) {
//For opening the first submenu and selection option one
tft.fillScreen(BLACK);
tft.println("Button 1 (Pin 29)");
tft.println();
tft.println("Weather submenu");
tft.println("1. Temperature");
tft.println("2. Humidity");
tft.println("3. Random Information");
}
if (switchOneState != lastSwitchTwoState && switchTwoState == HIGH) {
//For opening submenu 2 and selecting the second option
tft.fillScreen(BLACK);
tft.println("Button 2 (Pin 27)");
tft.println();
tft.println("1. Date");
tft.println("2. Time");
tft.println("3. Random information");
}
if (switchOneState != lastSwitchThreeState && switchThreeState == HIGH) {
//This one is for selecting the third option
tft.fillScreen(BLACK);
}
if (switchOneState != lastSwitchFourState && switchFourState == HIGH) {
tft.fillScreen(BLACK);
tft.println("Button 4 Pin (23)");
tft.println();
tft.println("Interrupt");
}
delay(50);
lastSwitchOneState = switchOneState;
lastSwitchTwoState = switchTwoState;
lastSwitchThreeState = switchThreeState;
lastSwitchFourState = switchFourState;
delay(1);
}