LennartHennigs / Button2

Arduino/ESP button library that provides callback functions to track single, double, triple and long clicks. It also takes care of debouncing.
MIT License
487 stars 81 forks source link

multiple ESP32CapacitiveTouch.ino #59

Closed vinhx closed 11 months ago

vinhx commented 1 year ago

hi Hennigs, thank you for your awesome library , the esp32 touch sample run well, so I I'd like to implement more than one touch button i.e: (meniu_button ,eq_button volup_button, voldow_down, ). that the code run , but only one the menu_button responding action, please help to validate my below sketch. thank you alot!

/////////////////////////////////////////////////////////////////

if !defined(ESP32)

error This sketch needs an ESP32

else

/////////////////////////////////////////////////////////////////

include "Button2.h"

/////////////////////////////////////////////////////////////////

Button2 menu_button; Button2 eq_button; Button2 volup_button; Button2 voldow_button;

define button1 4 // menu-button

define button2 15 // e button

define button3 32 // vol +

define button4 33 // vol -

/////////////////////////////////////////////////////////////////

byte capStateHandler1() { int capa = touchRead(button1); return capa < menu_button.getDebounceTime() ? LOW : HIGH; } byte capStateHandler2() { int capa = touchRead(button2); return capa < eq_button.getDebounceTime() ? LOW : HIGH; } byte capStateHandler3() { int capa = touchRead(button3); return capa < volup_button.getDebounceTime() ? LOW : HIGH; } byte capStateHandler4() { int capa = touchRead(button4); return capa < voldow_button.getDebounceTime() ? LOW : HIGH; }

/////////////////////////////////////////////////////////////////

void setup() { Serial.begin(9600); delay(50); Serial.println("\n\nCapacitive Touch Demo");

menu_button.setDebounceTime(40);
menu_button.setButtonStateFunction(capStateHandler1);
menu_button.setClickHandler(click);
menu_button.setLongClickHandler(longClick);
menu_button.begin(VIRTUAL_PIN);

eq_button.setDebounceTime(40);
eq_button.setButtonStateFunction(capStateHandler2);
eq_button.setClickHandler(eq_click);
eq_button.begin(VIRTUAL_PIN);

volup_button.setDebounceTime(40);
volup_button.setButtonStateFunction(capStateHandler3);
volup_button.setClickHandler(volup_click);
volup_button.begin(VIRTUAL_PIN);

voldow_button.setDebounceTime(40);
voldow_button.setButtonStateFunction(capStateHandler4);
voldow_button.setClickHandler(voldow_click);
voldow_button.begin(VIRTUAL_PIN);

}

/////////////////////////////////////////////////////////////////

void loop() { menu_button.loop(); eq_button.loop(); volup_button.loop(); voldow_button.loop(); }

///////////////////////////////////////////////////////////////// void click(Button2& btn) { Serial.println("menu click\n"); }

// long click void longClick(Button2 &btn) { unsigned int time = btn.wasPressedFor(); if (time > 1000) { Serial.println("long click\n"); } }

void eq_click(Button2& btn) { Serial.println("eq click\n"); }

void volup_click(Button2& btn) { Serial.println("volup click\n"); }

void voldow_click(Button2& btn) { Serial.println("voldow click\n"); }

/////////////////////////////////////////////////////////////////

endif

/////////////////////////////////////////////////////////////////


vinhx commented 1 year ago

ah, I found that you use the setDebounceTime method to define the threshold value for the handler to make compre with touch read value . I fixed my issue now, thank you!