Go Down

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


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;



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?


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
 prevState = newState;

Works great so far with my cheap, cheap button!


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131