Debounce

Hello,

I am new to arduino and have trouble with programming a debounce function. Yesterday I spent several hours programming, I think I understand it now, but my code doesn't work. Does anybody know why?

const int Button1 = 8; const int Led1 = 13; int Ledon = LOW; long Time = 0; const long Delay = 100;

void setup() { pinMode(Button1, INPUT); pinMode(Led1, OUTPUT); }

void loop() { if ((millis() - Time) > Delay && digitalRead(Button1) == HIGH && Ledon == LOW) { Ledon = HIGH; digitalWrite(Led1, HIGH); Time = millis(); } if ((millis() - Time) > Delay && digitalRead(Button1) == HIGH && Ledon == HIGH) { Ledon = LOW; digitalWrite(Led1, LOW); Time = millis(); } }

Debouncing means ignoring any change in state that happens within some interval of a previous change in state.

If does not mean not reading the switch until the interval has passed. That is what you are doing now.

Serial.print() statements would twll you are lot about what is going on. You seem to be missing them.

but my code doesn't work

Is real hard to help you with. The code does something. What it does you haven't explained. You want it to do something. What that is you haven't explained.

I am trying to toggle a led.

jasperachtbaan: I am trying to toggle a led.

That's all?

digitalWrite(ledPin, !digitalRead(ledPin));

Perhaps you mean "I want to toggle an LED when..."

Sorry, i mean that i am toggle a led when the button is pressed, but this code doesn't do anything, the led stays off.

jasperachtbaan: Sorry, i mean that i am toggle a led when the button is pressed, but this code doesn't do anything, the led stays off.

So you need to combine the StateChangeDetection example with the Debounce example.

First get the Debounce example working. Add another (global or static) variable to is called lastButtonState and follow the concepts of the StateChangeDetection example for assigning and checking for the signal edge.

This works pretty good, but not perfect... So what's wrong

const int Button1 = 8; const int Led1 = 13; int Ledon = LOW; long Time = 0; const long Delay = 100; int lastButtonState = LOW;

void setup() { pinMode(Button1, INPUT); pinMode(Led1, OUTPUT); }

void loop() { if (digitalRead(Button1) != lastButtonState)

if (digitalRead(Button1) == HIGH && (millis() - Time) > Delay) {

digitalWrite(Led1, !Ledon); Ledon = digitalRead(Led1); Time = millis();

}

lastButtonState = digitalRead(Button1); }

Maybe this will work,

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 8;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  }
  
  // set the LED using the state of the button:
  digitalWrite(ledPin, buttonState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

I've seen that one, but i don't understand it

jasperachtbaan: I've seen that one, but i don't understand it

Sounds like its a good opportunity to take the time to learn how it works.

That's what i'm trying to do......

jasperachtbaan: That's what i'm trying to do......

Well, it's certainly going to take more than 7 minutes.

That's only part of it though, you still need to incorporate the edge detection.

But which part dont u understand or the whole code function?

Yesterday i spent several hours trying to understand that code, and experimenting, and do you have a better example?

jasperachtbaan: Yesterday i spent several hours trying to understand that code, and experimenting, and do you have a better example?

I don't think it can get "better" than 6 lines of code.

http://www.gammon.com.au/switches

It's all there, wiring and debouncing. Oh, and state changes.