Pages: [1]   Go Down
Author Topic: Debouncing and toggle led using pushbutton problem  (Read 685 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
  }
  
//////////////////////////////////////////////////////////////////////
« Last Edit: February 17, 2013, 06:53:30 pm by nepdeep » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 34579
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 504
Posts: 19095
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1]   Go Up
Jump to: