Pages: [1]   Go Down
Author Topic: button debouncing for arduino  (Read 444 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Dont think like a computer, make it think like you
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found this bit of code:

Code:
boolean DebounceSwitch2() {
      int State = 0;
      State=(State<<1) | !RawKeyPressed() | 0xe000;
      if (State==0x000)
            return true;
      return false;
}

I'm trying to "port" this to Arduino. I'm pretty sure that RawKeyPressed() would be the same as digitalRead() (for my purposes), but  exactly what does the whole line mean?
Code:
     State=(State<<1) | !RawKeyPressed() | 0xe000;

Gabe
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 9
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

AFAICT, that code doesn't work (it will always return false), and even if it did, it seems much too clever for what it's trying to do.

I just posted a new example on the tutorial page: http://www.arduino.cc/en/Tutorial/Switch

It uses a pushbutton as a switch to toggle an LED on and off, and debounces the switch input.  Does it help?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Dont think like a computer, make it think like you
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I ended up doing almost the exact thing as you:

Code:
 newState = digitalRead(inPin);

  if ( (newState != prevState) && (timer + bounceTime < millis()) ){
    timer = millis();    //record time of output
    Serial.print((inPin*10)+newState);
  }
  
  prevState = newState;

Works great so far with my cheap, cheap button!

Gabe
Logged

Pages: [1]   Go Up
Jump to: