I attached 2 wires to Touch-Inputs T0 and T3. The touch of T0 shall decrease a value, the touch of T3 shall increase a value. Sound simple! But: Every time I touch one wire and then the other, an interrupt is executed for both (not only for the one that has been touched!).
If I touch one wire multiple times (with defined delay of minimum 500ms), it works properly. E.g. for T3 the value is increased each time I touch the wire for T3. Same for T0 (decreases value).
The code seems to be correct. I also tested other Touch-Pins as well as different ESP32 DEVKIT V1 boards. The behaviour every time is the same. I used Arduino IDE and Visual Studio Code (with necessary Plugins...) to test it.
Code: Select all
int threshold = 60;
bool touchdetected0 = false;
bool touchdetected3 = false;
int value = 0;
volatile unsigned long sinceLastTouch0 = 0;
volatile unsigned long sinceLastTouch3 = 0;
void gotTouch0()
{
if (millis() - sinceLastTouch0 < 500) return;
sinceLastTouch0 = millis();
//Serial.println("T3 to true");
touchdetected0 = true;
}
void gotTouch3()
{
if (millis() - sinceLastTouch3 < 500) return;
sinceLastTouch3 = millis();
//Serial.println("T3 to true");
touchdetected3 = true;
}
void setup()
{
Serial.begin(115200);
delay(1000);
touchAttachInterrupt(T0, gotTouch0, threshold); // T0 = GPIO 4
touchAttachInterrupt(T3, gotTouch3, threshold); // T3 = GPIO 15
Serial.println("Ready... ");
}
void loop()
{
if(touchdetected0)
{
//Serial.println("T0 to false");
touchdetected0 = false;
Serial.print("Touch 0 detected - Value: "); Serial.println(value=value-1);
}
if(touchdetected3)
{
//Serial.println("T3 to false");
touchdetected3 = false;
Serial.print("Touch 3 detected - Value: "); Serial.println(value=value+1);
}
}