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, BOXSIZE4+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