I'm writing a simple program that senses a push button and initiates a countdown sequence and pulses a flash relay. The event sequence looks like this:
1. Green "Ready" LED is on
2. Push button is activated, Green LED turns off
3. Red LED on for 1 sec
4. Red LED off, Orange LED on for 1 sec
5. Orange LED off, Yellow LED on for 1 sec
6. Yellow LED off, Green LED on, pulse Flash relay
7. Wait for push button to be activated again
My problem is that the sequence will loop itself 2 or more times every push of the button. I'm using a common debounce code I've used in the past with no problems.
NOTE my push button has +5 on the NO contact, GND on the NC contact, and the COM contact connected to my BUTTON pin.
int BUTTON = 18; // Pin connected to push button.
int FLASH = 19; // Pin connected to flash relay
int RED = 14; // Countdown LED
int ORANGE = 15; // Countdown LED
int YELLOW = 16; // Countdown LED
int GREEN = 17; // Ready LED
int BUTstate; // Current reading from BUTTON pin
int lastBUTstate = LOW; // Previous reading from BUTTON pin
long lastDebounce = 0; // Last time output was changed
long Delay = 25; // Debounce Time
void setup()
{
pinMode (BUTTON, INPUT);
pinMode (FLASH, OUTPUT);
pinMode (RED, OUTPUT);
pinMode (ORANGE, OUTPUT);
pinMode (YELLOW, OUTPUT);
pinMode (GREEN, OUTPUT);
digitalWrite(GREEN, HIGH);
}
void loop()
{
int reading = digitalRead(BUTTON);
if (reading != lastBUTstate)
{
lastDebounce = millis();
}
if ((millis() - lastDebounce) > Delay)
{
BUTstate = reading;
}
if (BUTstate == HIGH)
{
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
delay(1000);
digitalWrite(RED, LOW);
digitalWrite(ORANGE, HIGH);
delay(1000);
digitalWrite(ORANGE, LOW);
digitalWrite(YELLOW, HIGH);
delay(1000);
digitalWrite(YELLOW, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(FLASH, HIGH);
delay(100);
digitalWrite(FLASH,LOW);
}
lastBUTstate = reading;
}
I just can't seem to figure out why it's looping itself. Once the button is released its grounded on and LOW, so why is the loop repeating?
Adam