Where am I wrong?Debounce related question[SOLVED]
hi all, i'm having problem that's driving me crazy!
i take debounce code arduino examples sketch, , add couple of check more:
the highlighted part mine, need behaviour:
- button pressed , led off ? switch on led
- button pressed , led on ? switch off led
but problem randomly works, sometime needs 2 or 3 presses switch on/off led, sometime on first press.
do see wrong in code?
i take debounce code arduino examples sketch, , add couple of check more:
code: [select]
void loop() {
int reading = digitalread(buttonpin);
// 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;
[glow] if(buttonstate == high)
{
if(toggle == high)
toggle = low;
else
toggle = high;
}[/glow]
}
// set led using state of toggle:
[glow]digitalwrite(ledpin, toggle); [/glow]
// save reading. next time through loop,
// it'll lastbuttonstate:
lastbuttonstate = reading;
}
the highlighted part mine, need behaviour:
- button pressed , led off ? switch on led
- button pressed , led on ? switch off led
but problem randomly works, sometime needs 2 or 3 presses switch on/off led, sometime on first press.
do see wrong in code?
ok, found part of problem, needed change "toggle" if buttonstate changed, happens led never switched on :-(
any idea??
quote
if ((millis() - lastdebouncetime) > debouncedelay) {
// whatever reading at, it's been there longer
// debounce delay, take actual current state:
buttonstate = reading;
if (buttonstate == low && lastbuttonstate == high ||
buttonstate == high && lastbuttonstate == low) {
if(toggle == high)
toggle = low;
else
toggle = high;
}
}
any idea??
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Where am I wrong?Debounce related question[SOLVED]
arduino
Comments
Post a Comment