The following code I have has a light blink at one speed when the output is high and a different speed when the output is low, but each output only blinks once before it goes high or low. My goal for this project is to have it so when I press the debounce switch, the light will blink at one speed continuously speed until the button is pressed again and then it will toggle to a different blinking speed for the light. So the debounce switch can toggle between two different blinking settings. I am new Arduino, so the more you can help the better!
const int buttonPin = 2;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
#include <mechButton.h>
#include <blinker.h>
#define BUTTON_PIN 2
#define LED_PIN 13
#define PERIOD_A 300 // Ms
#define PERIOD_B 100 // Ms
mechButton ourButton(BUTTON_PIN); // Our button/switch. All deboiunced and running on auto.
blinker ourBlinker(LED_PIN); // Our blinker.
bool blinkA; // Our state variable.
void setup(void) {
ourBlinker.setPeriod(PERIOD_A); // Initialze our blinker to peroid "A".
blinkA = true; // Note that it's set to "A".
ourBlinker.setOnOff(true); // Fire up the blinks.
ourButton.setCallback(buttonClicked); // A function to call when the button gets clicked.
}
// This gets called when the button gets clicked.
void buttonClicked(void) {
if (!ourButton.trueFalse()) { // If the pin has gone to ground.. (pressed)
if (blinkA) { // If the lights are running period "A"..
ourBlinker.setPeriod(PERIOD_B); // Set to period "B"
} else { // Else they are alredy period "B"..
ourBlinker.setPeriod(PERIOD_A); // So we set them to peroid "A".
}
blinkA = !blinkA; // Flip the state of the LED state variable.
}
}
void loop(void) {
idle(); // This lets things like the mechButton do their thing.
}
You will need to install LC_baseTools from the library manager to try it.
it's just a matter of toggling between two different timing intervals
while you try to detect a button state, you need to capture that state when it changes. and when it changes you need to check for the active state (LOW).
when there's a button pressure a flag can be toggled and used to select between different delays
while you check that the time since some last time is > some interval, the last time must be set to the current time.
there's no need to check for buttons when the time interval has expired.
and using shorter variable names makes the code easier to read and is less confusing
consider
#if 0 // my hardware
const int buttonPin = A1;
#else
const int buttonPin = 2;
#endif
const int ledPin = 13;
unsigned long debounceDelay = 200;
byte butLst;
unsigned long msecLst;
int mode = 0;
// -----------------------------------------------------------------------------
void setup() {
Serial.begin (115200);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
byte but = digitalRead(buttonPin);
if (butLst != but) {
butLst = but;
if (LOW == but) {
mode = ! mode;
if (mode)
debounceDelay = 200;
else
debounceDelay = 500;
Serial.println (debounceDelay);
}
delay (20);
}
unsigned long msec = millis ();
if ((msec - msecLst) > debounceDelay) {
msecLst = msec;
digitalWrite (ledPin, ! digitalRead(ledPin));
}
}