Skip to content

Commit a3e1af7

Browse files
JakelerAircoookie
andauthored
Add ESP32 touch sensors as button alternative (wled#1190)
* Add touch option to button handler * Check if touch is pressed in setup * Add TOUCHPIN build env and override example Co-authored-by: Aircoookie <dev.aircoookie@gmail.com>
1 parent 1313a44 commit a3e1af7

File tree

6 files changed

+29
-6
lines changed

6 files changed

+29
-6
lines changed

platformio.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,14 @@ lib_ignore =
334334
ESPAsyncTCP
335335
ESPAsyncUDP
336336

337+
[env:custom32_TOUCHPIN_T0]
338+
board = esp32dev
339+
platform = espressif32@1.12.4
340+
build_flags = ${common.build_flags_esp32} -D TOUCHPIN=T0
341+
lib_ignore =
342+
ESPAsyncTCP
343+
ESPAsyncUDP
344+
337345
[env:wemos_shield_esp32]
338346
board = esp32dev
339347
platform = espressif32@1.12.4

platformio_override.ini.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ build_flags = ${common.build_flags_esp8266}
2121
; PIN defines - uncomment and change, if needed:
2222
; -D LEDPIN=2
2323
; -D BTNPIN=0
24+
; -D TOUCHPIN=T0
2425
; -D IR_PIN=4
2526
; -D RLYPIN=12
2627
; -D RLYMDE=1

wled00/button.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@ void shortPressAction()
1515
}
1616
}
1717

18+
bool isButtonPressed()
19+
{
20+
#ifdef BTNPIN
21+
if (digitalRead(BTNPIN) == LOW) return true;
22+
#endif
23+
#ifdef TOUCHPIN
24+
if (touchRead(TOUCHPIN) <= TOUCH_THRESHOLD) return true;
25+
#endif
26+
return false;
27+
}
28+
1829

1930
void handleButton()
2031
{
21-
#ifdef BTNPIN
32+
#if defined(BTNPIN) || defined(TOUCHPIN)
2233
if (!buttonEnabled) return;
23-
24-
if (digitalRead(BTNPIN) == LOW) //pressed
34+
35+
if (isButtonPressed()) //pressed
2536
{
2637
if (!buttonPressedBefore) buttonPressedTime = millis();
2738
buttonPressedBefore = true;
@@ -37,7 +48,7 @@ void handleButton()
3748
}
3849
}
3950
}
40-
else if (digitalRead(BTNPIN) == HIGH && buttonPressedBefore) //released
51+
else if (!isButtonPressed() && buttonPressedBefore) //released
4152
{
4253
long dur = millis() - buttonPressedTime;
4354
if (dur < 50) {buttonPressedBefore = false; return;} //too short "press", debounce

wled00/const.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126

127127
#define ABL_MILLIAMPS_DEFAULT 850; // auto lower brightness to stay close to milliampere limit
128128

129+
#define TOUCH_THRESHOLD 32 // limit to recognize a touch, higher value means more sensitive
130+
129131
// Size of buffer for API JSON object (increase for more segments)
130132
#ifdef ESP8266
131133
#define JSON_BUFFER_SIZE 9216

wled00/fcn_declare.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void updateBlynk();
2121

2222
//button.cpp
2323
void shortPressAction();
24+
bool isButtonPressed();
2425
void handleButton();
2526
void handleIO();
2627

wled00/wled.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ void WLED::beginStrip()
250250
#endif
251251

252252
// disable button if it is "pressed" unintentionally
253-
#ifdef BTNPIN
254-
if (digitalRead(BTNPIN) == LOW)
253+
#if defined(BTNPIN) || defined(TOUCHPIN)
254+
if (isButtonPressed())
255255
buttonEnabled = false;
256256
#else
257257
buttonEnabled = false;

0 commit comments

Comments
 (0)