Go Down

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

nepdeep

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....
   if(buttonPress==LOW)
   {
     countLow++;//get some increment to assure the button is pressed continuously...manually...~~
     countHigh=0;
   }
   else
   {
   countHigh++; //~~
   countLow=0;
   if(countHigh>60)
   countHigh=60;
   }
   
   if(countLow>40)//when pressed long eg...40 machine cycles.....i hope we can eliminate debounce
   {
    presentState=!presentState;
   }  
   else
   {
   presentState=presentState;
   }
   digitalWrite(13,presentState);
 }
 

//////////////////////////////////////////////////////////////////////

Grumpy_Mike

Read:-
http://arduino.cc/forum/index.php/topic,149015.0.html

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.

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:
http://www.gammon.com.au/electronics

Go Up