TFT buttons

Hello All

I used a TFT 2.4 inches and arduio uno to make a menu application with the first page ( page 0)contains two buttons ( button A and button B) if button A is pressed it is supposed to open a first page ( page 1) with also two buttons ( button 1 and button 2) and if button B ( page 0) is pressed it is supposed to open a new page ( page 2) with also two buttons. however compiling and uploading done without any error messages the tft shows only ( page 0) with its two inactive buttons ( A & B). any help of this will be highly appreciated.

the code is attached

tft_test_8.ino (3.33 KB)

Your code COULD, and SHOULD, have been posted inline, in code tags, like so:

#include <Adafruit_TFTLCD.h> 
#include <Adafruit_GFX.h>    
#include <TouchScreen.h>

#define LCD_CS A3 
#define LCD_CD A2 
#define LCD_WR A1 
#define LCD_RD A0 
#define LCD_RESET A4 

#define TS_MINX 122
#define TS_MINY 111
#define TS_MAXX 942
#define TS_MAXY 890

#define YP A3
#define XM A2
#define YM 9
#define XP 8

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

#define MINPRESSURE 10
#define MAXPRESSURE 1000
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
int page = 0;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);

boolean buttonEnabled = true;

void setup() {
  
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(0x7575);
  tft.setRotation(1);
  tft.fillScreen(YELLOW);
  tft.drawRect(0,0,240,320,YELLOW);
  
  
  
  tft.fillRect(10,10, 280, 50, RED);
  tft.drawRect(10,10, 280, 50,BLACK);
  tft.setCursor(20,15);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.print("A");

  tft.fillRect(10,150, 280, 50, GREEN);
  tft.drawRect(10,150, 280, 50,BLACK);
  tft.setCursor(20,155);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.print("B");

}

void loop() {
    
 TSPoint p = ts.getPoint();
  if (p.z > MINPRESSURE && p.z <MAXPRESSURE ) {
    
   p.x = map(p.x, TS_MAXX, TS_MINX, tft.width(),0);
   p.y = map(p.y, TS_MAXY, TS_MINY, tft.height(),0);
       
   if(p.x>10 && p.x<60 && p.y>10 && p.y<290 && buttonEnabled){
    if (page ==2){
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);

 tft.fillRect(10,10, 280, 50, BLUE);
  tft.drawRect(10,10, 280, 50,BLACK);
  tft.setCursor(20,15);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.print("C");
    }
  if (page ==1){
 
     tft.fillRect(10,10, 280, 50, RED);
  tft.drawRect(10,10, 280, 50,BLACK);
  tft.setCursor(20,15);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.print("1");

   tft.fillRect(10,150, 280, 50, RED);
  tft.drawRect(10,150, 280, 50,BLACK);
  tft.setCursor(20,155);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.print("2");
    }
     
     TSPoint p = ts.getPoint();
  if (p.z > MINPRESSURE && p.z <MAXPRESSURE ) {
    
   p.x = map(p.x, TS_MAXX, TS_MINX, tft.width(),0);
   p.y = map(p.y, TS_MAXY, TS_MINY, tft.height(),0);
   
  if(p.x>180 && p.x<230 && p.y>10 && p.y<290 && buttonEnabled){
    if ( page ==2){
   buttonEnabled = false;

    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
  
      tft.fillRect(10,10, 280, 50, GREEN);
  tft.drawRect(10,10, 280, 50,BLACK);
  tft.setCursor(20,15);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.print("3");

  tft.fillRect(10,150, 280, 50, GREEN);
  tft.drawRect(10,150, 280, 50,BLACK);
  tft.setCursor(20,155);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.print("4");

   } 

     if ( page ==1){
   buttonEnabled = false;

    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
  
      tft.fillRect(10,10, 280, 50, GREEN);
  tft.drawRect(10,10, 280, 50,BLACK);
  tft.setCursor(20,15);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.print("Z");

  tft.fillRect(10,150, 280, 50, GREEN);
  tft.drawRect(10,150, 280, 50,BLACK);
  tft.setCursor(20,155);
  tft.setTextColor(WHITE);
  tft.setTextSize(5);
  tft.print("Y");
  }

   if (page ==0){
    page = 1;
 
   }
   }
  
  }
 
  } 
  }
   
    }

You should have used Tools + Auto Format to fix that atrocious mess before you posted it.

     TSPoint p = ts.getPoint();

This second statement contains the SAME point as the first statement. It is COMPLETELY useless.

I’ve never understood why anyone would test for page equal 2 and then test for page equal 1. Test the pages IN ORDER.

When you properly indent your code, and properly post it, I’ll try again to make sense of it.

Meanwhile, what do your Serial.print()s tell you is happening?

I am so sorry for the unorganized post, it is my first post and thank you for putting my code in proper format. could you please advice why it is not working. I didn't print anything in serial

for putting my code in proper format.

I didn't. I simply posted it properly. YOU need to use Tools + Auto Format to properly indent your code, and post it again.

I can not make heads or tails of code that jerks all over the place.

#include <Adafruit_TFTLCD.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define TS_MINX 122
#define TS_MINY 111
#define TS_MAXX 942
#define TS_MAXY 890

#define YP A3
#define XM A2
#define YM 9
#define XP 8

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

#define MINPRESSURE 10
#define MAXPRESSURE 1000
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
int page = 0;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);

boolean buttonEnabled = true;

void setup() {

tft.reset();
uint16_t identifier = tft.readID();
tft.begin(0x7575);
tft.setRotation(1);
tft.fillScreen(YELLOW);
tft.drawRect(0, 0, 240, 320, YELLOW);

tft.fillRect(10, 10, 280, 50, RED);
tft.drawRect(10, 10, 280, 50, BLACK);
tft.setCursor(20, 15);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(“A”);

tft.fillRect(10, 150, 280, 50, GREEN);
tft.drawRect(10, 150, 280, 50, BLACK);
tft.setCursor(20, 155);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(“B”);

}

void loop() {

TSPoint p = ts.getPoint();
if (p.z > MINPRESSURE && p.z < MAXPRESSURE ) {

p.x = map(p.x, TS_MAXX, TS_MINX, tft.width(), 0);
p.y = map(p.y, TS_MAXY, TS_MINY, tft.height(), 0);

if (p.x > 10 && p.x < 60 && p.y > 10 && p.y < 290 && buttonEnabled) {
if (page == 2) {
buttonEnabled = false;

pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

tft.fillRect(10, 10, 280, 50, BLUE);
tft.drawRect(10, 10, 280, 50, BLACK);
tft.setCursor(20, 15);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(“C”);
}
if (page == 1) {

tft.fillRect(10, 10, 280, 50, RED);
tft.drawRect(10, 10, 280, 50, BLACK);
tft.setCursor(20, 15);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(“1”);

tft.fillRect(10, 150, 280, 50, RED);
tft.drawRect(10, 150, 280, 50, BLACK);
tft.setCursor(20, 155);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(“2”);
}

TSPoint p = ts.getPoint();
if (p.z > MINPRESSURE && p.z < MAXPRESSURE ) {

p.x = map(p.x, TS_MAXX, TS_MINX, tft.width(), 0);
p.y = map(p.y, TS_MAXY, TS_MINY, tft.height(), 0);

if (p.x > 180 && p.x < 230 && p.y > 10 && p.y < 290 && buttonEnabled) {
if ( page == 2) {
buttonEnabled = false;

pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

tft.fillRect(10, 10, 280, 50, GREEN);
tft.drawRect(10, 10, 280, 50, BLACK);
tft.setCursor(20, 15);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(“3”);

tft.fillRect(10, 150, 280, 50, GREEN);
tft.drawRect(10, 150, 280, 50, BLACK);
tft.setCursor(20, 155);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(“4”);

}

if ( page == 1) {
buttonEnabled = false;

pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

tft.fillRect(10, 10, 280, 50, GREEN);
tft.drawRect(10, 10, 280, 50, BLACK);
tft.setCursor(20, 15);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(“Z”);

tft.fillRect(10, 150, 280, 50, GREEN);
tft.drawRect(10, 150, 280, 50, BLACK);
tft.setCursor(20, 155);
tft.setTextColor(WHITE);
tft.setTextSize(5);
tft.print(“Y”);
}

if (page == 0) {
page = 1;

}
}

}

}
}

}

Put EVERY { on a line BY ITSELF. Put EVERY } on a line BY ITSELF.

Get rid of the useless blank lines.

Pay attention to, and correct, the issues that have already been pointed out.

Use meaningful names.

Learn to use Serial.print() to see what your program is doing. What is page, each time loop() is called? Is buttonEnabled when loop() is called?

Where was the screen touched, if it was touched?

Debugging by guesswork sucks. Debugging with facts is orders of magnitude faster.