Traffic Light Controller without delay

hey guys,

for school I’ve to make a traffic light controller without any use of delay and I’ve some problems with it.

My code so far:

#define ON    HIGH         
#define OFF   LOW         
#define RED   11         
#define YLW   12       
#define GRN   13        
#define BTN   7  

bool ledState = OFF;
unsigned long previousMillis = 0; 
int counter = 0;

byte trafficLightPhase = 0;

void setup() {
  pinMode(RED, OUTPUT); 
  pinMode(YLW, OUTPUT);
  pinMode(GRN, OUTPUT);

  // pinMode(BTN, INPUT_PULLUP); // LOW = not pressed | HIGH = pressed
  
  Serial.begin(9600);                
  Serial.println("START");
}

void loop() {
  trafficLightCar(); 
}

void trafficLightCar() {
  switch(trafficLightPhase) {
    case 0:
    	trafficLightFlash(GRN, 1000L);
        trafficLightPhase = 1;
    break;
    case 1:
    	trafficLightFlash(YLW, 1000L);
    	trafficLightPhase = 0;
    break;
  }
}

void trafficLightFlash(char ledColor, int ledDuration) {
  
  unsigned long lastMillis;
  unsigned long currentMillis = millis();
  
  Serial.print(ledDuration);
  Serial.println(ledColor);
  digitalWrite(ledColor, ON);
  
  if (currentMillis - lastMillis >= ledDuration) {
    lastMillis = millis();
    digitalWrite(ledColor, OFF);
  }
}

void trafficLightBlink(char ledColor, int blinkCount, int blinkFlashOn, int blinkFlashOff) {
  
  int blinkCount2 = blinkCount * 2; 
  unsigned long currentMillis = millis();
    
  if((ledState == HIGH) && (counter < blinkCount2) && (currentMillis - previousMillis >= blinkFlashOn)) {
   	ledState = !ledState;  // Turn it off
    previousMillis = currentMillis;  // Remember the time
    digitalWrite(ledColor, ledState);  // Update the actual LED
    counter++;
  }
  else if ((ledState == LOW) && (counter < blinkCount2) && (currentMillis - previousMillis >= blinkFlashOff)) {
   	ledState = !ledState;  // turn it on
   	previousMillis = currentMillis;   // Remember the time
   	digitalWrite(ledColor, ledState);    // Update the actual LED
  	counter++;
  }
}

As seen there are two functions for each LED state (trafficLightFlash and trafficLightBlink) and one for the whole traffic light for the loop function, where I’ll later add the button for activating the traffic light. (no help needed for this so far).

Now, I’ve tried to add a switch case statement, which switches betwen the states of the traffic light (RED → RED&YLW → GRN, etc)
My main problem now is that all cases are activated at the same time. How can I modify the code in a way where between each case it’ll wait till the functions is “over”.

I may have a fallacy in the whole code structure, but I would appreciate any help.

I'm not clear if your code is meant to stay in eiter state for some length of time - for example to do several flashes or several blinks?

As it is written as soon as the function trafficLightFlash() completes one iteration the whole thing will move on to CASE 1

Also, you don't seem to have any place to set the start value for lastMillis. It would probably be easiest to make it a global variable like previousMillis.

...R