Hello rccraze
Here comes my suggestion coded in C++ for your traffic light control. I borrowed the sequence from LarryD.
This sequence can be easily changed and added in the array without touching the rest of the code.
TRAFFICLIGHT trafficLights[]
{
//E E N N W W S S
//R G R G R G R G light time
{{0, 1, 1, 0, 1, 0, 1, 0}, 10000, 0, true},
{{1, 1, 1, 0, 1, 0, 1, 0}, 10000, 0, false},
{{1, 0, 0, 1, 1, 0, 1, 0}, 10000, 0, false},
{{1, 0, 1, 1, 1, 0, 1, 0}, 10000, 0, false},
{{1, 0, 1, 0, 0, 1, 1, 0}, 10000, 0, false},
{{1, 0, 1, 0, 1, 1, 1, 0}, 10000, 0, false},
{{1, 0, 1, 0, 1, 0, 0, 1}, 10000, 0, false},
{{1, 0, 1, 0, 1, 0, 1, 1}, 10000, 0, false},
};
For each sequence you can specify a traffic light time.
Check it, test it and make changes for your needs.
/* BLOCK COMMENT
ATTENTION: This Sketch contains elements of C++.
https://www.learncpp.com/cpp-tutorial/
Many thanks to LarryD
https://aws1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
https://forum.arduino.cc/t/1920s-traffic-light-help/1058420
Tested with Arduino: Mega[ ] - UNO [x] - Nano [ ]
*/
#define ProjectName "1920’s traffic light"
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
// copy from larryD
// E E N N W W S S
// R G R G R G R G
constexpr byte LedPins[] {2, 3, 4, 5, 6, 7, 8, 9}; // portPin o---|220|---|LED|---GND
constexpr unsigned long Blink512Hz {9}; // blinkrate for heartbeat function
#define OutPutTest
constexpr unsigned long OutPutTestTime {100};
unsigned long currentTime;
struct TIMER
{
const unsigned long Duration;
int unsigned long stamp;
int onOff;
};
struct TRAFFICLIGHT
{
int pattern[8];
TIMER patternTime;
};
/* copy from larryD
//E E N N W W S S
//R G R G R G R G
//0 1 2 3 4 5 6 7 Element
{ 0, 1, 1, 0, 1, 0, 1, 0}, //0 /
{ 1, 1, 1, 0, 1, 0, 1, 0}, //1 |
{ 1, 0, 0, 1, 1, 0, 1, 0}, //2 | AKA LEDsequence #s
{ 1, 0, 1, 1, 1, 0, 1, 0}, //3 <
{ 1, 0, 1, 0, 0, 1, 1, 0}, //4 | 1 = LED ON, 0 = LED OFF
{ 1, 0, 1, 0, 1, 1, 1, 0}, //5 |
{ 1, 0, 1, 0, 1, 0, 0, 1}, //6 |
{ 1, 0, 1, 0, 1, 0, 1, 1} //7 \
//
*/
TRAFFICLIGHT trafficLights[]
{
//E E N N W W S S
//R G R G R G R G light time
{{0, 1, 1, 0, 1, 0, 1, 0}, 10000, 0, true},
{{1, 1, 1, 0, 1, 0, 1, 0}, 10000, 0, false},
{{1, 0, 0, 1, 1, 0, 1, 0}, 10000, 0, false},
{{1, 0, 1, 1, 1, 0, 1, 0}, 10000, 0, false},
{{1, 0, 1, 0, 0, 1, 1, 0}, 10000, 0, false},
{{1, 0, 1, 0, 1, 1, 1, 0}, 10000, 0, false},
{{1, 0, 1, 0, 1, 0, 0, 1}, 10000, 0, false},
{{1, 0, 1, 0, 1, 0, 1, 1}, 10000, 0, false},
};
void heartBeat(int rate)
{
bool myBlink {currentTime & bit(rate)};
digitalWrite(LED_BUILTIN, myBlink);
}
// -------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(__FILE__);
Serial.print(F("Date : ")), Serial.println(__DATE__);
Serial.print(F("Project: ")), Serial.println(ProjectName);
pinMode (LED_BUILTIN, OUTPUT); // used as heartbeat indicator
// https://www.learncpp.com/cpp-tutorial/for-each-loops/
for (auto LedPin : LedPins) pinMode(LedPin, OUTPUT);
#ifdef OutPutTest
// check outputs
Serial.print(F("led test"));
for (auto LedPin : LedPins) Serial.print(F(".")),digitalWrite(LedPin, HIGH), delay(OutPutTestTime);
for (auto LedPin : LedPins) Serial.print(F(".")),digitalWrite(LedPin, LOW), delay(OutPutTestTime);
Serial.print(F("done\n"));
#endif
}
void loop ()
{
currentTime = millis();
heartBeat(Blink512Hz);
for (auto &trafficLight : trafficLights)
{
if (currentTime - trafficLight.patternTime.stamp >= trafficLight.patternTime.Duration && trafficLight.patternTime.onOff)
{
static int patternCounter = 0;
int element=0;
trafficLight.patternTime.onOff = false;
for (auto LedPin : LedPins) digitalWrite(LedPin, trafficLights[patternCounter].pattern[element++]);
trafficLights[patternCounter].patternTime.onOff = true;
trafficLights[patternCounter].patternTime.stamp = currentTime;
if (++patternCounter == (sizeof(trafficLights) / sizeof(trafficLights[0]))) patternCounter = 0;
}
}
}