Pressing the button for the tft LCD on off action doesn't work and I want valve1 in case 2 to only take the last position of valve1 in case 1. How to solve that?
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
const int buttonPin = 2;
int buttonState = 0; // variable for reading the pushbutton status
#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);
void setup() {
tft.begin();
pinMode(buttonPin, INPUT);
}
void loop() {
action2();
}
void action2() {
switch (menu3) {
case 0:
menu3 = 1;
break;
case 1:
//tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(84, 0 + 20);
tft.print("MANUAL");
tft.setTextColor(ILI9341_BLACK);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
tft.fillRect(35, 50, 169, 40, ILI9341_BLUE);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 1");
tft.fillRect(35, 100, 80, 40, ILI9341_RED);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
} else {
tft.fillRect(35, 50, 169, 40, ILI9341_BLUE);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 1");
tft.fillRect(35, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_RED);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
}
tft.fillRect(35, 150, 169, 40, ILI9341_GREEN);
tft.setCursor(83, 143 + 20);
tft.print("VALVE 2");
tft.fillRect(35, 200, 80, 40, ILI9341_GREEN);
tft.setCursor(65, 193 + 20);
tft.print("ON");
tft.fillRect(124, 200, 80, 40, ILI9341_RED);
tft.setCursor(150, 193 + 20);
tft.print("OFF");
break;
case 2:
//tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(84, 0 + 20);
tft.print("MANUAL");
tft.setTextColor(ILI9341_BLACK);
tft.fillRect(35, 50, 169, 40, ILI9341_GREEN);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 1");
tft.fillRect(35, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_RED);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
tft.fillRect(35, 50, 169, 40, ILI9341_BLUE);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 2");
tft.fillRect(35, 100, 80, 40, ILI9341_RED);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
} else {
tft.fillRect(35, 50, 169, 40, ILI9341_BLUE);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 2");
tft.fillRect(35, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_RED);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
}
break;
case 3:
//tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(84, 0 + 20);
tft.print("MANUAL");
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
tft.fillRect(35, 50, 169, 40, ILI9341_BLUE);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 3");
tft.fillRect(35, 100, 80, 40, ILI9341_RED);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
} else {
tft.fillRect(35, 50, 169, 40, ILI9341_BLUE);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 3");
tft.fillRect(35, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_RED);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
}
tft.fillRect(35, 150, 169, 40, ILI9341_GREEN);
tft.setCursor(83, 143 + 20);
tft.print("VALVE 4");
tft.fillRect(35, 200, 80, 40, ILI9341_GREEN);
tft.setCursor(65, 193 + 20);
tft.print("ON");
tft.fillRect(124, 200, 80, 40, ILI9341_RED);
tft.setCursor(150, 193 + 20);
tft.print("OFF");
break;
case 4:
//tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(84, 0 + 20);
tft.print("MANUAL");
tft.setTextColor(ILI9341_BLACK);
tft.fillRect(35, 50, 169, 40, ILI9341_GREEN);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 3");
tft.fillRect(35, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_RED);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
tft.fillRect(35, 50, 169, 40, ILI9341_BLUE);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 4");
tft.fillRect(35, 100, 80, 40, ILI9341_RED);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
} else {
tft.fillRect(35, 50, 169, 40, ILI9341_BLUE);
tft.setCursor(83, 43 + 20);
tft.print("VALVE 4");
tft.fillRect(35, 100, 80, 40, ILI9341_GREEN);
tft.setCursor(65, 93 + 20);
tft.print("ON");
tft.fillRect(124, 100, 80, 40, ILI9341_RED);
tft.setCursor(150, 93 + 20);
tft.print("OFF");
}
break;
case 5:
menu3 = 4;
break;
}
}