touch screen and programming

hello, for some reasons i am here to request for help.
to start, i have a 3.5" touch screen whhere i am trying to make a pop menu, so the problem is that when i touch the right buton (on the screen) it makes only one step if i want to go back with the left buton nothing happens, i will let you analying the code.

#include <Adafruit_TFTLCD.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>

const int XP=8,XM=A2,YP=A3,YM=9; //ID=0x9341
const int TS_LEFT=954,TS_RT=77,TS_TOP=398,TS_BOT=644;

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

#define BLACK 0x0002
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
int i;
Adafruit_GFX_Button on_btn, off_btn;
char currentPage ,selectedUnit;

boolean buttonEnabled = true;

void setup() {

tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(1);
tft.fillScreen(BLACK);//écran blac

currentPage = '0'; // Indicates that we are at Home Screen
currentPage_0();
}
void loop ()

{
if (currentPage == '0') {

TSPoint p = ts.getPoint();//c'est pour activer le touch

if (p.z > ts.pressureThreshhold) //si on appuie sur le bouton rouge l'écran va nour affichera un message
{

p.x = map(p.x, TS_LEFT, TS_RT, 0, 320);//la fonction map nous donne les valeurs qu'on a utilisé pour calibrer l'écran en pixel
p.y = map(p.y, TS_TOP, TS_BOT, 0, 480);//et définition de la résolution de l'écran tactile

if(p.x>210 && p.x<250 && p.y>420 && p.y<750)//si on appuie sur ces cordonnées on passe à la prochaine page

{ if(currentPage == '0'){

pinMode(XM, OUTPUT);//on définit les pinmodes demandés par la bibliothèque
pinMode(YP, OUTPUT);

currentPage = '1';
currentPage_1();

}
if(p.x>210 && p.x<250 && p.y>420 && p.y<750 && currentPage == '1') //si on appuie sur ces cordonnées on passe à la prochaine page

{ pinMode(XM, OUTPUT);//on définit les pinmodes demandés par la bibliothèque
pinMode(YP, OUTPUT);

currentPage = '2';
currentPage_2();
}
}

if (p.x>210 && p.x<250 && p.y>820 && p.y<1300 && currentPage == '1')//si on appuie sur ces cordonnées on passe à la prochaine page

{
pinMode(XM, OUTPUT);//on définit les pinmodes demandés par la bibliothèque
pinMode(YP, OUTPUT);

currentPage = '0';
currentPage_0();
commaande();

}

if (p.x>210 && p.x<250 && p.y>820 && p.y<1300 && currentPage == '2')//si on appuie sur ces cordonnées on passe à la prochaine page
{
currentPage = '1';

currentPage_1();

commaande();
}

if(p.x>210 && p.x<250 && p.y>420 && p.y<750 && currentPage == '1'){//si on appuie sur ces cordonnées on passe à la prochaine page

currentPage='2';
commaande();

}

}

}
}

void commaande(){
//BOUTON de commande
tft.drawRect(10,220,70,40,GREEN);
tft.setCursor(20,230);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("LEFT");

tft.fillRect(100,220,70,40,CYAN);
tft.drawRect(100,220,70,40,GREEN);
tft.setCursor(110,230);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("RIGHT");

tft.drawRect(55,270,70,40,GREEN);
tft.setCursor(65,280);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("DOWN");

tft.drawRect(55,170,70,40,GREEN);
tft.setCursor(80,180);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("UP");
}

void currentPage_0()
{
commaande();

tft.drawRect(300,70,100,150,BLACK);
tft.setCursor(310,80);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("IDLE");

}
void currentPage_2()
{
tft.fillScreen(BLACK);
commaande();

tft.drawRect(300,70,100,150,BLACK);
tft.setCursor(310,80);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("SETP");}

void currentPage_1(){
tft.fillScreen(BLACK);

tft.drawRect(300,70,100,150,BLACK);
tft.setCursor(310,80);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("Run");
commaande();
delay(0);

}

benwareth.ino (4.36 KB)

In addition to horrible indentation (which you should fix using the IDE's auto-format feature...ctrl+t), you map p.y to range (0,480) but some of your conditionals test p.y for range (820,1300) or (420,750). Map will extrapolate, so it is possible for it to generate numbers in those out-of-bounds ranges, but is that what you expect?

i think it's the only coordinates where i can touch the buton that i created!
what do you suggest?

zaki_HDne:
i think it's the only coordinates where i can touch the buton that i created!

I don't understand your response.

zaki_HDne:
what do you suggest?

Use Serial.print to see if your expectations are correct.

i am not as good as you think at arduino programmation, would you please explain wherei have to put serial.pin? and what it will indicate? thankyou

You should put the Serial.print anywhere in your code you want to verify that a value is in the range you expect it .

Remember to always put a text label to explain what the value is

Serial.print (F("p.x = "));
Serial.println (p.x);

i found that the calibrate was not correct if corrected the following code is :
you were right, the calibration was false! and i updated the code! but still not working!!
this is the new calibration code:
x = left:122 write : 914
y= top: 955 bot:98
xp=8
xm=2
yp=a3
ym=9

the print(f("p.x= "); won't work, nothing is displayed on the monitor.
please help

Please post your code and your problem fully

this is a code of a menu with different pages, the problem is when i click the right buton, it goes on the next page, when i want to go back or go to the next page, it do nothing, it goes next just once, this is my problem that i was trying to resolve 6 days ago.
i hope that you can help me!

here is my code:

#include <Adafruit_TFTLCD.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>

const int XP = 8, XM = A2, YP = A3, YM = 9; //ID=0x9341
const int TS_LEFT = 122, TS_RT = 915, TS_TOP = 955, TS_BOT = 98;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define BLACK 0x0002
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
int i;
Adafruit_GFX_Button on_btn, off_btn;
char currentPage , selectedUnit;

boolean buttonEnabled = true;

void setup() {

tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(1);
tft.fillScreen(BLACK);//écran blac

currentPage = '0'; // Indicates that we are at Home Screen
currentPage_0();

}
void loop ()

{
if (currentPage == '0') {

TSPoint p = ts.getPoint();//c'est pour activer le touch

if (p.z > ts.pressureThreshhold) //si on appuie sur le bouton rouge l'écran va nour affichera un message
{

p.x = map(p.x, TS_LEFT, TS_RT, 0, 320);//la fonction map nous donne les valeurs qu'on a utilisé pour calibrer l'écran en pixel
p.y = map(p.y, TS_TOP, TS_BOT, 0, 480);//et définition de la résolution de l'écran tactile

if (p.x > 20 && p.x < 250 && p.y > 20 && p.y < 100) //si on appuie sur ces cordonnées on passe à la prochaine page

{ if (currentPage == '0') {

pinMode(XM, OUTPUT);//on définit les pinmodes demandés par la bibliothèque
pinMode(YP, OUTPUT);
Serial.print (F("p.x = okeeyy "));
Serial.print(p.x);
currentPage = '1';
currentPage_1();

}
if (p.x > 20 && p.x < 250 && p.y > 20 && p.y < 100 && currentPage == '1') //si on appuie sur ces cordonnées on passe à la prochaine page

{ pinMode(XM, OUTPUT);//on définit les pinmodes demandés par la bibliothèque
pinMode(YP, OUTPUT);

currentPage = '2';
currentPage_2();
}
}

if (p.x > 210 && p.x < 250 && p.y > 220 && p.y < 300 && currentPage == '1') //si on appuie sur ces cordonnées on passe à la prochaine page

{
pinMode(XM, OUTPUT);//on définit les pinmodes demandés par la bibliothèque
pinMode(YP, OUTPUT);

currentPage = '0';
currentPage_0();
commaande();

}

if (p.x > 20 && p.x < 250 && p.y > 220 && p.y < 300 && currentPage == '2') //si on appuie sur ces cordonnées on passe à la prochaine page
{
currentPage = '1';

currentPage_1();

commaande();
}

if (p.x > 210 && p.x < 250 && p.y > 20 && p.y < 100 && currentPage == '1') { //si on appuie sur ces cordonnées on passe à la prochaine page

currentPage = '2';
commaande();

}

}

}
}

void commaande() {
//BOUTON de commande
tft.drawRect(10, 220, 70, 40, GREEN);
tft.setCursor(20, 230);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("LEFT");

tft.fillRect(100, 220, 70, 40, CYAN);
tft.drawRect(100, 220, 70, 40, GREEN);
tft.setCursor(110, 230);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("RIGHT");

tft.drawRect(55, 270, 70, 40, GREEN);
tft.setCursor(65, 280);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("DOWN");

tft.drawRect(55, 170, 70, 40, GREEN);
tft.setCursor(80, 180);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("UP");
}

void currentPage_0()
{
commaande();

tft.drawRect(300, 70, 100, 150, BLACK);
tft.setCursor(310, 80);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("IDLE");

}
void currentPage_2()
{
tft.fillScreen(BLACK);
commaande();

tft.drawRect(300, 70, 100, 150, BLACK);
tft.setCursor(310, 80);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("SETP");
}

void currentPage_1() {
tft.fillScreen(BLACK);

tft.drawRect(300, 70, 100, 150, BLACK);
tft.setCursor(310, 80);
tft.setTextColor(RED);
tft.setTextSize(2);
tft.print("Run");
commaande();
delay(0);

}