toughtscreen

Hello everyone ;

I am struggling with the touchscreen.
What i want is easy;
i want pin2 to go HIGH for 1 sec and then LOW for 11 sec after i push start,
as i push the icons below the screen go to setup-screen where i can change the 11 sec push again the icons below save,
and when i push stop it needs to stop.
Easy i thought but i find out it is not.
i tried different methodes but cant seem to do it right.
every time the p.z doesn’t seem to react it does the first and i get the setup-screen to work oke but if i lets it run it stops when it want.
What is wrong?
Why do i read p.x and p.y but no p.z ?
In a other sequens the p.z is always there .

#include <Adafruit_GFX.h> // Core graphics library

#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft; // hard-wired for UNO shields anyway.
#include <TouchScreen.h>

#if defined(SAM3X8E)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif

uint8_t YP = A1; // must be an analog pin, use “An” notation!
uint8_t XM = A2; // must be an analog pin, use “An” notation!
uint8_t YM = 7; // can be a digital pin
uint8_t XP = 6; // can be a digital pin
uint8_t Landscape = 0;

uint16_t TS_LEFT = 920;
uint16_t TS_RT = 150;
uint16_t TS_TOP = 940;
uint16_t TS_BOT = 120;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
#define LCD_RST A4
#define LCD_CS A3
#define LCD_RS A2
#define LCD_WR A1
#define LCD_RD A0

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
int16_t BOXSIZE2;
int16_t BOXSIZE;
int16_t PENRADIUS = 3;
int16_t start = 0;
int16_t starten;
uint16_t oldcolor, currentcolor;

int vullen;
int w ;
int h ;
int s;
int save ;
int i = 0 ;
int b=0;
int halt = 0;
//-----------------------------------------------------------------
void setup(void)
{
pinMode (2,OUTPUT);//13
digitalWrite(2,LOW);
Serial.begin(9600);
Serial.println(F(“MakkeLeon !”));

tft.reset();
uint16_t identifier = 0x9341;
Serial.println(F(“Found ILI9341 LCD driver BLUE”));
TS_LEFT = 150; TS_RT = 920; TS_TOP = 940; TS_BOT = 130;

ts = TouchScreen(XP, YP, XM, YM,300); //call the constructor AGAIN with new values.
tft.begin(identifier);

BOXSIZE = tft.width() / 6;
tft.fillScreen(BLACK);
BOXSIZE2 = tft.height();
h=11;

#define MINPRESSURE 100
#define MAXPRESSURE 2000
}

void loop()
{

w = h * 1000;
s = w/1000;

if (start <1){

tft.fillScreen(BLACK);
tft.fillRect(BOXSIZE * 2, BOXSIZE *1.5, BOXSIZE *2, BOXSIZE 2, GREEN);
tft.setTextSize(2);
tft.setTextColor(YELLOW);
tft.setCursor(BOXSIZE 2+10, BOXSIZE2+10);
tft.println(“START”);
tft.setTextSize(3);
tft.setTextColor(CYAN);
tft.setCursor(30, BOXSIZE
4+10);
tft.println(“Hallo PIET”); //writes the text “button 1 pressed”
tft.setTextColor(RED);
tft.println(""); //writes the text “button 1 pressed”
tft.setTextSize(2);
tft.println(" Druk op start"); //writes the text “button 1 pressed”
tft.println(" om de kachel “); //writes the text “button 1 pressed”
tft.println(” te starten"); //writes the text “button 1 pressed”
start=start+1;
delay(1000);

}

int vullen;
int tmp;
int l = 0;

TSPoint p = ts.getPoint();

pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);

if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
Serial.print(“min”);
Serial.print("X = "); Serial.print(p.x);
Serial.print("tY = "); Serial.print(p.y);
Serial.print("tPressure = "); Serial.println(p.z);
if (Landscape) { // swap X and Y
tmp = p.y;
p.y = p.x;
p.x = tmp;
}
starten:
l=l+1;

p.x = map(p.x, TS_LEFT, TS_RT,0, tft.width());
p.y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
Serial.print("X = p.x = "); Serial.print(p.x);
Serial.print("tY = p.y = "); Serial.print(p.y);
Serial.print("tPressure = "); Serial.println(p.z);

if (p.y > BOXSIZE *1.5 && p.y < BOXSIZE *3.5 && halt == 0 && i == 0){

if (p.x> BOXSIZE *2 && p.x < BOXSIZE *4 && halt == 0 && i == 0) {

tft.fillScreen(BLACK); //Set’s LCD Back Ground as Black
tft.fillRect(BOXSIZE * 2, BOXSIZE * 5, BOXSIZE *2, BOXSIZE *2, RED);
tft.setTextSize(2);
tft.setTextColor(YELLOW);
tft.setCursor(BOXSIZE 2+15, BOXSIZE6-10);
tft.println(“STOP”);
start = start+1;
halt = 1;
Serial.print(start);

}
}

if (p.y > BOXSIZE *5 && p.y < BOXSIZE *7 && halt == 1 && i == 0){
oldcolor = currentcolor;

if (p.x> BOXSIZE *2 && p.x < BOXSIZE *4 && halt == 1 && i == 0) {

tft.setTextSize(3);
tft.setTextColor(RED);
tft.setCursor(60, BOXSIZE*2+10);
tft.println(“STOPPEN”);
delay(1000);
start=0;
halt=0;
}

}

if (p.y > BOXSIZE2+10 && i == 0) {

Serial.println(“erase”);
// press the bottom of the screen to erase
tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
tft.fillRect(BOXSIZE * 2, BOXSIZE *1.5, BOXSIZE *2, BOXSIZE *2, GREEN);
tft.setTextColor(YELLOW);
tft.setTextSize(4);
tft.setCursor(BOXSIZE 2+16, BOXSIZE2+8);
tft.println( s);
tft.fillRect(BOXSIZE * 0.5, BOXSIZE *5, BOXSIZE *2, BOXSIZE *2, GREEN);
tft.fillRect(BOXSIZE * 3.5, BOXSIZE *5, BOXSIZE *2, BOXSIZE *2, GREEN);
tft.setTextColor(YELLOW);
tft.setCursor(BOXSIZE 1.30, BOXSIZE6-8);
tft.println("-");
tft.setCursor(BOXSIZE 4.30, BOXSIZE6-8);
tft.println("+");
i = 1;
delay(2000);

}
if (p.y > BOXSIZE5 && p.y < BOXSIZE7 && p.x > BOXSIZE * 0.5 && p.x < BOXSIZE* 3 && i == 1 ){
Serial.println(“min”);
// press the bottom of the screen to erase
// tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
h =s-1;
tft.setCursor(BOXSIZE * 2+16, BOXSIZE * 2+8);
tft.fillRect(BOXSIZE * 2, BOXSIZE *1.5, BOXSIZE *2, BOXSIZE *2, CYAN);
tft.setTextSize(4);
tft.println( h);
Serial.println(s);
Serial.println(h);
delay(1000);
// p.y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
} else {
if (p.y > BOXSIZE * 5 && p.y < BOXSIZE * 7 && p.x > BOXSIZE * 4 && p.x < BOXSIZE * 6 && i == 1){
Serial.println(“plus”);
// press the bottom of the screen to erase
// tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, BLACK);
h =s+1;
tft.setCursor(BOXSIZE * 2 + 16, BOXSIZE * 2 + 8);
tft.fillRect(BOXSIZE * 2, BOXSIZE *1.5, BOXSIZE *2, BOXSIZE *2, CYAN);
tft.setTextSize(4);
tft.println( h);
Serial.println(s);
Serial.println(h);
delay(1000);
// p.y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
}

Serial.println(p.z);
delay(1000);
if (p.y > BOXSIZE2+10 && i == 1) {
start=0;
halt=0;
i=0;
save;
Serial.println(“save”);
delay(1000);

}
}
if(halt == 1){
pinMode (2,OUTPUT);//13

digitalWrite(2,HIGH);
delay(2000);
digitalWrite(2,LOW);
delay(w);

b=b+1;

Serial.print("b = “); Serial.print(b);
Serial.print(” druk "); Serial.println(p.z);
Serial.print("X = "); Serial.print(p.x);
Serial.print("tY = "); Serial.print(p.y);
Serial.print("tPressure = "); Serial.println(p.z);
// p.y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
goto starten;
}

}

I already make a program like this with a lcd 16x2 12c and real buttons that was no problem and works fine.
It seems like a nice idea to make it with a touchscreen but it is a struggle.
help please !
good day

Hi
not sure if this helps, according to some information, pins 2 & 3 are not used by the screen.
I found this not to be the case with pin 3 and I think it is the same with pin 2 they are use during boot up & screen refresh

I used A5 as a digital pin (19) to control a transistor !
See

Terry

All of the UNO shields have the pins clearly printed. e.g. digital#2 pin is used for LCD_D2.

If you are not using the microSD, you have digital #10, 11, 12, 13 available.
You always have Analog#5 available.

Please use CODE tags when posting a sketch.

I tried to build it but it seems incredibly complicated. I really did not understand what you are trying to do.

Please look at this example:

#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>        //.kbv ? missing isTouching() method

//Y-plus, Y-minus, X-plus, X-minus
#define YP A1 //A3  //A2  //A3  // A1
#define XM A2 //A2  //A1  //A2  // A2
#define YM 7  //9   //6   //9   // 7
#define XP 6  //8   //7   //8   // 6
//              teee  kbv   teee   default Touch wiring
#define TS_LEFT 900 //200 //920
#define TS_RT   139 //920 //200
#define TS_TOP  944 //120 //940
#define TS_BOT  140 //940 //120

//Color definitions
#define BLACK       0x0000
#define NAVY        0x000F
#define DARKGREEN   0x03E0
#define DARKCYAN    0x03EF
#define MAROON      0x7800
#define PURPLE      0x780F
#define OLIVE       0x7BE0
#define LIGHTGREY   0xC618
#define DARKGREY    0x7BEF
#define BLUE        0x001F
#define GREEN       0x07E0
#define CYAN        0x07FF
#define RED         0xF800
#define MAGENTA     0xF81F
#define YELLOW      0xFFE0
#define WHITE       0xFFFF
#define ORANGE      0xFD20
#define GREENYELLOW 0xAFE5
#define PINK        0xF81F

MCUFRIEND_kbv tft;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button btns[12];
char legends[] = "123456789*0#";

int Xposn, Yposn, tftpos, tftwid, tftht; //.kbv use intuitive names for globals

void setup()
{
    int x, y, i;
    char msg[2] = "X";          //initialises msg[1] = 0 (NUL)
    tft.begin(tft.readID());    //use the correct controller
    tft.setRotation(0);         //Portrait
    tftwid = tft.width();
    tftht = tft.height();
    tft.fillScreen(BLACK);
    // note that Button x,y is the CENTRE and not the TLHC.
    for (y = 0, i = 0; y < 4; y++) {
        for (x = 0; x < 3; x++) {
            msg[0] = legends[i];
            btns[i].initButton(&tft, 56 + x * 64, 90 + y * 64, 60, 60,
                               WHITE, CYAN, BLACK, msg, 4);
            btns[i++].drawButton(false);
        }
    }
}

bool getPress()
{
    TSPoint p = ts.getPoint();  //point on touchscreen that has been touched
    pinMode(YP, OUTPUT);        //.kbv these pins are shared with TFT
    pinMode(XM, OUTPUT);        //.kbv these pins are shared with TFT
    if (p.z > 10 && p.z < 1000) {
        //map TSPoint to 240 X 320 pixels
        Xposn = map(p.x, TS_LEFT, TS_RT, 0, tftwid); //.kbv makes sense to me
        Yposn = map(p.y, TS_TOP, TS_BOT, 0, tftht);
        return true;
    }
    return false;
}

void loop()
{
    static int old = 0;
    Adafruit_GFX_Button *b;               //pointer to the real button
    if (getPress()) {
        for (int i = 0 ; i < 12; i++) {   //check all buttons
            b = &btns[i];
            b->press(b->contains(Xposn, Yposn));
            b->drawButton(b->isPressed()); //update current state
            if (b->justPressed()) {
                if (tftpos >= tftwid - 12) { //last printable posn
                    tft.fillRect(0, 0, tftwid, 16, BLACK); //erase
                    tftpos = 0;           //and start again
                }
                tft.setCursor(tftpos, 0); //reset text posn, color, size
                tft.setTextSize(2);
                tft.setTextColor(YELLOW);
                tft.print(legends[i]);
                tftpos += 12;             // 2x System font width
                old = i;
            }
        }
    }
    else {
        b = &btns[old];                   //last button is released
        b->press(false);
        if (b->justReleased()) b->drawButton(false);
    }
}

You will probably need to alter the Touch pins and LEFT and RT values to suit your shield.

David.