Go Down

Topic: Debouncing and toggle led using pushbutton problem (Read 807 times) previous topic - next topic


Feb 18, 2013, 12:01 am Last Edit: Feb 18, 2013, 12:53 am by nepdeep Reason: 1
Hi i have the following code written and tested...it works with some strangeness...the led dims three times before going to on or off state....

I think the debounce is not working properly...i guess there is some modification needed in my code....some hints and tips please...thanks in advance for you time

ps  my input pin is pulled high...so should work when grounded or button pressed


Code: [Select]

int led = 12;
int presentState=1;
int buttonPress,countLow,countHigh;

void setup() {                

 pinMode(13, OUTPUT);
   pinMode(12, OUTPUT);


// the loop routine runs over and over again forever:
void loop() {
   buttonPress=digitalRead(3);  //pin 3 is pulled high......so the function is activated only when button pressed and pin3 connected to ground....
     countLow++;//get some increment to assure the button is pressed continuously...manually...~~
   countHigh++; //~~
   if(countLow>40)//when pressed long eg...40 machine cycles.....i hope we can eliminate debounce




You normally debounce by ignoring any change for a specific amount of time after the first transition. The loop exicuites very quickly and so it is too fast to debounce your switch in this way.

Nick Gammon

How to use this forum

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Please post technical questions on the forum, not by personal message. Thanks!

More info:

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