Go Down

Topic: tft buton presses issues (Read 54 times) previous topic - next topic

E-CarNut

Hello all, having trouble with button presses on my touchscreen.   I am trying to make a tester for turn signals.  I am trying to make the button stay on when pressed and then go out when pressed again.  What happens is that it works only once and then gets stuck on the last button press and doesn't reset.
Thanks.


  if (p.y > 56 && p.y<73 &&p.x>52 && p.x<72){
      if (ltTurn ==HIGH){
     ltAction2();
     
      }
     
     // delay(250);
      if (ltTurn ==LOW){
        ltTurn=1;
        ltAction();
      }
     
   
    }


   
    }

    void ltAction(){
    tft.setCursor(70,80);
    tft.setTextSize(3);
    tft.setTextColor(ORANGE);
    tft.print("left turn");
    Serial.print("left turn on");
    Serial.println();
    delay(500);
    }
    void ltAction2(){
     tft.setCursor(70,80);
    tft.setTextSize(3);
    tft.setTextColor(WHITE);
    tft.print("left turn");
    Serial.print("left turn off");
   
    Serial.println();
   
    delay(500);

HazardsMind

You need a boolean variable, call it latch or whatever you want it to be and a state check. 
If the button is pressed and does not equal its last state then check to see if the button is HIGH. If both of these conditions are true, latch = !latch;

make this a function that accepts 4 ints and returns true if pressed.
if (p.y > 56 && p.y<73 &&p.x>52 && p.x<72)

Next use that returned value in your if statement.

bool myButton = button(52, 56, 72, 73); // x1, y1, x2, y2


if( myButton != laststate) // if they are different then proceed
{
  if(myButton == true) // if the button is pressed
  {
    latch = !latch; // toggle 1 ->0 or 0 -> 1
  }
  laststate = myButton;  // update last state
}

if(latch == true)
  // do something
else
  // do something else
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

Go Up