Debounce using EventFuse library?
i've been working on problem off , on weeks, , dense figure out. i'm trying here simple. i've got blue led, red led, , tactile switch. sketch starts off blue on , red off default. push switch once, , blue turns off while red turns on. push again, , alternate, flashing. push again...and it's supposed go blue on/red off. because of inevitable overlap in flash delay , debounce delay, more half time keeps flashing. i've tried tweaking debounce delay length, don't have leeway in tweaking flashing interval.
i'm wondering if it's possible make debouncer using eventfuse library rather standard delay. i've never used eventfuse library, , frankly can't wrap head around it.
i want fix sketch reacts reliably when switch pressed.
don't care means. occurred me eventfuse might solve problem, welcome suggestions on how fix this. please keep in mind i'm n00b close 0 programming experience, , more experience in electronics. many in advance.
i'm wondering if it's possible make debouncer using eventfuse library rather standard delay. i've never used eventfuse library, , frankly can't wrap head around it.
i want fix sketch reacts reliably when switch pressed.
don't care means. occurred me eventfuse might solve problem, welcome suggestions on how fix this. please keep in mind i'm n00b close 0 programming experience, , more experience in electronics. many in advance.quote
// 3-mode blue , red
const int ledred = 10; // assign red led pin 10
const int ledblue = 9; // assign blue led pin 9
const int buttonpin = 2; // assign tactile switch pin 2
int button = 0; // current state of button
int previousbutton = 0; // previous debounced state of button
unsigned long debouncetimer = 0;
const unsigned long debouncedelay = 6; // increase if multiple presses in 1 buttonpress.
int x = 0; // number tells in cycle
void setup() // setting things up
{
pinmode(ledblue, output); // set blue led pin output
pinmode(ledred, output); // set red led pin output
pinmode(buttonpin,input); // set button pin input
}
void loop() // starting loop
{
button = digitalread(buttonpin); // read current state of button
// if button changed state pressed , stayed there > debouncedelay milliseconds:
if (!button && previousbutton && (millis()-debouncetimer) > debouncedelay)
{
x++;
if (x>2)x=0; // variable x reset 0 , cycle begins again
previousbutton = button; // set previous state current state debounced , assumed sure.
debouncetimer = millis(); // reset debouncetimer
}
// if button changed state not pressed , stayed there > debouncedelay milliseconds:
if (button && !previousbutton && (millis()-debouncetimer) > debouncedelay)
{
previousbutton = button; // set previous state current state debounced , assumed sure.
debouncetimer = millis(); // reset debouncetimer
}
if(x==0) { // first mode
digitalwrite(ledred, low); // makes sure red turned off
digitalwrite(ledblue, high); // turns on blue
}
if(x==1) { // second mode
digitalwrite(ledblue, low); // turns off blue
digitalwrite(ledred, high);
} // turns on red
if(x==2) { // third mode, flashing
if (!button && previousbutton && (millis()-debouncetimer) > debouncedelay)
{
x++;
if (x>2)x=0; // variable x reset 0 , cycle begins again
previousbutton = button; // set previous state current state debounced , assumed sure.
debouncetimer = millis(); // reset debouncetimer
}
// if button changed state not pressed , stayed there > debouncedelay milliseconds:
if (button && !previousbutton && (millis()-debouncetimer) > debouncedelay)
{
previousbutton = button; // set previous state current state debounced , assumed sure.
debouncetimer = millis(); // reset debouncetimer
}
digitalwrite(ledred, low); // turns off red
digitalwrite(ledblue, high); // turns on blue
delay(300); // waits 300 milliseconds
digitalwrite(ledblue, low); // turns off blue
digitalwrite(ledred, high); // turns on red
delay(300); // waits 300 milliseconds
}
}
hi,
the problem debouncetimer set millis() when button stable. if button changes again, millis()-debouncetimer alreay bigger debouncedelay.
add following statement begind button = digitalread(buttonpin):
if (button == previousbutton) debouncetimer = millis();
this should work.
mike
the problem debouncetimer set millis() when button stable. if button changes again, millis()-debouncetimer alreay bigger debouncedelay.
add following statement begind button = digitalread(buttonpin):
if (button == previousbutton) debouncetimer = millis();
this should work.
mike
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Debounce using EventFuse library?
arduino
Comments
Post a Comment