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
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
Whereabouts in the code do you read the values from the touch sensors and where do you test the values against the threshold value ?
PatMax
November 25, 2021, 2:03pm
4
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 !)
PatMax
November 25, 2021, 2:07pm
5
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);
}
}
PatMax
November 25, 2021, 3:10pm
7
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 !
system
Closed
May 24, 2022, 3:11pm
8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.