Touch button not working in "Solid Colors"

Hello,

I have a question about my code.
I'm using Arduino Mega2560 with a ILI9486 TFT LCD shield.

When I start the progam it is showing the "Homescreen" like it should.
When I touch the "Solid colors" button it works fine and goes to the "solid colors" screen.
When I touch the "Back" button in that screen it should return to the "Homescreen", but this doesn't happen.
It looks like the "bool down = Touch_getXY()" function is not working in that screen.

What am I doing wrong?

#if 1

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 200
#define MAXPRESSURE 1000

const int XP = 8, XM = A2, YP = A3, YM = 9;  //320x480 ID=0x9486
const int TS_LEFT = 967, TS_RT = 94, TS_TOP = 914, TS_BOT = 237;

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

int currentPage = 0;

Adafruit_GFX_Button prog1_btn, back_btn;

int pixel_x, pixel_y;

bool Touch_getXY(void) {
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);  //restore shared pins
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);  //because TFT control pins
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.y, TS_LEFT, TS_RT, 0, 480);  //.kbv makes sense to me
    pixel_y = map(p.x, TS_TOP, TS_BOT, 0, 320);
  }
  return pressed;
}

#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 GREY 0xD7D7

void setup() {
  uint16_t ID = tft.readID();
  if (ID == 0xD3D3) ID = 0x9486;
  tft.begin(ID);
  tft.setRotation(1);
  HomeScreen();
  currentPage = 0;
}

void loop() {
  //Homescreen
  if (currentPage == 0) {
    bool down = Touch_getXY();
    prog1_btn.press(down && prog1_btn.contains(pixel_x, pixel_y));
    if (prog1_btn.justReleased())
      prog1_btn.drawButton();
    if (prog1_btn.justPressed()) {
      prog1_btn.drawButton(true);
      currentPage = 1;
      SolidColors();
    }
  }
}

void HomeScreen() {
  // Title
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.setCursor(65, 23);
  tft.print("Mobile Stage Light Controller");
  tft.fillRect(17, 20, 30, 6, RED);
  tft.fillRect(17, 27, 30, 6, WHITE);
  tft.fillRect(17, 34, 30, 6, BLUE);
  tft.fillRect(428, 20, 10, 20, RED);
  tft.fillRect(438, 20, 10, 20, YELLOW);
  tft.drawRect(16, 19, 32, 22, WHITE);
  tft.drawRect(427, 19, 32, 22, WHITE);
  tft.drawLine(0, 48, 480, 48, WHITE);
  tft.setCursor(155, 60);
  tft.print("Select Program");

  // Button - Program 1
  prog1_btn.initButton(&tft, 80, 100, 154, 30, WHITE, RED, BLACK, "Solid Colors", 2);
  prog1_btn.drawButton(false);
}

void SolidColors() {  // Program 1
  // Title
  currentPage = 1;
  tft.fillScreen(BLACK);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.setCursor(65, 23);
  tft.print("Mobile Stage Light Controller");
  tft.fillRect(17, 20, 30, 6, RED);
  tft.fillRect(17, 27, 30, 6, WHITE);
  tft.fillRect(17, 34, 30, 6, BLUE);
  tft.fillRect(428, 20, 10, 20, RED);
  tft.fillRect(438, 20, 10, 20, YELLOW);
  tft.drawRect(16, 19, 32, 22, WHITE);
  tft.drawRect(427, 19, 32, 22, WHITE);
  tft.drawLine(0, 48, 480, 48, WHITE);

  //Button - Back
  back_btn.initButton(&tft, 430, 200, 80, 30, WHITE, GREY, BLACK, "Back", 2);
  back_btn.drawButton(false);

  // Led Control
  if (currentPage == 1) {
    bool down = Touch_getXY();
    back_btn.press(down && back_btn.contains(pixel_x, pixel_y));
    if (back_btn.justReleased())
      back_btn.drawButton();
    if (back_btn.justPressed()) {
      back_btn.drawButton(true);
      currentPage = 0;
      HomeScreen();
    }
  }
}
#endif

You don't use the serial monitor. It is the first and simplest way to actually "see" what things "look like".

Add copious printing of key variables to see if the values are plausible and properly inform the flow through your code.

That is the first thing I would do if I was in the lab now.

a7