Push Button TFT touch screen

Hi,
I need help about push button function on touch screen..

With this sketch if I am touching the screen and holding it, pin 13 is going high then low then high then low then high... Etc and will be low after i released the screen.

My goal is as long as the screen is touching and holding pin 13 should stay in high. Not high then low then high then low... Etc... The pin 13 should be low after i released the screen.

Here is the sketch:

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop()
{
  readingTouch();
  oneTime();
}

void readingTouch()
{
 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;
}


void oneTime()
{

    if((X > 50 && X < 130) && (Y > 130 && Y < 190) && (Z > MINPRESSURE && Z < MAXPRESSURE) && a ==0 )
    {    
    tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
    tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,BLUE);
    tft.setCursor(curs_1x_x,curs_1x_y);
    tft.setTextSize(txt_btn_sz); 
    tft.setTextColor(YELLOW);
    tft.println("1X");
    Serial.print("\tPressure = "); Serial.println(Z);
    digitalWrite(13,HIGH);
    a=1;
    delay(150); 
     
    }
    
    else if ((Z < MINPRESSURE || Z > MAXPRESSURE) && (a == 1))
    {  
       
    tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
    tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,YELLOW);
    tft.setCursor(curs_1x_x,curs_1x_y);
    tft.setTextSize(txt_btn_sz); 
    tft.setTextColor(WHITE); 
    tft.println("1X"); 
    Serial.print("\tNoPressure = "); Serial.println(Z);
    digitalWrite(13,LOW);
    a=0;
    delay(150); 
    
    }
     
      
}

The serial print when I am touching and holding the touch screen:

Pressure = 488
	NoPressure = 0
	Pressure = 215
	NoPressure = 0
	Pressure = 201
	NoPressure = 0
	Pressure = 196
	NoPressure = 0
	Pressure = 184
	NoPressure = 0
	Pressure = 175
	NoPressure = 0
	Pressure = 200
	NoPressure = 0

When you detect that the screen is touched, because the x location is in range, the y location is in range, and the pressure is high enough, you print the first message, turn the LED on, and set a to 1.

The next time through loop(), you call the oneTime() function (one can only guess at what the function does) which determines that the location is still OK, but that a is not 0, so the else case is evaluated. In the else test, you determine that Z is still good (pressing is still happening) and you see that a is 1, so you turn the LED off, and print a message, and set a to 1.

So, the code is doing exactly what you describe.

You want to test that the screen is being touched, in a function, and return true or false.

You want, in another function, to determine if the screen is now, but was not then, being touched. If the state has changed (there is a state change detection example that you can look at), you determine which change happened - the screen was being touched and no longer is or the screen is now being touched but wasn't before.

If no change happened, there is nothing to do. If a change did happen, then you decide what to do about it.

PaulS:
If no change happened, there is nothing to do. If a change did happen, then you decide what to do about it.

I tried with "else" but I got the screen and led at pin 13 blinking. How to stop the "else" statement from looping?

How to stop the "else" statement from looping?

Simple. else statements do not loop. You don't have to do anything.

If you changed your code, post the new code. If not, you're wasting our time.

PaulS:
Simple. else statements do not loop. You don't have to do anything.

If you changed your code, post the new code. If not, you're wasting our time.

I tried both:

void setup(void) { 
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  tft.reset();
  tft.begin(0x9341);
  tft.setRotation(3); 
  tft.fillScreen(BLACK);
//////////////////////////////////////////////
 tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
 tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,YELLOW);
 tft.setCursor(curs_1x_x,curs_1x_y);
 tft.setTextSize(txt_btn_sz); 
 tft.setTextColor(WHITE); 
 tft.println("1X"); 
 Serial.begin(9600);
 
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop()
{
  readingTouch();
  oneTime();
}

void readingTouch()
{
 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;
}


void oneTime()
{

    if((X > 50 && X < 130) && (Y > 130 && Y < 190) && (Z > MINPRESSURE && Z < MAXPRESSURE))
    {    
    tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
    tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,BLUE);
    tft.setCursor(curs_1x_x,curs_1x_y);
    tft.setTextSize(txt_btn_sz); 
    tft.setTextColor(YELLOW);
    tft.println("1X");
    Serial.print("\tPressure = "); Serial.println(Z);
    digitalWrite(13,HIGH);
    delay(150); 
     
    }
    
    else 
    {  
       
    tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
    tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,YELLOW);
    tft.setCursor(curs_1x_x,curs_1x_y);
    tft.setTextSize(txt_btn_sz); 
    tft.setTextColor(WHITE); 
    tft.println("1X"); 
    Serial.print("\tNoPressure = "); Serial.println(Z);
    digitalWrite(13,LOW);
    delay(150); 
    
    }
     
      
}

and:

void setup(void) { 
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  tft.reset();
  tft.begin(0x9341);
  tft.setRotation(3); 
  tft.fillScreen(BLACK);
//////////////////////////////////////////////
 tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
 tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,YELLOW);
 tft.setCursor(curs_1x_x,curs_1x_y);
 tft.setTextSize(txt_btn_sz); 
 tft.setTextColor(WHITE); 
 tft.println("1X"); 
 Serial.begin(9600);
 
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop()
{
  readingTouch();
  oneTime();
}

void readingTouch()
{
 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;
}


void oneTime()
{

    if((X > 50 && X < 130) && (Y > 130 && Y < 190) && (Z > MINPRESSURE && Z < MAXPRESSURE) && a ==0 )
    {    
    tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
    tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,BLUE);
    tft.setCursor(curs_1x_x,curs_1x_y);
    tft.setTextSize(txt_btn_sz); 
    tft.setTextColor(YELLOW);
    tft.println("1X");
    Serial.print("\tPressure = "); Serial.println(Z);
    digitalWrite(13,HIGH);
    a=1;
    delay(150); 
     
    }
    
    else 
    {  
       
    tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
    tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,YELLOW);
    tft.setCursor(curs_1x_x,curs_1x_y);
    tft.setTextSize(txt_btn_sz); 
    tft.setTextColor(WHITE); 
    tft.println("1X"); 
    Serial.print("\tNoPressure = "); Serial.println(Z);
    digitalWrite(13,LOW);
    a=0;
    delay(150); 
    
    }
     
      
}

The screen is not touched. Here is the Serial print:

 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0
 NoPressure = 0

I am getting PIN 13 working like what I want but the screen ("1X" button) always blinking

Here is the latest sketch:

void setup(void) { 
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  tft.reset();
  tft.begin(0x9341);
  tft.setRotation(3); 
  tft.fillScreen(BLACK);
//////////////////////////////////////////////
 tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
 tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,YELLOW);
 tft.setCursor(curs_1x_x,curs_1x_y);
 tft.setTextSize(txt_btn_sz); 
 tft.setTextColor(WHITE); 
 tft.println("1X");
 //Serial.begin(9600);
 
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop()
{
  readingTouch();
  oneTime();
}

void readingTouch()
{
 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;
}


void oneTime()
{

    if((X > 50 && X < 130) && (Y > 130 && Y < 190))
    {  
      if((Z > MINPRESSURE && Z < MAXPRESSURE)) 
      {
      tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
      tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,BLUE);
      tft.setCursor(curs_1x_x,curs_1x_y);
      tft.setTextSize(txt_btn_sz);
      tft.setTextColor(YELLOW);
      tft.println("1X");
      //Serial.print("\tPressure = "); Serial.println(Z);
      digitalWrite(13,HIGH);
      delay(150);
     
      }  
    }
    else
      {   
      tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
      tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,YELLOW);
      tft.setCursor(curs_1x_x,curs_1x_y);
      tft.setTextSize(txt_btn_sz);
      tft.setTextColor(WHITE);
      tft.println("1X");
      //Serial.print("\tNoPressure = "); Serial.println(Z);
      digitalWrite(13,LOW);
   
      delay(150);
      }
     
     
}

How to make the button on the screen ("1X" button) not blinking?

Where are you determining that the screen is touched now and was not touched last time, or is not touched now but was last time? Look at the state change detection example again.

PaulS:
Where are you determining that the screen is touched now and was not touched last time, or is not touched now but was last time? Look at the state change detection example again.

Now with this sketch the "1X" button is not blinking when the button is pressed but always blinking if the screen is not touched

boolean state = false;
boolean laststate = false;

void setup(void) { 
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  tft.reset();
  tft.begin(0x9341);
  tft.setRotation(3); 
  tft.fillScreen(BLACK);
//////////////////////////////////////////////
 tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
 tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,YELLOW);
 tft.setCursor(curs_1x_x,curs_1x_y);
 tft.setTextSize(txt_btn_sz); 
 tft.setTextColor(WHITE); 
 tft.println("1X");
 Serial.begin(9600);
 
}

#define MINPRESSURE 10
#define MAXPRESSURE 1000

void loop()
{
  readingTouch();
  oneTime();
}

void readingTouch()
{
 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;
}


void oneTime()
{

    if((X > 50 && X < 130) && (Y > 130 && Y < 190))
    {  
      if((Z > MINPRESSURE && Z < MAXPRESSURE) && state == false ) 
      {
      tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
      tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,BLUE);
      tft.setCursor(curs_1x_x,curs_1x_y);
      tft.setTextSize(txt_btn_sz);
      tft.setTextColor(YELLOW);
      tft.println("1X");
      Serial.print("\tPressure = "); Serial.println(Z);
      digitalWrite(13,HIGH);
      delay(150);
      state=true;
      }
       
    }
    else
      {   
      tft.fillRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect, RED);
      tft.drawRoundRect(rect_1x_x, rect_1x_y, width_rect,height_rect,round_rect,YELLOW);
      tft.setCursor(curs_1x_x,curs_1x_y);
      tft.setTextSize(txt_btn_sz);
      tft.setTextColor(WHITE);
      tft.println("1X");
      Serial.print("\tNoPressure = "); Serial.println(Z);
      
      digitalWrite(13,LOW);
   
      delay(150);
      state=false;
      }  
}

Bummer. You still are not detecting a change in state.