I've been working on this problem off and on for weeks, and I am just too dense to figure it out. What I'm trying to do here is really simple. I've got a blue LED, a red LED, and a tactile switch. The sketch starts off with blue on and red off by default. I push the switch once, and blue turns off while red turns on. I push it again, and they alternate, flashing. I push it again...and it's supposed to go back to blue on/red off. But because of inevitable overlap in the flash delay and the debounce delay, more than half the time it just keeps flashing. I've tried tweaking the debounce delay length, but I don't have much leeway in tweaking the flashing interval.
I'm wondering if it's possible to make a debouncer using the EventFuse library rather than the standard delay. But I've never used the EventFuse library, and frankly I just can't wrap my head around it.
I just want fix this sketch so that it reacts reliably when the switch is pressed. I don't care about the means. It just occurred to me that EventFuse might solve the problem, but I am welcome to any suggestions on how to fix this. Just please keep in mind that I'm a n00b with close to zero programming experience, and only slightly more experience in electronics. Many thanks in advance.
// 3-mode blue and red
const int ledRed = 10; // assign red LED to pin 10
const int ledBlue = 9; // assign blue LED to pin 9
const int buttonPin = 2; // assign tactile switch to pin 2int 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 you get multiple presses in one buttonpress.
int x = 0; // a number that tells us where we are in the cyclevoid setup() // setting things up
{
pinMode(ledBlue, OUTPUT); // set blue LED pin to output
pinMode(ledRed, OUTPUT); // set red LED pin to output
pinMode(buttonPin,INPUT); // set button pin as input
}
void loop() // starting loop
{
button = digitalRead(buttonPin); // read current state of the button
// If button changed state to pressed and stayed there for > deBounceDelay milliseconds:
if (!button && previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
x++;
if (x>2)x=0; // variable x is reset to 0 and the cycle begins again
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}
// If button changed state to not pressed and stayed there for > deBounceDelay milliseconds:
if (button && !previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}if(x==0) { // first mode
digitalWrite(ledRed, LOW); // makes sure red is 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 is reset to 0 and the cycle begins again
previousButton = button; // set previous state to current state which is debounced and assumed sure.
deBounceTimer = millis(); // reset debouncetimer
}
// If button changed state to not pressed and stayed there for > deBounceDelay milliseconds:
if (button && !previousButton && (millis()-deBounceTimer) > deBounceDelay)
{
previousButton = button; // set previous state to current state which is debounced and 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
}
}