How to create a working return button in TFT, Adafuit?

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

// define the pins used to communicate with the display
#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

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

// create an instance of the TFT display object
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 364);

boolean buttonEnabled = true;

void setup() {
  
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(1);
  tft.fillScreen(BLUE);

  tft.setCursor(180,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(4);
  tft.print("MENU");

  tft.setCursor(20,40);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("04/11/2023");

  tft.setCursor(20,30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("EAT'S TUESDAY!");

  tft.fillRect(20,80, 80, 40, MAGENTA);
  tft.drawRect(20,80,80,40,BLACK);
  tft.setCursor(30,95);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("ADOBO");

  tft.fillRect(110,80, 80, 40, MAGENTA);
  tft.drawRect(110,80,80,40,BLACK);
  tft.setCursor(120,95);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SINIGANG");

  tft.fillRect(200,80, 80, 40, MAGENTA);
  tft.drawRect(200,80,80,40,BLACK);
  tft.setCursor(210,95);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TULINGAN");

  tft.fillRect(20,180, 80, 40, MAGENTA);
  tft.drawRect(20,180,80,40,BLACK);
  tft.setCursor(30,195);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SIOMAI");

  tft.fillRect(110,180, 80, 40, MAGENTA);
  tft.drawRect(110,180,80,40,BLACK);
  tft.setCursor(120,195);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("MENUDO");

  tft.fillRect(200,180, 80, 40, MAGENTA);
  tft.drawRect(200,180,80,40,BLACK);
  tft.setCursor(210,195);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TINOLA");

  tft.fillRect(20,130, 80, 40, MAGENTA);
  tft.drawRect(20,130,80,40,BLACK);
  tft.setCursor(30,145);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("CHICKEN");

  tft.fillRect(110,130, 80, 40, MAGENTA);
  tft.drawRect(110,130,80,40,BLACK);
  tft.setCursor(120,145);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("AFRITADA");

  tft.fillRect(200,130, 80, 40, MAGENTA);
  tft.drawRect(200,130,80,40,BLACK);
  tft.setCursor(210,145);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("KARE-KARE");
}

void loop() {
  TSPoint p = ts.getPoint();
  
  if (p.z > ts.pressureThreshhold) {
    
   p.x = map(p.x, TS_MAXX, TS_MINX, 0, 320);
   p.y = map(p.y, TS_MAXY, TS_MINY, 0, 480);
       
   if(p.x>50 && p.x<260 && p.y>180 && p.y<270 && buttonEnabled){
    
    buttonEnabled = false;
    
    pinMode(XM, OUTPUT);
    pinMode(YP, OUTPUT);
    
    tft.fillScreen(BLUE);
    
    tft.setCursor(30,30);
    tft.setTextColor(YELLOW);
    tft.setTextSize(3);
    tft.print("SINIGANG");
    
    tft.setCursor(30,60);
    tft.setTextColor(WHITE);
    tft.setTextSize(3);
    tft.print("Php 60.00");    

    tft.setCursor(30,100);
    tft.setTextColor(YELLOW);
    tft.setTextSize(3);
    tft.print("Your Balance:");

    tft.setCursor(30,130);
    tft.setTextColor(WHITE);
    tft.setTextSize(3);
    tft.print("Php 500.00"); 
    
    tft.fillRect(190,180, 100, 40, RED);
    tft.drawRect(190,180,100,40,WHITE);
    tft.setCursor(225, 193);
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.print("PAY");        

    tft.fillRect(30,180, 100, 40, BLUE);
    tft.drawRect(30,180,100,40,BLACK);
    tft.setCursor(45,193);
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.print("RETURN");
  }      
 }
}

I need the return button to work, and return to the first page (see void setup). What code could we need for the button to work to return to the first page, without duplicating the latter.

Start by putting the code that outputs the first page into a function rather than in setup() then you can call it from anywhere when you want to output it again

Can you give me an example line of code?

void setup()
{
  Serial.begin(115200);
  firstScreen();  //draw the first screen
}

void loop()
{
  //when later in the code you want to go back to the first screen
  firstScreen();
}

void firstScreen()
{
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.setRotation(1);
  tft.fillScreen(BLUE);
  tft.setCursor(180, 30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(4);
  tft.print("MENU");
  tft.setCursor(20, 40);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("04/11/2023");
  tft.setCursor(20, 30);
  tft.setTextColor(YELLOW);
  tft.setTextSize(1);
  tft.print("EAT'S TUESDAY!");
  tft.fillRect(20, 80, 80, 40, MAGENTA);
  tft.drawRect(20, 80, 80, 40, BLACK);
  tft.setCursor(30, 95);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("ADOBO");
  tft.fillRect(110, 80, 80, 40, MAGENTA);
  tft.drawRect(110, 80, 80, 40, BLACK);
  tft.setCursor(120, 95);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SINIGANG");
  tft.fillRect(200, 80, 80, 40, MAGENTA);
  tft.drawRect(200, 80, 80, 40, BLACK);
  tft.setCursor(210, 95);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TULINGAN");
  tft.fillRect(20, 180, 80, 40, MAGENTA);
  tft.drawRect(20, 180, 80, 40, BLACK);
  tft.setCursor(30, 195);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("SIOMAI");
  tft.fillRect(110, 180, 80, 40, MAGENTA);
  tft.drawRect(110, 180, 80, 40, BLACK);
  tft.setCursor(120, 195);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("MENUDO");
  tft.fillRect(200, 180, 80, 40, MAGENTA);
  tft.drawRect(200, 180, 80, 40, BLACK);
  tft.setCursor(210, 195);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("TINOLA");
  tft.fillRect(20, 130, 80, 40, MAGENTA);
  tft.drawRect(20, 130, 80, 40, BLACK);
  tft.setCursor(30, 145);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("CHICKEN");
  tft.fillRect(110, 130, 80, 40, MAGENTA);
  tft.drawRect(110, 130, 80, 40, BLACK);
  tft.setCursor(120, 145);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("AFRITADA");
  tft.fillRect(200, 130, 80, 40, MAGENTA);
  tft.drawRect(200, 130, 80, 40, BLACK);
  tft.setCursor(210, 145);
  tft.setTextColor(WHITE);
  tft.setTextSize(1);
  tft.print("KARE-KARE");
}

NOTE : This will not compile because I didn't bother to declare and define all of your variables but it shows the principle

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