[SOLVED] Successor Clickbutton.h for ESP32?

I have to reprogram a sketch for Arduino Nano to ESP.

Clickbutton.h had nice features, but doens't work with the ESP.
Is there a successor for Clickbutton.h suitable for ESP?

Your topic was MOVED to its current forum category as it is more suitable than the original

What sort of features do you want ?

Hello Bob,

I need to use four buttons with the next functionality:

  • All debounced

  • LongLeft, Left, Up, Down, Right, Longright.

“Long” means pressed during 1 second.

I use them to control a menu.

I hope you can find a solution.

Best regards

Kees Baltus

The Netherlands

Have you looked at the Button2 library ?

This library allows you to use callback functions to track single, double, triple and long clicks. It takes care of debouncing. Using this lib will reduce and simplify your source code significantly.

It has been tested with Arduino, ESP8266 and ESP32 devices.

Hello Bob,

Thanks for your tip.

I tried the button2 library, bust it uses within the main loop button.loop();

At a certain time I want to call the state of the buttons only once.

In the meantime I made contact with the developer of Clickbutton.h; I hope he has a tip to reuse Clickbutton.h (I was happy with the library when I used an Arduino Nano).

Best regards Kees Baltus

I am not sure what you mean by this but because you can get the state of a button as many or little times that you decide. In general it would be good practice to read the state of an input into a variable once each time through loop() and then to use or ignore the value as many times as required

I am trying to use 4 buttons (two of them with a double function; therefor I us the library Buttons2.h
I can't get enclosed sketch working.

/*
 Reference: https://github.com/LennartHennigs/Button2/blob/master/examples/MultiHandlerTwoButtons/MultiHandlerTwoButtons.ino
 This program trys to demonstrate the working of four keys; 
 The function of the keys:
 - Left  (short and long)
 - Up    (short)
 - Down  (short)
 - Right (short and long)
 */

//===================================================================
#include "Button2.h"

#define BUTTON_Left  15 
#define BUTTON_Up    2
#define BUTTON_Down  0
#define BUTTON_Right 4

Button2 button_1, button_2, button_3, button_4;
int intMenu;

//===================================================================
void setup() {
  Serial.begin(9600);
  delay(50);
  Serial.println("\n\nMulti Handler Demo with 4 buttons");

  button_1.begin(BUTTON_Left);
  button_1.setClickHandler(click);
  button_1.setLongClickHandler(longpress);

  button_2.begin(BUTTON_Up);
  button_2.setClickHandler(click);

  button_3.begin(BUTTON_Down);
  button_3.setClickHandler(click);

  button_4.begin(BUTTON_Right);
  button_4.setClickHandler(click);
  button_4.setLongClickHandler(longpress);
}

//===================================================================
void loop() {
  delay(500);
  //Call the state of the buttons
  button_1.loop();
  button_2.loop();
  button_3.loop();
  button_4.loop();    
  }

//===================================================================
void click(Button2& btn) {
    if (btn == button_1) {intMenu = 1;  Serial.println("Short click on left key");}
    if (btn == button_2) {intMenu = 3;  Serial.println("Short click on up key");  }
    if (btn == button_3) {intMenu = 4;  Serial.println("Short click on down key");}
    if (btn == button_4) {intMenu = 5;  Serial.println("Short click on down key");}
    }

//=======================================================
void longpress(Button2& btn) {
    unsigned int time = btn.wasPressedFor();
    if ((time > 750) && (btn == button_1)) {intMenu = 2; Serial.println("Long click on left key");}
    if ((time > 750) && (btn == button_4)) {intMenu = 6; Serial.println("Long click on right key");}
    }

Take the delay() out of loop()

Thanx, it works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.