Here's some code...
Derived from your original example
Not tested, but it compiles without errors
/*--------------------------------------------------------------------------------------//
3-Light Sequential Brake/Turn Signal Actuation
(Both Left and Right sides)
//--------------------------------------------------------------------------------------*/
#define PotInput A5
#define BRAKE_SWITCH (digitalRead(RightTurnSwitch)==HIGH && digitalRead(LeftTurnSwitch)==HIGH)
const int LeftLED[] = {10, 11, 12};
const int RightLED[] = {5, 4, 3};
const int LeftTurnSwitch = 8;
const int RightTurnSwitch = 7;
int delayPeriodMS = 250; // declare delay time (default 250mS)
// this can be stored in EEPROM if needed
boolean Rstate=0, Lstate=0;
// *********************************************************************************
void setup() {
for (int counter = 0; counter <= 2; counter++) {
pinMode (RightLED[counter], OUTPUT); // Right: connect LEDs, relay drivers or mosfet Gates here
pinMode (LeftLED[counter], OUTPUT); // Left: connect LEDs, relay drivers or mosfet Gates here
}
pinMode (PotInput, INPUT);
// connect Brake/Turn signal here (through divider or regulator to give ~4-5V when ON)
pinMode (RightTurnSwitch, INPUT); // Right ts/b
pinMode (LeftTurnSwitch, INPUT); // Left ts/b
}
// *********************************************************************************
void loop() {
delayPeriodMS = map(analogRead(PotInput ),0,1023,0,500);
// the following block is a bit shady, and works by coincidence - as there are nested conditions
// testing for true and false on the *same* variables - it should be rewritten if possible.
// ideally as a state machine - but this works with blocking delays
// ----------------------------------------------------------------------------
if (digitalRead(RightTurnSwitch) || digitalRead(LeftTurnSwitch)) { // a switch is 'on'
// ----------------------------------------------------------------------------
// LEFT-RIGHT TURN INDICATORS
// ----------------------------------------------------------------------------
if (!Rstate) { // If lights are already on, skip turning them on again
delay(20); // short pause to allow read/writes to proceed without interference
// when Right signal goes High, actuate R outputs in sequence
if (digitalRead(RightTurnSwitch)==HIGH && digitalRead(LeftTurnSwitch)==LOW) {
Rstate=1;
for (int counter = 0; counter <= 2; counter++) {
digitalWrite(RightLED[counter], HIGH);
delay(delayPeriodMS);
}
}
}
// ----------------------------------------------------------------------------
if (!Lstate) { // If lights are already on, skip turning them on again
delay(20); // short pause to allow read/writes to proceed without interference
// when Left signal goes High, actuate L outputs in sequence
if (digitalRead(RightTurnSwitch)==LOW && digitalRead(LeftTurnSwitch)==HIGH) {
Lstate=1;
for (int counter = 0; counter <= 2; counter++) {
digitalWrite(LeftLED[counter], HIGH);
delay(delayPeriodMS);
}
}
}
// ----------------------------------------------------------------------------
// BRAKE LIGHTS
// ----------------------------------------------------------------------------
if ((!Rstate) || (!Lstate)) { // If lights are already on, skip turning them on again
// when Brake (both) signal goes High, actuate R and L outputs in sequence
if (BRAKE_SWITCH) {
Rstate=1; Lstate=1;
for (int counter = 0; counter <= 2; counter++) {
digitalWrite(RightLED[counter], HIGH);
digitalWrite(LeftLED[counter], HIGH);
delay(delayPeriodMS);
}
}
}
} else { // no switches are on
// ----------------------------------------------------------------------------
// turn OFF ALL LIGHTS
// ----------------------------------------------------------------------------
// when signal goes LOW turn off all outputs
for (int counter = 0; counter <= 2; counter++) {
digitalWrite(RightLED[counter], HIGH); Rstate=0;
digitalWrite(LeftLED[counter], HIGH); Lstate=0;
}
}
}
// *********************************************************************************
// end
MODIFIED PotInput within the analogRead() call.
Only done for completeness