Button debounce on Arduino

I'm trying to use this guide for the button debounce Arduino - Button - Debounce | Arduino Tutorial (arduinogetstarted.com) on my arduino. The idea is when I press the button the code on void.setup run once, and then when I press the button again the rest of the code on void.setup is executed.

My problem with the debounce code is that none of my code is on void.loop and what I can do to make the debounce code run on void.setup instead of running on void.loop?

#include <ezButton.h>

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

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
}

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

  if(button.isPressed())
    Serial.println("The button is pressed");

  if(button.isReleased())
    Serial.println("The button is released");
}
void setup() 
{
  Serial.begin(9600);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds

  // Wait until button is pressed
  do 
  {
    button.loop();
  } while (!button.isPressed());
  Serial.println("The button is pressed");

  // Some of the code in setup()

  // Wait until button is released and pressed again
  do 
  {
    button.loop();
  } while (!button.isReleased());
  Serial.println("The button is released");
  do 
  {
    button.loop();
  } while (!button.isPressed());
  Serial.println("The button is pressed again");

  // The rest of the code in setup()
}
1 Like

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