1

I'm using ezbutton to make a pushbutton act like a toggle switch, first press doing permanent on, second press doing permanent off. I'm just trying to understand the basic coding. This project is just a testbed for what I'm planning to do later - wiring vehicle toggle switches to Arduino and feeding the output signal to mosfet that will control the load state. So I'm trying to get the code as nice and tidy as I possible can. In the code below, I would like to know if ledState will be applicable when this sketch will control a mosfet instead of a test led, and I've seen comments saying "ledState = !ledState;" is not optimal - but being a beginner I'm not sure what this means, and is there a better more sound way to address it. Thanks!

#include <ezButton.h>

// ---
// Declaration
// ---

const int button1  = 9;    
const int led1     = 4;    
int ledState = LOW;

ezButton button(button1);

// ---
// Setup
// ---

void setup()
{

  pinMode(led1, OUTPUT);    
  pinMode(button1, INPUT_PULLUP);    
  button.setDebounceTime(50);

}

// ***
// Main Loop
// ***
void loop()
{

  button.loop();

  if (button.isPressed()) {
    ledState = !ledState;
    digitalWrite(led1, ledState);
  }

}
7
  • what comments? if a button press should change the state from true to false and from false to true then this does it right. – Juraj Apr 18 at 14:27
  • Thanks @Juraj I'm just trying to understand the code that I managed to merge together from various examples I found, and as for ledState I haven't found explanation for it on Arduino's official reference pages, so I didn't know what did it do exactly – Varonne Apr 18 at 14:32
  • if : it works + you understand how it works , then it's find ; ledstate = !ledstate ; // is short and efficient to "flip flap" the boolean value – jo_ Apr 18 at 14:46
  • 2
    please take a coding course – Juraj Apr 18 at 14:59
  • 1
    Thanks, I'll have to try the button class. I didn't know it contained a state machine. I just though it de-bounced switches. – st2000 Apr 18 at 15:32
0

I've been looking through the source code for ezButton.c

The function isPressed() is something of a misnomer and is only meaningful for a push-to-make button on the low side of a pull-up resistor. It actually detects falling edges of the signal, so it should be called isFallingEdge(), but the code itself is good.

Similarly for isReleased() which detects rising edges of the signal, so should be called isRisingEdge().

The constructor defaults to INPUT_PULLUP which may (or may not) be what you want for your buttons and toggle switches, so it's something to bear in mind especially for low-side push-to-break buttons or high-side push-to-make buttons which will generate rising edges when pressed and falling edges when released thus reversing the meanings of isPressed() and isReleased(), hence why they really should be called isFallingEdge() and isRisingEdge().

The algorithm in ezButton::loop() could be optimised a bit by getting rid of previousSteadyState and nesting the final if statement within the second if statement.

Other than that, it's quite a good algorithm that makes use of hysteresis to reset the debounce timer.

For comparison you could take a look at my algorithm in this simple debouncer for Arduino that simply detects rising edges, falling edges or both edges without regard to whether a button was pressed or released, and can be used in a polling loop or with interrupts. Here's the essence of the algorithm:

    bool input_state = digitalRead(INPUT_PIN);
    unsigned long current_ms = millis();

    edge = rise = fall = false;

    // Hysteresis:
    //   If there is no change, reset the debounce timer.
    //   Else, compare the time difference with the debounce delay.
    if (input_state == output_state)
    {
      last_ms = current_ms;
    }
    else if ((current_ms - last_ms) >= DEBOUNCE_DELAY_ms)
    {
      // Successfully debounced, so update the outputs.
      rise = input_state && !output_state;
      fall = !input_state && output_state;
      edge = rise || fall;
      output_state = input_state;
    }

And ledState = !ledState; simple toggles the value of ledState which is what you write to your LED with digitalWrite(led1, ledState); each time a falling edge is detected in the input signal.

1
  • Thanks for the insight @tim ! I'll be honest I'm struggling with this because I'm completely new to electronics and in the past month I've introduced myself to crimping, soldering, Arduino, connectors, all the things, and it's bit overwhelming, but I'll try to comprehend :D – Varonne Apr 19 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.