Touch detection on ESP32

Hi
I am trying to experiment with this touch system, where I want my code to detect the combined touch at two touch ports. I am using the following code, the part where I have put conditions of touchdetected4 and touchdetected5 inside the void loop, it does not seem to work. Any suggestions would be greatly appreciated. Individual touch seems to be working fine.

</>
int threshold = 60;
bool touchdetected5 = false;
bool touchdetected4 = false;
const byte led_gpio1 = 32;
const byte led_gpio2 = 33;
const byte led_gpio3 = 36;

int value = 0;
volatile unsigned long sinceLastTouch5 = 0;
volatile unsigned long sinceLastTouch4 = 0;

void gotTouch5(){
if (millis() - sinceLastTouch5 < 500) return;
sinceLastTouch5 = millis();
touchdetected5 = true;
}

void gotTouch4(){
if (millis() - sinceLastTouch4 < 500) return;
sinceLastTouch4 = millis();
touchdetected4 = true;
}

void setup() {
Serial.begin(115200);
pinMode(led_gpio1, OUTPUT);
pinMode(led_gpio2, OUTPUT);
pinMode(led_gpio3, OUTPUT);

delay(1000); // give me time to bring up serial monitor
printf("\n ESP32 Touch Interrupt Test\n");
touchAttachInterrupt(T5, gotTouch5, threshold); // T0 = GPIO 4
touchAttachInterrupt(T4, gotTouch4, threshold); // T3 = GPIO 15
}

void loop(){
if(touchdetected4 && touchdetected5){
touchdetected5 = false;
touchdetected4 = false;
digitalWrite(led_gpio3, HIGH);
delay(150);
digitalWrite(led_gpio3, LOW);
delay(150);
Serial.print("Both Teouched - Value: "); Serial.println(value=value-1);
}
if(touchdetected5){
touchdetected5 = false;
digitalWrite(led_gpio1, HIGH);
delay(150);
digitalWrite(led_gpio1, LOW);
delay(150);
Serial.print("Touch 5 detected - Value: "); Serial.println(value=value-1);
}
if(touchdetected4){
touchdetected4 = false;
digitalWrite(led_gpio2, HIGH);
delay(150);
digitalWrite(led_gpio2, LOW);
delay(150);
Serial.print("Touch 4 detected - Value: "); Serial.println(value=value+1);
}

}
</>

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Whereabouts in the code do you read the values from the touch sensors and where do you test the values against the threshold value ?

Hi rauf_anwar,
the logic of your code is good, but the double condition "touchdetected4 && touchdetected5 " is only true if the keys are pressed at exactly the same time. Otherwise the flag of the first pressed key will be set to zero in the function that concerns this key, and the double condition in "void loop" can never be met.
(pressing 2 touch at the same time with a processor that runs at many many Mhz is difficult !)

hi UKHeliBob
the touchs are under interruption.

Let's help make the code clearer by posting it correctly so that it is easier to read

int threshold = 60;
bool touchdetected5 = false;
bool touchdetected4 = false;
const byte led_gpio1 = 32;
const byte led_gpio2 = 33;
const byte led_gpio3 = 36;

int value = 0;
volatile unsigned long sinceLastTouch5 = 0;
volatile unsigned long sinceLastTouch4 = 0;

void gotTouch5()
{
  if (millis() - sinceLastTouch5 < 500) return;
  sinceLastTouch5 = millis();
  touchdetected5 = true;
}

void gotTouch4()
{
  if (millis() - sinceLastTouch4 < 500) return;
  sinceLastTouch4 = millis();
  touchdetected4 = true;
}

void setup()
{
  Serial.begin(115200);
  pinMode(led_gpio1, OUTPUT);
  pinMode(led_gpio2, OUTPUT);
  pinMode(led_gpio3, OUTPUT);
  delay(1000); // give me time to bring up serial monitor
  printf("\n ESP32 Touch Interrupt Test\n");
  touchAttachInterrupt(T5, gotTouch5, threshold); // T0 = GPIO 4
  touchAttachInterrupt(T4, gotTouch4, threshold); // T3 = GPIO 15
}

void loop()
{
  if (touchdetected4 && touchdetected5)
  {
    touchdetected5 = false;
    touchdetected4 = false;
    digitalWrite(led_gpio3, HIGH);
    delay(150);
    digitalWrite(led_gpio3, LOW);
    delay(150);
    Serial.print("Both Teouched - Value: ");
    Serial.println(value = value - 1);
  }
  if (touchdetected5)
  {
    touchdetected5 = false;
    digitalWrite(led_gpio1, HIGH);
    delay(150);
    digitalWrite(led_gpio1, LOW);
    delay(150);
    Serial.print("Touch 5 detected - Value: ");
    Serial.println(value = value - 1);
  }
  if (touchdetected4)
  {
    touchdetected4 = false;
    digitalWrite(led_gpio2, HIGH);
    delay(150);
    digitalWrite(led_gpio2, LOW);
    delay(150);
    Serial.print("Touch 4 detected - Value: ");
    Serial.println(value = value + 1);
  }
}

be careful rauf_anwar, the GPIO36 pin can't be in output mode, that's also why the led of the double-pressed keys doesn't light up !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.