touch screen programming

hello, i am new at arduino programming, the purpose is to make a pop menu.
to start, i have a 3.5" touch screen.
so the problem is that when i touch the right buton (on the touch screen) it makes only one step and if i want to go back with the left buton nothing happens, i would like to creat 6 pages, untill now i created 2 pages because and i stopped because of this problem. i will post the code here.
thankyou for help.

#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);

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);

}

benwareth.ino (4.36 KB)

First off. Press ctrl-T in the Arduino IDE. It will format your code nicely. You can always edit your own posts e.g. Code tags use the </> icon. Just say if you change something important.

Run the Calibration sketch again. Your values for TS_TOP, TS_BOT are unusual. 900 and 100 are typical i.e. a wide difference. You have only 246 difference.

Study the library examples. You will see there is some alternative code that is commented out.
It can be quite simple to maintain different screens with different sets of buttons.

The main lesson is to write neatly formatted code. It helps you design your logic correctly. Ctrl-T is brilliant.

David.

thankyou very much for your advices, i appreciate it, the code now is more organized. i tried to calibrate and this is the result: x left : 90 rt : 815 y top : 772 bot : 388 i can't discover what's the problem, could you give me some corrections please?

90 and 815 is normal. 772 and 388 is unusual.

There is nothing that I can do. If you run the Touchshield_kbv example, the coloured dot should follow the stylus perfectly when the calibration is correct.

David.

you were right, the calibration was false i corrected it! 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

can you help me please!

I presume that your XM = A2 and "xm=2" is a typo. Those figures look normal.

Always copy-paste from real-life e.g. the Terminal in the Calibration program or your actual sketch.

Copy-paste the calibration to the TouchShield_kbv example. Does the coloured dot follow the stylus?

It takes 10 minutes of your life to prepare a proper question. Use known examples. Quote which library version and which example. Explain or paste any "edits" you have made. Say what you expected. Say what you actually got.

You probably get an answer straightaway. The thread does not look very impressive. Problem is solved with a few messages. Often one question with one answer ! If we have to extract blood from stones, the messages can go back and forth for days. Generally taking hours from your life.

Why did you have "unusual" calibration in the first place?

David.

hello, thankyou for your reply, now the coloured dot follow the stylus normally, i think that i don’t have a calibrating problem, i think the problem is in the loop of if(p.x<…{instructions…}), because the program whent it goes in the instructions of if and excute it, it wont goes out, i tried to use goto label; and i placed label: before the instructions that it activate the touch screen ( TSPoint p = ts.getPoint(); ) but nothing i got nothing, any advices other advices please?

Try the button_simple example.

I think there is an example that does different menu screens.

David.

hi,david. i try to run the button_simple example, it was just displaying. touch is still not work. i mean it can not change the color whatever i touch the button. forgiven my poor english.

my tftdisplay is 3.97'mcufriend st7793.and i used arduine due to run it. here is calibration code: But they can be in ANY order e.g. right to left or bottom to top or wrong direction Edit name and calibration statements

Please Calibrate. ID=0x7793 Screen is 240x400 Calibration is: LEFT = 907 RT = 136 TOP = 942 BOT = 139

Wiring is always PORTRAIT YP=55 XM=56 YM=7 XP=6

how can i do to make it work?

I just ran the Calibration sketch example on a Nucleo-F446 board.

This Shield happens to have Touch pins on 6, 7, A1, A2. It is not possible to diagnose the pins on an ARM but it still calibrates normally if you have the correct pins.

You can diagnose the Touch pins with a DMM. Remove Shield. Measure resistance between pins. Common pins are 6, 7, 8, 9, A1, A2, A3. The Y direction has a typical resistance of 500R. The X direction is 300R.

Readers find this process difficult. Especially if they are not English speakers. If you can suggest any improvements, I would be very grateful.

David.

In your sketch you only check for a touch when on page 0, so when you change the page it will stop responding.

You need to check the touch scree on every loop, only draw a new page if the selected page is different to the one already drawn. When a touch is recorded set a flag, later in the loop test for a new touch, perform the action needed, then clear the flag to indicate that touch has been handled.

Add Serial.print messages to your code and a delay at the end of the loop so you can see what is hapening in your code.

You will see your proram flow issue if you mentally step though the program.