-->
Page 1 of 1

Which debouncer code will work?

PostPosted: Thu Jul 07, 2022 5:23 pm
by alexmoorephoto
Hi All, I have seen a lot of debouncer codes available for Arduino but less that are compatible with ESP8266. I am still very new to it all which can mean that even testing these codes can be confusing. I have a sketch that sends an IFTTT notification via Webhooks when a button is pressed. I want to adjust this so that the code only runs if the button (actually a switch) is held down for 20 seconds. I have tried two approaches. One using EZButton for Arduino (may not run on esp8266?) and one using a more universal code from Danixu (original from Xoseperez). One of the things I am struggling with is how to string these example codes, and my existing code together. My original code was running entirely in Setup, but I believe we want to change that now that we are introducing the debounce function. Can anyone tell me which, if any, of these should work? I am not getting the desired results and worried it could be an error in the coding.

Original code (tested and works but gets repeat triggers):

Code: Select all#include <ESP8266Webhook.h>
#include <ESP8266WiFi.h>

#define wakePin 16
#define _SSID "xxxxssid"      // Your WiFi SSID
#define _PASSWORD "xxxxpw"  // Your WiFi Password
#define KEY "xxxxwhkey"        // Webhooks Key
#define EVENT "xxxxevent"      // Webhooks Event Name

Webhook webhook(KEY, EVENT);    // Create an object.

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(1000);

  // Connect to WiFi
  Serial.println();
  Serial.println();
  Serial.print("Connecting to: ");
  Serial.println(_SSID);
  WiFi.begin(_SSID, _PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("-");
  }

  Serial.println("");
  Serial.println("WiFi Connected");

  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  digitalWrite(LED_BUILTIN, HIGH);

//================================================================//
//================================================================//


  // Trigger without any value and get response.
  int response = webhook.trigger();
  if(response == 200)
    Serial.println("OK");
  else
    Serial.println("Failed");



    ESP.deepSleep(wakePin);
}

void loop() {
  // Nothing
}


EZButton code:

Code: Select all#include <ezButton.h>
#include <ESP8266Webhook.h>
#include <ESP8266WiFi.h>

#define _SSID "xxxxssid"      // Your WiFi SSID
#define _PASSWORD "xxxxpw"  // Your WiFi Password
#define KEY "xxxxwhk"        // Webhooks Key
#define EVENT "xxxxevent"      // Webhooks Event Name

Webhook webhook(KEY, EVENT);    // Create an object.

ezButton button(16);  // create ezButton object that attach to pin 16;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(20000); // set debounce time to 20 seconds
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed()) {
    Serial.println("The button has been pressed for 20 seconds");

  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(1000);

  // Connect to WiFi
  Serial.println();
  Serial.println();
  Serial.print("Connecting to: ");
  Serial.println(_SSID);
  WiFi.begin(_SSID, _PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("-");
  }

  Serial.println("");
  Serial.println("WiFi Connected");

  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
  digitalWrite(LED_BUILTIN, HIGH);

//================================================================//
//================================================================//


  // Trigger without any value and get response.
  int response = webhook.trigger();
  if(response == 200)
    Serial.println("OK");
  else
    Serial.println("Failed");

  }
}


Buttondebouncer code:

Code: Select all#include <Arduino.h>
#include <ButtonDebouncer.h>
#include <ESP8266Webhook.h>
#include <ESP8266WiFi.h>

#define BUTTON_PIN              16

#define CUSTOM_DEBOUNCE_DELAY   20000

// Time the library waits for a second (or more) clicks
// Set to 0 to disable double clicks but get a faster response
#define CUSTOM_REPEAT_DELAY     0
#define _SSID "xxxxssid"      // Your WiFi SSID
#define _PASSWORD "xxxxpw"  // Your WiFi Password
#define KEY "xxxxwhk"        // Webhooks Key
#define EVENT "xxxxevent"      // Webhooks Event Name

Webhook webhook(KEY, EVENT);    // Create an object.

ButtonDebouncer * button;

void setup() {
    Serial.begin(9600);
    Serial.println();
    Serial.println();
    button = new ButtonDebouncer(BUTTON_PIN, BUTTON_PUSHBUTTON | BUTTON_DEFAULT_HIGH | BUTTON_SET_PULLUP, CUSTOM_DEBOUNCE_DELAY, CUSTOM_REPEAT_DELAY);

    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(1000);
}

void loop() {
    if (unsigned int event = button->loop()) {
        if (event == EVENT_RELEASED) {
            Serial.print("Count : "); Serial.print(button->getEventCount());
            Serial.print(" Length: "); Serial.print(button->getEventLength());
            Serial.println();
}

 // Connect to WiFi
    Serial.println();
    Serial.println();
    Serial.print("Connecting to: ");
    Serial.println(_SSID);
    WiFi.begin(_SSID, _PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("-");
  }

    Serial.println("");
    Serial.println("WiFi Connected");

  // Print the IP address
    Serial.print("Use this URL to connect: ");
    Serial.print("http://");
    Serial.print(WiFi.localIP());
    Serial.println("/");
    digitalWrite(LED_BUILTIN, HIGH);

//================================================================//
//================================================================//


  // Trigger without any value and get response.
  int response = webhook.trigger();
  if(response == 200)
    Serial.println("OK");
  else
    Serial.println("Failed");
        }
    }

Re: Which debouncer code will work?

PostPosted: Sun Jul 10, 2022 1:34 am
by JurajA
I use Bounce2 library

Re: Which debouncer code will work?

PostPosted: Tue Jul 12, 2022 8:17 am
by ninadpurohit
I use avdweb Switch library. It works with Arduino and ESP both.

I don't see any hardware specific code in ezbutton source files. If it works with Arduino then it should work with ESP just fine.