-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathTouchButton.ino
46 lines (38 loc) · 1.25 KB
/
TouchButton.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
This is an example how to use Touch Intrrerupts
The sketch will tell when it is touched and then released as like a push-button
This method based on touchInterruptSetThresholdDirection() is only available for ESP32
*/
#include "Arduino.h"
int threshold = 40;
bool touchActive = false;
bool lastTouchActive = false;
bool testingLower = true;
void gotTouchEvent() {
if (lastTouchActive != testingLower) {
touchActive = !touchActive;
testingLower = !testingLower;
// Touch ISR will be inverted: Lower <--> Higher than the Threshold after ISR event is noticed
touchInterruptSetThresholdDirection(testingLower);
}
}
void setup() {
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
Serial.println("ESP32 Touch Interrupt Test");
touchAttachInterrupt(T2, gotTouchEvent, threshold);
// Touch ISR will be activated when touchRead is lower than the Threshold
touchInterruptSetThresholdDirection(testingLower);
}
void loop() {
if (lastTouchActive != touchActive) {
lastTouchActive = touchActive;
if (touchActive) {
Serial.println(" ---- Touch was Pressed");
} else {
Serial.println(" ---- Touch was Released");
}
}
Serial.printf("T2 pin2 = %d \n", touchRead(T2));
delay(125);
}