Pressing the button for the tft LCD on off action does not work

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

The sketch looks good... except these lines...

#define TFT_RST 49
#define TFT_RST  -1  // Set TFT_RST to -1 if display RESET is connected to ESP32 board RST

error: 'menu3' was not declared in this scope
   switch (menu3) {
           ^~~~~

Verify the button is wired correctly (on its own).


Also, include a drawing of how you connected your project.

Sorry I forgot to add int menu3 = 1. Below the sketch

Your code shows pin 2 is your button pin, and you want it to be tied "HIGH" when pressed.

The drawing you showed is from this web site:

For your code, read and follow the section labeled, "Arduino push button with external pull down resistor"

Not related to your problem but look my posts in this topic to learn a better way to build and manage menus.

1 Like

I'm making this project but I'm confused about the placement of the select button for on off and display so that case2 for valve 1 only takes the last status in case1 running

Placement where?

Also for the circuit you showed, the input pin should be configured "INPUT_PULLUP" not "INPUT".

Your TFT isn't touch?

Place the test for "select" and "display" at the top of the void loop()

Yes, no touch because I'm confused how to make the touch action

I've added it

Can you provide an example?

Yes, no touch because I'm confused how to make the touch action

Follow my link above and you will understand.

Yes, after you make the code corrections noted above (your code only compiles after making those changes), and read the work @FernandoGarcia has indicated in a link.

Ok, I will understand your code

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