need help with touch GPIO

am.steen
Posts: 2
Joined: Sun May 12, 2019 10:54 am

need help with touch GPIO

Postby am.steen » Sun May 12, 2019 11:12 am

I write a code for two touch GPIO T0 and T3 to turn on leds at GPIO 22,23
this is my code

Code: Select all

int threshold = 50;
bool touch1detected = false;
bool touch2detected = false;
void gotTouch(){
 touch1detected = true;
}

void gotTouch1(){
 touch2detected = true;
}

void setup() {
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  printf("\n ESP32 Touch Interrupt Test\n");
  touchAttachInterrupt(T0, gotTouch, threshold);
  touchAttachInterrupt(T3, gotTouch1, threshold);
  pinMode (22, OUTPUT);
  pinMode (23, OUTPUT);
}

void loop(){
  if(touch1detected){
    touch1detected = false;
    Serial.println("Touch 0 ON");
    digitalWrite(22, HIGH);
    delay(1000);
  }
    else{
      touch1detected = true;
digitalWrite(22, LOW);
Serial.println("Touch 0 OFF");
    } 
 
  
  if(touch2detected){
    touch2detected = false;
    Serial.println("Touch 3 ON");
    digitalWrite(23, HIGH);
    delay(1000);
  }
    else{
digitalWrite(23, LOW);
Serial.println("Touch 3 OFF");
    
}

}
and it works but I need another thing :-
1. when touch T0 it turns led at GPIO22 on
2. when touch T0 again it turns led at GPIO22 off
3. when touch T3 it turns led at GPIO23 on
4. when touch T3 again it turns led at GPIO23 off

and so on loop

please help

username
Posts: 477
Joined: Thu May 03, 2018 1:18 pm

Re: need help with touch GPIO

Postby username » Mon May 13, 2019 4:40 am

See if this works for you

Code: Select all

int threshold = 50;
bool touch1detected = false;
bool touch2detected = false;
bool touch1state = false;
bool touch2state = false;

void gotTouch()
{
 touch1detected = true;
}

void gotTouch1()
{
 touch2detected = true;
}

void setup() 
{
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  printf("\n ESP32 Touch Interrupt Test\n");
  touchAttachInterrupt(T0, gotTouch, threshold);
  touchAttachInterrupt(T3, gotTouch1, threshold);
  pinMode (22, OUTPUT);
  pinMode (23, OUTPUT);
}

void loop()
{
    if(touch1detected)
    {
        if(touch1state == false)
        {
            touch1state = true;
            Serial.println("Touch 0 ON");
            digitalWrite(22, HIGH);
        }
        else
        {
            touch1state = false;
            digitalWrite(22, LOW);
            Serial.println("Touch 0 OFF");
        }

        // Stay here until there is not more touch detected 
        // So we dont re-trigger
        do
        {
            touch1detected = false;
            delay(250);
        }while(touch1detected);

    } 


    if(touch2detected)
    {
        if(touch2state == false)
        {
            touch2state = true;
            Serial.println("Touch 3 ON");
            digitalWrite(23, HIGH);
        }
        else
        {
            touch2state = false;
            digitalWrite(23, LOW);
            Serial.println("Touch 3 OFF");
        }

        // Stay here until there is not more touch detected 
        // So we dont re-trigger
        do
        {
            touch2detected = false;
            delay(250);
        }while(touch2detected);

    } 


}

am.steen
Posts: 2
Joined: Sun May 12, 2019 10:54 am

Re: need help with touch GPIO

Postby am.steen » Mon May 13, 2019 8:53 am

Yes it works

Thank you very much

Who is online

Users browsing this forum: No registered users and 126 guests