UTFT - Touch buttons read on all pages instead of just "home screen"

Hi all

I am very new to arduino (started playing with it since last week)

I am playing around with TFT LCD Touch Screen.

At the moment I have managed to put together a Home screen with 2 touch buttons

One button for photo gallery and second one I will use for RGB ligh controll...

The problem I have now is that when I go to RGB Control Page (void mcled) when I hold the screen at the location where Gallery Button shows on home screen, it takes me to Photo gallery...

I can't figure why this buttons reads on all pages instead of only on Home screen.

I was hoping some of you, more experienced programmers could help me out with my problem.

Here's my entire code, hopefully not too messy for you guys ::slight_smile:

Thank you in advance


#include <UTFT.h> // biblioteka wyswitlacza
#include <UTouch.h> // biblioteka touch wyswitlacza
#include <SPI.h> // biblioteka
#include <SdFat.h> // biblioteka karty sd
#include <UTFT_SdRaw.h> // biblioteka karty sd
#define SD_CHIP_SELECT 53 // declaring pin for SD card
SdFat sd; // reading sd card
UTFT myGLCD(TFT01_70,38,39,40,41); // deklaracja PIN wyswietlacza
UTouch myTouch( 6, 5, 4, 3, 2); // deklaracja PIN touch wyswietlacza
UTFT_SdRaw myFiles(&myGLCD); // declaring files from SD card
extern uint8_t SmallFont[]; // declaring font size
extern uint8_t BigFont[]; // declaring font size
extern uint8_t nadianne[]; // declaring font style
extern uint8_t Grotesk32x64[]; // declaring font style
extern uint8_t Grotesk16x32[]; // declaring font style
int x, y;
char currentPage;
int redLed = 10;
int greenLed = 11;
int blueLed = 12;
int xR=38;
int xG=38;
int xB=38;

//===VOID SET UP==============================

void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);

Serial.begin(9600); //starting connection
bool mysd = 0; // declaring sd
while (!mysd) // reating loop
{
if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED))
{
Serial.println(F("Card failed or not present")); // print on serial monitor
Serial.println(F("Retrying...")); // print on serial monitor
}
else
{
mysd = 1;
Serial.println(F("Card initialised.")); // print on serial monitor
}
}

Serial.println(F("Initialising LCD.")); // print on serial monitor
myGLCD.InitLCD(); // starting the screen
myGLCD.clrScr(); // czysci
myTouch.InitTouch(); // wlacza toucz skreen
myTouch.setPrecision(PREC_MEDIUM); // dokladnosc dotyku

Serial.println(F("Loading Logo"));
myFiles.load(0, 0, 800, 480, "logo.RAW"); // wgrywa zdjecie
delay(500);// pausa

}

//======VOID LOOP==========================

void loop()
{homeScreen();}

//===HOME SCREEN============================

void homeScreen()
{

currentPage = 'homePage';
Serial.println(F("Loading Home Screen"));
myGLCD.clrScr(); //czysci ekran
myFiles.load(0, 0, 800, 480, "background.RAW"); // wgrywa background
myGLCD.setColor(250, 250, 250); // kolor ramki przycisku
myGLCD.drawRoundRect (59, 99, 291, 171 ); //rysuje ramke przycisku
myGLCD.setColor(100, 100, 100); //kolor tla przycisku
myGLCD.fillRoundRect (60, 100, 290, 170); // przycisk -od prawej- -wysokosc- -szerokosc-

myGLCD.setFont(Grotesk16x32); //wielkosc czcionki
myGLCD.setBackColor( VGA_TRANSPARENT ); // kolor tla wyswietlania (przezroczysty)
myGLCD.setColor(255, 127, 0); // kolor trzcionki
myGLCD.print("PHOTO GALLERY" , 70, 120); // Napis w kolorze

myGLCD.setColor(250, 250, 250); // kolor ramki przycisku
myGLCD.drawRoundRect (59, 359, 291, 431 ); //rysuje ramke przycisku
myGLCD.setColor(100, 100, 100); //kolor tla przycisku
myGLCD.fillRoundRect (60, 360, 290, 430); // przycisk -od prawej- -wysokosc- -szerokosc-

myGLCD.setFont(Grotesk16x32); //wielkosc czcionki
myGLCD.setBackColor( VGA_TRANSPARENT ); // kolor tla wyswietlania (przezroczysty)
myGLCD.setColor(255, 127, 0); // kolor trzcionki
myGLCD.print("LIGHTS PLAY" , 85, 380); // Napis w kolorze
{buttons();} // BUTTONS

}

//======BUTTONS==================

void buttons()
{ if (currentPage == 'homeScreen');
if (myTouch.dataAvailable())
{
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();

myGLCD.setBackColor(255, 255, 255); //SCREEN COORDINATES
myGLCD.print(" ", CENTER, 400); myGLCD.printNumI((x), CENTER, 400); //SCREEN COORDINATES
myGLCD.print(" ", CENTER, 450); myGLCD.printNumI((y), CENTER, 450); //SCREEN COORDINATES

if ((x>=507) && (x<=736) &&(y>=295) && (y<=360)){gallery();}
if ((x>=507) && (x<=736) &&(y>=35) && (y<=100)){mcled();}

}
buttons();
}

//=======PHOTOT GALLERY============================

void gallery()

{
myGLCD.clrScr();
Serial.println(F("Loading Gallery"));
while(true)
{
myFiles.load(0, 0, 800, 480, "audi.RAW"); // wgrywa zdjecie
if (myTouch.dataAvailable()){
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x>=1) && (x<=800) &&(y>=1) && (y<=460))
Serial.println(F("Exiting Gallery"));
{homeScreen();}
}
delay(500);

myFiles.load(0, 0, 800, 480, "vw.RAW"); // wgrywa zdjecie
if (myTouch.dataAvailable()){
myTouch.read();
x = myTouch.getX();
y = myTouch.getY();
if ((x>=1) && (x<=800) &&(y>=1) && (y<=460))
Serial.println(F("Exiting Gallery"));
{homeScreen();}
}
delay(500);

}
}

==========LIGHTS PLAY==================

void mcled()
{ currentPage = 'lightsPlay';
myGLCD.clrScr();
myGLCD.setFont(Grotesk16x32); //wielkosc czcionki
myGLCD.setBackColor( VGA_TRANSPARENT ); // kolor tla wyswietlania (przezroczysty)
myGLCD.setColor(255, 127, 0); // kolor trzcionki
myGLCD.print("TEST" , 85, 380); // Napis w kolorze
}