Hello everyone,
I'm Trying to setup 8 touch pins on the esp32 for a keypad I'm trying to build. The idea being when a button is pressed (touched) , A message to be sent out, and when released another message gets sent.
I'm having a bit of trouble figuring out how to accomplish this. with my current code, when a button is pressed, I get continuous messages for press/release until I let go.
here is my code as it is (I'm only focusing on the 1st touch pin at the moment):
/*
This is un example howto use Touch Intrrerupts
The bigger the threshold, the more sensible is the touch
*/
int threshold = 30;
bool touch1detected = false;
bool touch2detected = false;
bool touch3detected = false;
bool touch4detected = false;
bool touch5detected = false;
bool touch6detected = false;
bool touch7detected = false;
bool touch8detected = false;
bool TP1State = false;
bool TP1LastState =false;
void gotTouch1(){
touch1detected = true;
}
void gotTouch2(){
touch2detected = true;
}
void gotTouch3(){
touch3detected = true;
}
void gotTouch4(){
touch4detected = true;
}
void gotTouch5(){
touch5detected = true;
}
void gotTouch6(){
touch6detected = true;
}
void gotTouch7(){
touch7detected = true;
}
void gotTouch8(){
touch8detected = true;
}
void setup() {
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
Serial.println("ESP32 Touch Interrupt Test");
touchAttachInterrupt(T0, gotTouch1, threshold);
touchAttachInterrupt(T3, gotTouch2, threshold);
touchAttachInterrupt(T4, gotTouch3, threshold);
touchAttachInterrupt(T5, gotTouch4, threshold);
touchAttachInterrupt(T6, gotTouch5, threshold);
touchAttachInterrupt(T7, gotTouch6, threshold);
touchAttachInterrupt(T8, gotTouch7, threshold);
touchAttachInterrupt(T9, gotTouch8, threshold);
}
void loop(){
/////////// need help with this section
if(touch1detected) TP1State = true;
else if(!touch1detected)TP1State = false;
if(TP1State && !TP1LastState)Serial.println("Touch 1 Pressed"); //this message should only be sent once when pin is touched
if(!TP1State && TP1LastState)Serial.println("Touch 1 Released"); // this message should only be sent once after touch is removed
TP1LastState = TP1State;
////////////////////////////////////////////////////////////////////
if(touch2detected){
touch2detected = false;
Serial.println("Touch 2 detected");
}
if(touch3detected){
touch3detected = false;
Serial.println("Touch 3 detected");
}
if(touch4detected){
touch4detected = false;
Serial.println("Touch 4 detected");
}
if(touch5detected){
touch5detected = false;
Serial.println("Touch 5 detected");
}
if(touch6detected){
touch6detected = false;
Serial.println("Touch 6 detected");
}
if(touch7detected){
touch7detected = false;
Serial.println("Touch 7 detected");
}
if(touch8detected){
touch8detected = false;
Serial.println("Touch 8 detected");
}
}