Go Down

Topic: Debouncing method required for push-button toggle? (Read 1 time) previous topic - next topic

TheDarkIn1978

Sep 03, 2010, 12:39 am Last Edit: Sep 03, 2010, 01:01 am by gmattie Reason: 1
i'm a new Arduino user and was having trouble using debouncing methods (with millis() timers) all day.  it seems like the proper thing to do when using a push button toggle switch according to the book i'm reading and many online forms.

however, i just figured out a way to completely remove debouncing timer methods from my code and replace it with a simple buttonCount variable instead.  basically, the state changes when the button's state has changed twice (push and release).  it works perfect and i didn't need to resort to using timers or fear debouncing.  so i assume, anyway.  is my code below debounce proof?  should i still include bedounce methods in the sketch rather than relying on my buttonCount variable?


Code: [Select]
//Constants
const int LED = 13;
const int BUTTON = 7;
const int DELAY = 25;

//Variables
int reading = LOW;
int previousReading = LOW;
int buttonCount = 0;
boolean state = false;

//Initialize
void setup()
 {
 pinMode(LED, OUTPUT);
 pinMode(BUTTON, INPUT);
 }
 
//Execute
void loop()
 {
 reading = digitalRead(BUTTON);
 
 if  (reading != previousReading)
     {
     previousReading = reading;
     buttonCount++;
     }
 
 if  (buttonCount == 2)
     {
     state = !state;
     buttonCount = 0;
     }
 
 switch  (state)
         {
         case  (true):     strobe();
                           break;
                           
         case  (false):    digitalWrite(LED, LOW);
         }
 }
 
void strobe()
 {
 digitalWrite(LED, HIGH);
 delay(DELAY);
 digitalWrite(LED, LOW);
 delay(DELAY);
 }


cr0sh

Quote
is my code below debounce proof?


I think you're just getting lucky with your switch.

The point of debouncing is the fact that when you close a switch, the switch likely doesn't close completely, instead it "bounces"; making and breaking contact (a few times) very quickly (maybe even quicker than the cycling of the cpu) according to the "springiness" of the contacts and the design of the switch. Debouncing attempts to let things settle down and filter out this issue.

I would put the code back in, unless this isn't a critical thing to you...

Something else to consider (especially if debounce code is causing you a speed issue or such) - you can debounce a switch using a bit of extra hardware (there are several different ways to do it - a quick googling will show you how - look up "hardware debounce")...

:)
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.


InvalidApple

If you wanted to go this way, what you can do is create a variable that increments every time it sees a positive reading and decrements when it sees a negative reading.

Set the variable range so it stops decrementing at 0, and incrementing at something like 20.

If it is over, say, 10, consider it on.

ie.

(pseudo)
if button is pushed
    if reading gauge is under 20
      increment reading_guage

if button is not pushed
    if reading gauge is larger than 0
      decrement reading guage

if reading gauge is larger than 10
    button is pushed

(/pseudo)

This method works well if you are continuously polling for the buttons value (checking over and over).

:)

Go Up