Traffic light project (Make blink without using delay function)

Hi folks,

I'm new to this forum and still a novice in Arduino programming, pardon me if I am unable to understand some of the terms :confused:

There's this project involving a traffic light system which uses 4 outputs, RED light, AMBER light, GREEN light & RIGHT TURN light.

The programming sequence:

  1. RED ON for 1 second
  2. RED switches to GREEN, GREEN ON for 10 seconds
  3. GREEN switches to AMBER, AMBER ON for 2 seconds
  4. AMBER switches to RED, RED ON for 1 seconds
  5. RED switches to GREEN & RIGHT TURN, both ON for 4 seconds
  6. GREEN switches to AMBER and AMBER ON, RIGHT TURN starts to blink 3 times, with 1 second OFF 1 second ON(ON OFF ON OFF ON OFF)
  7. AMBER OFF and RIGHT TURN BLINK OFF
  8. Repeat step 1 again

I have no problem with any of the sequence except with the RIGHT TURN blink. The thing is, I am suppose to use only the Millis function to perform the blinking. I had a clue which said I have to use the following code below, but just where to place it is the problem.

const int ledPin=13;
int ledState=0;
long previousMillis=0;
long interval=1000; //1 second interval
void setup()
{
pinMode(ledPin,OUTPUT);
}
void loop()
{
unsigned long currentMillis=millis();
if(currentMillis - previousMillis > interval)
{

  • previousMillis=currentMillis;*

  • if(ledState==0)*

  • ledState=1;*

  • else*

  • ledState=0;*

  • digitalWrite(ledPin, ledState);*
    }
    }

Please enlighten me on this issue. Do let me know if there's anything not understandable. Any other alternative code suggestions will be welcomed too. Quite an urgent matter.

Thanks in advance

Have you got the LEDs connected and can you turn them on & off individually? Can you make the the program work using the delay() function?

What's the problem with RIGHT TURN blink? To me that doesn't look any trickier than any of the other steps...

It might help if you post your actual code instead of the "hint". And when you post your code, click the icon that looks like </> to enclose your code in "code tags".

Well this is a school assignment and I'm supposed to program it to how the lecturer instructed me to. Yes I can turn the LED on and off individually using "digitalWrite(LOW/HIGH);" function. But to make it blink, I'm only entitled to use the Millis function to make it blink else I will get some of my marks minus off.

[code]
const int RED=13;
const int AMBER=12;
const int GREEN=11;
const int RIGHT_TURN=9;
int ledState=0;
long previousMillis=0;
long interval=1000;

void setup()
{
pinMode(RED,OUTPUT);
pinMode(AMBER,OUTPUT);
pinMode(GREEN,OUTPUT);
pinMode(RIGHT_TURN,OUTPUT);
}

void loop()
{
digitalWrite(RED, HIGH);
digitalWrite(AMBER, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(RIGHT_TURN, LOW);
delay(1000);

digitalWrite(RED, LOW);
digitalWrite(AMBER, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(RIGHT_TURN, LOW);
delay(8000);

digitalWrite(RED, LOW);
digitalWrite(AMBER, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(RIGHT_TURN, LOW);
delay(2000);

digitalWrite(RED, HIGH);
digitalWrite(AMBER, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(RIGHT_TURN, LOW);
delay(1000);

digitalWrite(RED, LOW);
digitalWrite(AMBER, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(RIGHT_TURN, HIGH);
delay(4000);

digitalWrite(RED, LOW);
digitalWrite(AMBER, HIGH);
digitalWrite(GREEN, LOW);

{
unsigned long currentMillis=millis();

if(currentMillis - previousMillis > interval)
{
  previousMillis=currentMillis;
  
  if(ledState==0)
  ledState=1;
  else
  ledState=0;
  
  digitalWrite(RIGHT_TURN, ledState);
}

}

delay(6000);
digitalWrite(RED, LOW);
digitalWrite(AMBER, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(RIGHT_TURN, LOW);
}

[/code]

From this program, everything works except for the blink. I am pretty sure the Millis code will blink the RIGHT TURN LED only if it goes on a loop but in this program, it's been obstructed by my other LED light function before the Millis code can join back again after a round.

Any way to go around the code to ensure the RIGHT TURN LED gets to blink 3 times only?

Looking forward to your advice :slight_smile:

Have a look at the code in several things at a time. It blinks 3 leds using millis()

...R