Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple ESP32CapacitiveTouch.ino #59

Closed
vinhx opened this issue Oct 10, 2023 · 1 comment
Closed

multiple ESP32CapacitiveTouch.ino #59

vinhx opened this issue Oct 10, 2023 · 1 comment

Comments

@vinhx
Copy link

vinhx commented Oct 10, 2023

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
Copy link
Author

vinhx commented Oct 10, 2023

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants