Go Down

Topic: button debouncing for arduino (Read 564 times) previous topic - next topic

Gabe462

I found this bit of code:

Code: [Select]
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: [Select]
     State=(State<<1) | !RawKeyPressed() | 0xe000;

Gabe

mellis

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?

Gabe462

I ended up doing almost the exact thing as you:

Code: [Select]
 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

Go Up