I have a while loop that turns the pin 13 LED on and off, and then repeats the flashing until a switch is opened.
The switch is connected between pin 2 and ground.
If the switch is open when the sketch starts, the LED never flashes. This is what I expect to happen.
If the switch is closed when the sketch starts the while loop flashes the LED until the switch is opened.
The problem is the following: If the sketch starts with the switch closed, and I open the switch as soon as I see the LED illuminate, the sketch completes the loop and goes through it one more time, even though the switch status is checked at the bottom of the loop. The on and off states of the LED last for 2000 ms each, so there is plenty of time to me to release the switch, I would think, before the switch status is updated at the bottom of the loop.
This problem was first seen with a motor controller shield. I've recreated the problem with the LED to simply the situation.
The loop is in the setup() portion of the sketch by design.
What am I missing?
#include <Bounce2.h>
// autoManPin - HIGH (default) = auto mode, LOW = manual/stop mode
const int autoManPin = 2;
// set debounce interval
const uint16_t dbTime = 5;
// create debounced switch object
Bounce autoManLine = Bounce();
const int LED_PIN = 13;
// =============== SETUP ===============
void setup() {
pinMode(LED_PIN, OUTPUT);
// connect internal pull-up resistors on switch lines
// N.B. pull-up must be set before pin assignment to function properly
// attach debounce switches to pins, debounce time interval
// MANUAL-STOP/AUTO switch line
pinMode(autoManPin, INPUT_PULLUP);
autoManLine.attach(autoManPin);
autoManLine.interval(dbTime);
// update autoManLine switch
autoManLine.update();
int autoManLineValue = autoManLine.read();
// WHILE SWITCH SET TO MANUAL (LOW value)
while (autoManLineValue == LOW) {
digitalWrite(LED_PIN, HIGH);
delay(2000);
digitalWrite(LED_PIN, LOW);
delay(2000);
// UPDATE SWITCH POSTION BEFORE LOOPING
autoManLine.update();
autoManLineValue = autoManLine.read();
}
}
// ========== LOOP ==========
void loop() {
}