I have code that creates a sequential signal with 4 LEDS. It cascades to the right. This works when I hold down the input button. What I need help with is having the code run exactly the same when there is an input signal of .5s HIGH --> .5s LOW (repeat). So what I am thinking in terms of pseudo-code is a code that keeps reading a HIGH signal as long as the signal is not low for more than .5s.
//LED outputs
int LED_5 = 6;
int LED_6 = 7;
int LED_7 = 8;
int LED_8 = 9;
//right turn signal
int RTS_IN = A0;
//state variable for switch case statements
int stateRightTurn = 0;
//time variable
unsigned long t = 0;
//interval is the time spacing between case statements
unsigned long interval = 100;
//time for shutting off the code
unsigned long off = 600;
void setup()
{
Serial.begin(9600);
//inputs
pinMode(RTS_IN, INPUT);
//right turn signal output from left to right
pinMode(LED_5, OUTPUT);
pinMode(LED_6, OUTPUT);
pinMode(LED_7, OUTPUT);
pinMode(LED_8, OUTPUT);
}
void loop()
{
StateRight();
}
//sequential turn signals
void StateRight()
{
switch (stateRightTurn)
{
case 0: //idle
if (digitalRead(RTS_IN) == HIGH)
{
Serial.println("0000");
t = millis();
SwitchRightLedsOff();
stateRightTurn = 1;
}
break;
case 1: //One LED On
if (digitalRead(RTS_IN) == LOW)
{
Serial.println("1111-LOWLOWLOW");
SwitchRightLedsOff();
stateRightTurn = 0;
}
else if (millis() > t + interval)
{
Serial.println("1111-HHHHHHHH");
t = millis();
digitalWrite(LED_5, HIGH);
stateRightTurn = 2;
}
break;
case 2: // Two LEDs On
if (digitalRead(RTS_IN) == LOW)
{
Serial.println("2222-LOWLOWLOW");
SwitchRightLedsOff();
stateRightTurn = 0;
}
else if (millis() > t + interval)
{
Serial.println("2222-HHHHHHHH");
t = millis();
digitalWrite(LED_6, HIGH);
stateRightTurn = 3;
}
break;
case 3: //Three LEDs On
if (digitalRead(RTS_IN) == LOW)
{
Serial.println("3333-LOWLOWLOW");
SwitchRightLedsOff();
stateRightTurn = 0;
}
else if (millis() > t + interval)
{
Serial.println("3333-HHHHHHHH");
t = millis();
digitalWrite(LED_7, HIGH);
stateRightTurn = 4;
}
break;
case 4: //Four LEDs On
if (digitalRead(RTS_IN) == LOW)
{
Serial.println("4444-LOWLOWLOW");
SwitchRightLedsOff();
stateRightTurn = 0;
}
else if (millis() > t + interval)
{
Serial.println("4444-HHHHHHHH");
t = millis();
digitalWrite(LED_8, HIGH);
stateRightTurn = 5;
}
break;
case 5: //No LEDs on
if (digitalRead(RTS_IN) == LOW)
{
Serial.println("5555-LOWLOWLOW");
SwitchRightLedsOff();
stateRightTurn = 0;
}
else if (millis() > t + interval)
{
Serial.println("55555-HHHHHHHH");
SwitchRightLedsOff();
stateRightTurn = 0;
}
break;
}
}
//method to switch LEDs off
void SwitchRightLedsOff()
{
digitalWrite(LED_5, LOW);
digitalWrite(LED_6, LOW);
digitalWrite(LED_7, LOW);
digitalWrite(LED_8, LOW);
}
t
to the other side of the>
sign in expressions like this:(millis() > t + interval)
so that you are always comparing intervals rather than timestamps. // Your proposed solution is a debouncing, or lock-out of a low inputs for 0.5s after the last observed high.