Pages: [1]   Go Down
Author Topic: Debouncing method required for push-button toggle?  (Read 1144 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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);
  }
« Last Edit: September 02, 2010, 06:01:12 pm by gmattie » Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5551
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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")...

 smiley
Logged

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

0
Offline Offline
Edison Member
*
Karma: 0
Posts: 1103
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might want to read through this thread
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282810975/14
Logged

Melbourne, Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 219
Have you tried turning it off and on again?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
[/list]
if button is not pushed
    if reading gauge is larger than 0
    decrement reading guage
[/list]
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).

smiley
Logged

Pages: [1]   Go Up
Jump to: