relay switching with an tft touchscreen

hello
i need help with this sketch

i work with the uno
2,8 tft with touchscreen
and an 4 relays board

al of this works with this sketch but the relay is not stabil in his contact i want to control the relay with the touch as an pushbutton when i push relay on and off as i the screen not touch

#include <Adafruit_GFX.h> 
#include <Adafruit_TFTLCD.h>  
#include <TouchScreen.h>    
#define YP A1 
#define XM A2 
#define YM 7 
#define XP 6 
#define MINPRESSURE 1
#define MAXPRESSURE 1000

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

short TS_MINX = 150; 
short TS_MINY = 120; 
short TS_MAXX = 850;
short TS_MAXY = 891; 

#define LCD_CS A3   
#define LCD_CD A2   
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define	BLACK   0x0000  
#define	BLUE    0x001F  
#define	RED     0xF800  
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); 

int X;
int Y;
int Z;

int a = 0;

#define BOXSIZE 110
#define PENRADIUS 3
int oldcolor, currentcolor;

#define vooromhoog 10



void setup(void) 
{ 
  tft.begin(0x9341);
  
  pinMode(10, OUTPUT);



  tft.fillScreen(BLACK);//BG COLOR


}
void loop()
{          
    lecturaPanel(); // Realizamos lectura del panel para detectar presion y coordenadas
      
    // Si la pulsación del eje X se produce entre los puntos 40 y 160
    // Y la pulsacion del eje Y se produce entre los puntos 20 y 60
    // Y la presión realizada esta entre el margen determinado
    if((X > 20 && X < 400) && (Y > 20 && Y < 60) && (Z > MINPRESSURE && Z < MAXPRESSURE)) 
    {
        digitalWrite(vooromhoog, LOW);
    delay(20);
        digitalWrite(vooromhoog, HIGH);
      }
    }    

void lecturaPanel()
{
    
    TSPoint p = ts.getPoint(); // Realizamos lectura de las coordenadas


  
    pinMode(XM, OUTPUT); // La librería utiliza estos pines como entrada y salida
    pinMode(YP, OUTPUT); // por lo que es necesario declararlos como salida justo
                         // despues de realizar una lectura de coordenadas.    
  
    // Mapeamos los valores analogicos leidos del panel tactil (0-1023)
    // y los convertimos en valor correspondiente a la medida del LCD 320x240
    X = map(p.x, TS_MAXX, TS_MINX, tft.width(), 0);
    Y = map(p.y, TS_MAXY, TS_MINY, tft.height(), 0);
    Z = p.z;
}

im from the netherlands

but the relay is not stabil in his contact

What does this mean? Does the relay turn on when it shouldn’t? Does the relay turn off when it shouldn’t?

You realize, I hope, that reading a touch screen is not going to be fast.

    if((X > 20 && X < 400) && (Y > 20 && Y < 60) && (Z > MINPRESSURE && Z < MAXPRESSURE))
    {
        digitalWrite(vooromhoog, LOW);
    delay(20);
        digitalWrite(vooromhoog, HIGH);
      }
    }

Does it matter where you touch, if you are not touching? You should have an outer if statement that determines if the screen IS being touched. You should have an inner if statement that determines where the screen is being touched.

hi the relay is on when i push but the relay is not an stable contact

The relay will then flapping when I press

donnyrijken: hi the relay is on when i push but the relay is not an stable contact

The relay will then flapping when I press

Yes, because you turn it on and off every 20ms in loop().

yes i know but that is now gone in de sketch and he is now not yet good working how can i make it good working here is mij sketch now

#include <Adafruit_GFX.h> 
#include <Adafruit_TFTLCD.h>  
#include <TouchScreen.h>    
#define YP A1 
#define XM A2 
#define YM 7 
#define XP 6 
#define MINPRESSURE 1
#define MAXPRESSURE 1000

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

short TS_MINX = 150; 
short TS_MINY = 120; 
short TS_MAXX = 850;
short TS_MAXY = 891; 

#define LCD_CS A3   
#define LCD_CD A2   
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define	BLACK   0x0000  
#define	BLUE    0x001F  
#define	RED     0xF800  
#define	GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); 

int X;
int Y;
int Z;

int a = 0;

#define BOXSIZE 110
#define PENRADIUS 3
int oldcolor, currentcolor;

#define vooromhoog 10



void setup(void) 
{ 
  tft.begin(0x9341);
  
  pinMode(10, OUTPUT);



  tft.fillScreen(BLACK);//BG COLOR


}
void loop()
{          
    lecturaPanel(); // Realizamos lectura del panel para detectar presion y coordenadas
      
    // Si la pulsación del eje X se produce entre los puntos 40 y 160
    // Y la pulsacion del eje Y se produce entre los puntos 20 y 60
    // Y la presión realizada esta entre el margen determinado
    if((X > 20 && X < 400) && (Y > 20 && Y < 60) && (Z > MINPRESSURE && Z < MAXPRESSURE)) 
    {
        digitalWrite(vooromhoog, LOW);
   
        digitalWrite(vooromhoog, HIGH);
      }
    }    

void lecturaPanel()
{
    
    TSPoint p = ts.getPoint(); // Realizamos lectura de las coordenadas


  
    pinMode(XM, OUTPUT); // La librería utiliza estos pines como entrada y salida
    pinMode(YP, OUTPUT); // por lo que es necesario declararlos como salida justo
                         // despues de realizar una lectura de coordenadas.    
  
    // Mapeamos los valores analogicos leidos del panel tactil (0-1023)
    // y los convertimos en valor correspondiente a la medida del LCD 320x240
    X = map(p.x, TS_MAXX, TS_MINX, tft.width(), 0);
    Y = map(p.y, TS_MAXY, TS_MINY, tft.height(), 0);
    Z = p.z;
}
    if((X > 20 && X < 400) && (Y > 20 && Y < 60) && (Z > MINPRESSURE && Z < MAXPRESSURE))

You are STILL concerned about where the press happens, EVEN WHEN THERE IS NO PRESS. Why?

Why are you banging the pin off and then right back on?

donnyrijken: yes i know but that is now gone in de sketch

You took out the 20ms delay. Now you are "flapping" the relay every several microseconds. That is not a fix. You need to detect when the TFT becomes pressed, not when it is pressed.

... and your relays may "self-flap" if your 4 relays board is stealing power from the Arduino.

PaulS:

    if((X > 20 && X < 400) && (Y > 20 && Y < 60) && (Z > MINPRESSURE && Z < MAXPRESSURE))

You are STILL concerned about where the press happens, EVEN WHEN THERE IS NO PRESS. Why?

Why are you banging the pin off and then right back on?

hi how can i make it correctly

hi how can i make it correctly

if(Z > MINPRESSURE && Z < MAXPRESSURE)
{
   // A press has occurred
   if((X > 20 && X < 400) && (Y > 20 && Y < 60))
   {
      // In the right place
   }
   else
   {
      // In the wrong place
   }
}
else
{
   // No press
}

You then need to put the no-press code in the proper place.

#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#include <TouchScreen.h>
#define YP A1
#define XM A2
#define YM 7
#define XP 6
#define MINPRESSURE 1
#define MAXPRESSURE 1000

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

short TS_MINX = 150;
short TS_MINY = 120;
short TS_MAXX = 850;
short TS_MAXY = 891;

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4

#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

int X;
int Y;
int Z;

int a = 0;

#define BOXSIZE 110
#define PENRADIUS 3
int oldcolor, currentcolor;

#define vooromhoog 10

void setup(void)
{
tft.begin(0x9341);

pinMode(10, OUTPUT);

tft.fillScreen(BLACK);//BG COLOR

}
void loop()
{
lecturaPanel();

if((X > 20 && X < 400) && (Y > 20 && Y < 60) && (Z > MINPRESSURE && Z < MAXPRESSURE))
{

digitalWrite(vooromhoog, HIGH);
}
}

void lecturaPanel()
{

TSPoint p = ts.getPoint();

pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
X = map(p.x, TS_MAXX, TS_MINX, tft.width(), 0);
Y = map(p.y, TS_MAXY, TS_MINY, tft.height(), 0);
Z = p.z;
}

PaulS:
hi i have try that but than is the relay always on when i press

if(Z > MINPRESSURE && Z < MAXPRESSURE)

{
  // A press has occurred
  if((X > 20 && X < 400) && (Y > 20 && Y < 60))
  {
     // In the right place
  }
  else
  {
     // In the wrong place
  }
}
else
{
  // No press
}




You then need to put the no-press code in the proper place.

You are setting pinMode over and over and over in loop, because it's in lecturaPanel(). Why?

that is a sample that i have used i dont know how i can make it correctly or has somebody a good sample for me