Trouble with Debounce tutorial
i'm working through debounce tutorial @ http://arduino.cc/en/tutorial/debounce. code included in examples in arduino 0018 environment.
my understanding of sketch should toggle led (switch on off, or vice versa) when button pushed.
the description says, "this example demonstrates use of pushbutton switch: each time press button, led (or whatever) turned on (if it's off) or off (if on)."
i traced through code line line , tried understand how increment (is right term?) led's state, looked me code turn led on when button pushed , turn off when button released, not toggle output state of led pin.
here's
when run code, that's happens. new programming , electronics, seems me beginning line buttonstate = reading, there should routine switches button state high low or vice versa.
am correct code description doesn't match code?
my understanding of sketch should toggle led (switch on off, or vice versa) when button pushed.
the description says, "this example demonstrates use of pushbutton switch: each time press button, led (or whatever) turned on (if it's off) or off (if on)."
i traced through code line line , tried understand how increment (is right term?) led's state, looked me code turn led on when button pushed , turn off when button released, not toggle output state of led pin.
here's
code: [select]
/*
debounce
each time input pin goes low high (e.g. because of push-button
press), output pin toggled low high or high low. there's
minimum delay between toggles debounce circuit (i.e. ignore
noise).
circuit:
* led attached pin 13 ground
* pushbutton attached pin 2 +5v
* 10k resistor attached pin 2 ground
* note: on arduino boards, there led on board
connected pin 13, don't need components example.
created 21 november 2006
david a. mellis
modified 3 jul 2009
limor fried
http://www.arduino.cc/en/tutorial/debounce
*/
// constants won't change. they're used here
// set pin numbers:
const int buttonpin = 2; // number of pushbutton pin
const int ledpin = 13; // number of led pin
// variables change:
int ledstate = high; // current state of output pin
int buttonstate; // current reading input pin
int lastbuttonstate = low; // previous reading input pin
// following variables long's because time, measured in miliseconds,
// become bigger number can stored in int.
long lastdebouncetime = 0; // last time output pin toggled
long debouncedelay = 50; // debounce time; increase if output flickers
void setup() {
pinmode(buttonpin, input);
pinmode(ledpin, output);
}
void loop() {
// read state of switch local variable:
int reading = digitalread(buttonpin);
// check see if pressed button
// (i.e. input went low high), and you've waited
// long enough since last press ignore noise:
// if switch changed, due noise or pressing:
if (reading != lastbuttonstate) {
// reset debouncing timer
lastdebouncetime = millis();
}
if ((millis() - lastdebouncetime) > debouncedelay) {
// whatever reading at, it's been there longer
// debounce delay, take actual current state:
buttonstate = reading;
}
// set led using state of button:
digitalwrite(ledpin, buttonstate);
// save reading. next time through loop,
// it'll lastbuttonstate:
lastbuttonstate = reading;
}
when run code, that's happens. new programming , electronics, seems me beginning line buttonstate = reading, there should routine switches button state high low or vice versa.
am correct code description doesn't match code?
i believe description of code correct. comments in code appear wrong.
it wouldn't take - boolean hold led state, toggled each time the button pressed, make code match comments.
it wouldn't take - boolean hold led state, toggled each time the button pressed, make code match comments.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Trouble with Debounce tutorial
arduino
Comments
Post a Comment