Arduino debounce code.

Hello fellows. I have a problem with a code line in the debounce code.

if (reading != lastButtonState) { lastDebounceTime = millis(); }

if ((millis() - lastDebounceTime) > debounceDelay) {

if (reading != buttonState) { buttonState = reading;

if (buttonState == HIGH) { ledState = !ledState; } } }

what is that line doing?? example: First:

millis()=1

lastdebounce=1 1-1>0 False

Second

millis()=2 lastdebounce=2

2-2>0

51th

millis()=51 lastdebounce=51

51-51>0

Am I understanding this wrong? Is the second if the only one repeating.

If that is the case why do not we just put a delay(50) after the push and then read that again.

It is really hard for me to figure it out what is that line doing. Thanks for everything.

It is really hard for me to figure it out what is that line doing.

What which line is doing?

is your debounceDelay == 0???

Please, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags [code][/code]

It looks like example from here: https://www.arduino.cc/en/Tutorial/Debounce Have you read the comments in the code?

Every time button change state, lastDebounceTime is updated. When button stabilize, your if statement will soon be true (debounceDelay millis later).

Hey hector.

http://robotsbigdata.com/docs-arduino.html

i found this link pretty helpful to learn how to use timers (millis), for a newbie it takes a bit to get your head around, i downloaded the files for timer and opened the header and cpp files for timer. Atmel studio 7 is free and can open both files. i followed each line off code and wrote it down on paper, highlighting the functions and variables. once you can see step by step on paper, you will understand what timing is all about.

the biggest thing i learnt is 1. millis(); or micros(); both work the same way, every so often a behind the scenes counter overflows and updates the millis() and micros() counters.

    unsigned long snapshot;
    unsigned long (target);

void startTimer(){
        1. unsigned long current;
        2. current += (millis()-snapshot);
        3. if(current >= target){
        snapshot = millis();
        timerUp = !timerUp;
  1. variable current, (current millis)
  2. this line stores millis - snapshot, snapshot is your (lastDebounceTime = millis();)
  3. if statement asks whether current millis has reached your target.

the code below uses classes, which is similar to a library but it is directly in your code.

when you use timers and your timer is finished the first thing you should do is take a snapshot. so you can refer to it on the next loop. from code below

if(current >= target)
{
    snapshot = millis();

if you run this code you can see how timers work, easier to learn timers first and then apply it to something like debouncing.

int pin13 = 13;
int pin9 = 9;

class TestTimer{
    public:
    unsigned long snapshot;
    unsigned long (target);
    bool timerUp;

    void setTimer(unsigned long value){
        target = value;
    }

    void startTimer(){
        unsigned long current;
        current += (millis()-snapshot);
        if(current >= target){
            snapshot = millis();
            timerUp = !timerUp;
        }
    }
};
/*=========== Instantiate class members==============*/
TestTimer LED1;
TestTimer LED2;


void LED1Update(){
    if (LED1.timerUp == false){
        digitalWrite(pin13, LOW);
    }
    if(LED1.timerUp == true){
        digitalWrite(pin13, HIGH);
    }
}


void LED2Update(){
    if (LED2.timerUp == false){
        digitalWrite(pin9, LOW);
    }
    if(LED2.timerUp == true){
        digitalWrite(pin9, HIGH);
    }
}

void setup() {
    //Serial.begin(115200);
    pinMode(pin13, OUTPUT);
    pinMode(pin9, OUTPUT);

    LED1.setTimer(1000);
    LED2.setTimer(10000);

}

void loop() {
    LED1.startTimer();
    LED2.startTimer();

    LED1Update();
    LED2Update();

    /*========= loop time (25.7ms with 2 class members) ============*/
    //static long x;
    //static long last;
    //x  = ++x;
    //if (x == 1000){
      //serial.println(millis() - last);
      //last = millis();
      //x = 0;
      //}
    /*========= loop time (73ms with 6 class members) ============*/
}

millis() and micros() are also explained in the Arduino reference. It's not like they tried to make it a big mystery.

aarg, not if you know where and what to look at, its easy for the old hands, but when your just starting up the examples can be a bit vague, even if you know what your looking for.

joeblogs: aarg, not if you know where and what to look at, its easy for the old hands, but when your just starting up the examples can be a bit vague, even if you know what your looking for.

The documentary for millis() links to this tutorial. You be the judge. https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay