millis instead of delay in arduino ide example code

The following is an example code from Arduino IDE. But in code for button bouce is delay.

Can you advise please how to put millis function instead.

Thanks in advance.

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange
*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

}

Clearly you just want an easy answer otherwise you would not post it like this. So just have a look at the Bounce2 library :slight_smile:

Use this example instead.

int state = HIGH;      // the current state of the output pin

Wow ::slight_smile:

long time = 0;         // the last time the output pin was toggled
//....
time = millis();

NO NO NO NO NO!

anthonyHope:
Use this example instead.

So please don't and run while you can...

septillion:
run while you can…

Presumably if you don’t like that example, you have reported it to the site’s owners. Then I’m sure they’ll fix it like they fix all the other crap, like examples running servos from the 5V pin.

septillion:
So just have a look at the Bounce2 library :slight_smile:

Talking of, here’s my state change detect template using Bounce2, changed for INPUT_PULLUP, and pruned of the counter stuff:

//StateChangeDetection example pruned of all the counting stuff and debounced
//active low
//debounced
#include <Bounce2.h>
Bounce debouncer = Bounce();

// this constant won't change:
const int  inputPin = 2;    // the pin that the input voltage is attached to


// Variables will change:
int inputState;         // current state of the input
int lastInputState=1;     // previous state of the input

void setup()
{
  // initialize the input pin as a input pullup for active low
  pinMode(inputPin, INPUT_PULLUP);
  debouncer.attach(inputPin);
  debouncer.interval(50);
  Serial.begin(9600);
  Serial.println("Debounced state change detect, input pullup");
  Serial.println("Press the button then release it");
}


void loop()
{
    debouncer.update();
  // read the input pin:
  inputState = debouncer.read();

  // compare the inputState to its previous state (lastInputState)
  if (inputState != lastInputState) //so it changed one way or the other
  {
    if (inputState == LOW) //pressed
    {
      // if the current state is LOW then the input
      // went from not high to low:
      Serial.print("Pressed");

      // *****************************************
      // do the stuff that a PRESSED input triggers
      // *****************************************

    }
    else //released
    {
      // if the current state is HIGH then the input
      // went from low to high: do nothing except print something if you want.
      Serial.println("..released");

      // *****************************************
      // do the stuff that a RELEASED input triggers
      // *****************************************

    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastInputState = inputState;
}

mikaletto:
Can you advise please how to put millis function instead.

There is a delay(5); call in your program. Do you want to replace this function by codes that work around millis() function?

GolamMostafa:
There is a delay(5); call in your program.

50

(It's not OP's program, it's this one.)

anthonyHope:
Presumably if you don’t like that example, you have reported it to the site’s owners. Then I’m sure they’ll fix it like they fix all the other crap, like examples running servos from the 5V pin.

It’s a constant battle. But the least I can do is not shoot in my own foot and not feed newbies examples with serious bugs in it. Because it will come back to use.

But if it was up to me I would just disable the examples site until they are decent. But then again, I would also like if every second chapter in each Arduino tutorial was called “And now NEVER EVER use delay() agian”. :slight_smile:

And Bounce2 can also take care of the state change detection :slight_smile:

#include <Bounce2.h>

// this constant won't change:
const byte buttonPin = 2;    // the pin that the input voltage is attached to

// Variables will change:
Bounce button = Bounce();  //(de)Bounce object representing the button
unsigned int buttonPushCounter = 0;   // counter for the number of button presses

void setup()
{
  // initialize the input pin as a input pullup for active low
  button.attach(buttonPin, INPUT_PULLUP);
  button.interval(50);
  Serial.begin(9600);
  Serial.println("Debounced state change detect, input pullup");
  Serial.println("Press the button then release it");
}

void loop()
{
  button.update();

  //Bounce2 also takes care of the state change detection
  if (button.fell()) //became pressed
  {
    // if the current state is LOW then the input
    // went from not high to low:
    Serial.print("Pressed, ");
    buttonPushCounter++;
    Serial.print("number of button pushes: ");
    Serial.println(buttonPushCounter);

    // *****************************************
    // do the stuff that a PRESSED input triggers
    // *****************************************
  }
  else if(button.rose()) //became released
  {
    // if the current state is HIGH then the input
    // went from low to high: do nothing except print something if you want.
    Serial.println("...released");

    // *****************************************
    // do the stuff that a RELEASED input triggers
    // *****************************************
  }
}
if (button.fell()) //became pressed
...
...
else if(button.rose()) //became released

Well I didn't know about those, so thanks for that.

Are they new-ish features? I don't recall seeing them when I made my template, the file of which was created over 2.5 years ago, but that's not to say they weren't there.

When I added the feature to set pinMode when attaching the pin to the library in 2015 it already include rose() and fall() :smiley:

septillion:
When I added the feature to set pinMode when attaching the pin to the library in 2015 it already include rose() and fall() :smiley:

Yeah your example above fell over for me on the pinMode() thing, so I just upgraded to 2.52 and I see the updated "change" example.

// compare the buttonState to its previous state
  if (buttonState != lastButtonState) {    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
...
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

changes to:

  unsigned long currentTime = millis();
  static unsigned long previousStateChangeTime;
  const unsigned long debounceInterval = 50;

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState && currentTime - previousStateChangeTime >= debounceInterval) 
  {
    previousStateChangeTime = currentTime;
    // save the current state as the last state, for next time through the loop
    lastButtonState = buttonState;

    if (buttonState == HIGH) {
...
    }
  }