I did this project to learn millis(). Two traffic lights with Two pedestrian lights and buttons. The traffic works well. There is a small timing issue in the main loop every 7 to 9 cycles. I added some resets to the lights to correct this issue. I would like to get feedback on this code of mine. It works very well, but know it can be better. I am very new to Arduino. I have about 2 months into this rabbit hole that I am loving. Please if anyone give me some improvements within the code. I would appreciate it.
int northLed[] = { 13, 12, 11 }; //13 red 12 yellow 11 green
int northWalking[] = { 10, 9 }; // 10 red 9 green
int southLed[] = { 2, 3, 4}; // 4 red 3 yellow 2 green
int southWalking[]{ 5, 6 }; // 5 green 4 red
int NLT, SLT; // Led Counters
int nWRT, sWRT; // pedestrian counters
unsigned long southcurrentTime; // assigned to millis()
unsigned long southoldTime; // assigned to millis()
const int southTimeEvent = 3000; // time millis() for Red and Green lights 3sec
const int southGreenEvent = 1500; // time millis() needed to exit yellow loop 1.5sec
bool SredLightOn = true; // exit and start the loop with millis()
bool SyellowLightOn = false;
bool SgreenLightOn = false;
unsigned long northcurrentTime;
unsigned long northoldTime;
const int northTimeEvent = 3000; // time millis() for Red and Green lights 3sec
const int northGreenEvent = 1500; // time millis() needed to exit yellow loop 1.5sec
bool NredLightOn = true; // exit and start the loop with millis()
bool NyellowLightOn = false;
bool NgreenLightOn = false;
int buttonN = 8; // north ligh button
int buttonS = 7; // south light button
bool runTime = true; // allows for one side of the north or southLightContol to run oposite of each other.
int trafficRunTime= 9100; // time needed to run through either north or southLightControl function.
unsigned long trafficOldTime; // assigned to millis()
void setup() {
//enable all pins
for (NLT = 0; NLT <= 2; NLT++) {
pinMode(northLed[NLT], OUTPUT);
}
for (SLT = 0; SLT <= 2; SLT++) {
pinMode(southLed[SLT], OUTPUT);
}
for (nWRT = 0; nWRT <= 1; nWRT++) {
pinMode(northWalking[nWRT], OUTPUT);
}
for (sWRT = 0; sWRT <= 1; sWRT++) {
pinMode(southWalking[sWRT], OUTPUT);
}
// enable button pins
pinMode(buttonN, INPUT_PULLUP);
pinMode(buttonS, INPUT_PULLUP);
// starts lights in the correct order traffic lights to red and the pedestrain to green
digitalWrite(northLed[0], HIGH);
digitalWrite(southLed[2], HIGH);
digitalWrite(northWalking[1], HIGH);
digitalWrite(southWalking[0], HIGH);
Serial.begin(9600);
}
void loop() {
int nWalkingRunTime = digitalRead(buttonN); //pedestrain button read for while()
int sWalkingRunTime = digitalRead(buttonS);
unsigned long trafficCurrentTime = millis(); // starts time
if (trafficCurrentTime - trafficOldTime >= trafficRunTime) {
trafficOldTime = trafficCurrentTime; // sets the if statement for main loop
if (runTime == true) { // used for turning off and on north/southLightControl
runTime = false;
} else {
runTime = true;
}
}
if (runTime == true) {
for (NLT = 0; NLT <= 2; NLT++) { // used to reset northLightControl
digitalWrite(northLed[NLT], LOW);
}
for (nWRT = 0; nWRT <= 1; nWRT++) { // used to reset North Pedestrian light
digitalWrite(northWalking[nWRT], LOW);
}
digitalWrite(northLed[0], HIGH); // part of North light reset
digitalWrite(northWalking[1], HIGH); // part of Pedestrian reset
southLightControl(); // first main function loop
} else {
for (SLT = 0; SLT <= 2; SLT++) { // used to reset southLightControl
digitalWrite(southLed[SLT], LOW);
}
for (sWRT = 0; sWRT <= 1; sWRT++) { // used to reset South Pedestrian light
digitalWrite(southWalking[nWRT], LOW);
}
digitalWrite(southLed[2], HIGH); // part of the South Light reset
digitalWrite(southWalking[0], HIGH); // part of the South Pedestrain Light reset
northLightControl(); // second main function loop
}
while (nWalkingRunTime == 0 || sWalkingRunTime == 0) { // pedestrain button delay() is used, no exit is needed safety first lol
walkLight();
return 0; // exit loop(probably not needed I am still learning)
}
}
void southLightControl() {
southcurrentTime = millis();
//start of the south light Red yellow and then green. bools are used to control the condition with millis().
if (southcurrentTime - southoldTime >= southTimeEvent && SredLightOn == true) {
southoldTime = southcurrentTime;
for (SLT = 0; SLT <= 2; SLT++) { // used to reset lights
digitalWrite(southLed[SLT], LOW);
}
for (nWRT = 0; nWRT <= 1; nWRT++) { //resets lights
digitalWrite(northWalking[nWRT], LOW);
}
digitalWrite(northWalking[1], HIGH); // resets North pedestrian light from Red to Green
digitalWrite(northWalking[0], LOW);
digitalWrite(southWalking[0], HIGH); // turn pedestrian lights from Red to Green
digitalWrite(southWalking[1], LOW);
digitalWrite(southLed[2], HIGH); // turn on Red light
SyellowLightOn = true; // controls the loop
SredLightOn = false;
}
//Yellow Light
if (southcurrentTime - southoldTime >= southTimeEvent && SyellowLightOn == true) {
southoldTime = southcurrentTime;
for (SLT = 0; SLT <= 2; SLT++) { // used to reset lights
digitalWrite(southLed[SLT], LOW);
}
digitalWrite(southWalking[1], HIGH); //turn pedestrian from Green to Red
digitalWrite(southWalking[0], LOW);
digitalWrite(southLed[1], HIGH); // turn on Yellow Light
SyellowLightOn = false; // controls the loop
SgreenLightOn = true;
}
//Green Light
if (southcurrentTime - southoldTime >= southGreenEvent && SgreenLightOn == true) {
southoldTime = southcurrentTime;
for (SLT = 0; SLT <= 2; SLT++) { // used to reset lights
digitalWrite(southLed[SLT], LOW);
}
digitalWrite(southWalking[1], HIGH); //turn pedestrian from Green to Red help with the reset of the exit of the while()
digitalWrite(southWalking[0], LOW);
digitalWrite(southLed[0], HIGH); // turn on Green Light
SredLightOn = true;
SgreenLightOn = false;
}
}
void northLightControl() {
northcurrentTime = millis();
//start of the north light Red yellow and then green. bools are used to control the condition with millis()
if (northcurrentTime - northoldTime >= northTimeEvent && NredLightOn == true) {
northoldTime = northcurrentTime;
for (NLT = 0; NLT <= 2; NLT++) { //resets the lights
digitalWrite(northLed[NLT], LOW);
}
for (sWRT = 0; sWRT <= 1; sWRT++) { //resets the lights
digitalWrite(southWalking[nWRT], LOW);
}
digitalWrite(southWalking[0], HIGH); //resets the south Pedestrian from Red to Green
digitalWrite(southWalking[1], LOW);
digitalWrite(northWalking[1], HIGH); // turns on Pedestrian from Red to Green
digitalWrite(northWalking[0], LOW);
digitalWrite(northLed[0], HIGH); // turns on Red Light
NyellowLightOn = true; // controls the loop
NredLightOn = false;
}
// Yellow Light
if (northcurrentTime - northoldTime >= northTimeEvent && NyellowLightOn == true) {
northoldTime = northcurrentTime;
for (NLT = 0; NLT <= 2; NLT++) { //resets the lights
digitalWrite(northLed[NLT], LOW);
}
digitalWrite(northWalking[0], HIGH); // turn on Pedestrian Green to Red
digitalWrite(northWalking[1], LOW);
digitalWrite(northLed[1], HIGH); // turn on Yellow Light
NgreenLightOn = true; // controls the loop
NyellowLightOn = false;
}
// Green Light
if (northcurrentTime - northoldTime >= northGreenEvent && NgreenLightOn == true) {
northoldTime = northcurrentTime;
for (NLT = 0; NLT <= 2; NLT++) { // reset lights
digitalWrite(northLed[NLT], LOW);
}
digitalWrite(northWalking[0], HIGH); //turn pedestrian from Green to Red help with the reset of the exit of the while()
digitalWrite(northWalking[1], LOW);
digitalWrite(northLed[2], HIGH); // turns on Green Light
NredLightOn = true; // controls the loop
NgreenLightOn = false;
}
}
void walkLight() {
// Pedestrain function
for (NLT = 0; NLT <= 2; NLT++) { //resets northLightControl
digitalWrite(northLed[NLT], LOW);
}
for (SLT = 0; SLT <= 2; SLT++) { // resets southLightControl
digitalWrite(southLed[SLT], LOW);
}
digitalWrite(northLed[0], HIGH); // turns on North Red Light part of reset
digitalWrite(southLed[2], HIGH); // turns on South Red Light
digitalWrite(northWalking[0], LOW); // Red Pedestrian Light
digitalWrite(southWalking[1], LOW);
delay(200); // delay enought to see the reset
digitalWrite(northWalking[1], HIGH); //Turns on Pedestrain light to green
digitalWrite(southWalking[0], HIGH);
delay(1500); // Pedestrian walking time, move it people
// no need to digtialWrite Low due to resets in the code
}