Traffic light code (beginner level coding)

Hello all!
I'm a new member to the arduino community.
I received my duemilanove yesterday in the mail and she is indeed a beauty.

This morning I started familiarizing myself with coding and decided "why not make my own?".
After an hr or 2 (lost count) of trial and error, I finally came up with this short traffic light LED code.
Try it for yourself =)

// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13
// Connect yellow LED to pin 12
// Connect red LED to pin 11

void setup()  { 
  // declare pin 11,12,13 to be outputs:
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
} 

void loop(){{
   digitalWrite(13, HIGH); // Turns LED on pin 13 on
 delay(2500); // LED on pin 13 remains on for 5 seconds
   digitalWrite(13, LOW); //  Turns LED on pin 13 off
 delay(0);}
   digitalWrite(12, HIGH); // Turns LED on pin 12 on
 delay(2500); // LED on pin 12 remains on for 5 seconds
   digitalWrite(12, LOW); //  Turns LED on pin 12 off
 delay(0);
   digitalWrite(11, HIGH); // Turns LED on pin 11 on
 delay(2500); // LED on pin 11 remains on for 5 seconds    
   digitalWrite(11, LOW); //  Turns LED on pin 11 off
 delay(0);
}

Double lane code below

 // Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13 and 7
// Connect yellow LED to pin 12 and 6
// Connect red LED to pin 11 and 5

int var = 0;

void setup()  { 
  // declare pins 11,12,13,5,6,7 to be outputs:
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
} 

void loop(){{delay(1000);
  
 if (var == 0)
    digitalWrite(5, HIGH);
    delay(0);
    var = 1;
  
  if (var == 1)
    digitalWrite(13, HIGH);
    delay(0);
    var = 0; }
    
 { delay(7000);
    if (var == 0)
    digitalWrite(13, LOW);
    delay(0);
    var = 1; 
    
    
    if (var == 1)
    digitalWrite(12, HIGH);
    delay(0);
    var = 2;}
    
    {    
    delay(5000);
    if (var == 2)
    digitalWrite(11, HIGH);
    delay(0);
    var = 2;
    
    if (var == 2)
    digitalWrite(12, LOW);
    delay(0);
    var = 3;
    }
    
   {  delay(3000);
     if (var == 3)
     digitalWrite(11, HIGH);
     delay(0);
     var = 4;
     
     if (var == 4)
     digitalWrite(5, HIGH);
     delay(0);
     var = 3;
     
      
   }{
    
    delay(3000);
    if (var == 3)
    digitalWrite(11, HIGH);
    delay(0);
    var = 2;
    
    if (var == 2)
    digitalWrite(5, LOW);
    delay(0);
    var = 2;
    
      
    if (var == 2)
    digitalWrite(7, HIGH);
    delay(0);
    var = 4;}
    
 {  delay(5000);
   if (var == 4)
    digitalWrite(11, HIGH);
    delay(0);
    var = 2;
    
    if (var == 2)
    digitalWrite(7, LOW);
    delay(0);
    var = 3; 
    
    if (var == 3)
    digitalWrite(6, HIGH);
    delay(0);
    var = 2; }
    
    { delay(3000);
      if (var == 2)
    digitalWrite(6, LOW);
    delay(0);
    var = 3; 
    
    if (var == 3)
    digitalWrite(5, HIGH);
    delay(0);
    var = 4;
    
    if (var == 4)
    digitalWrite(11, HIGH);
    delay(0);
    var = 3; }
    
    {delay(3000);
        if (var == 3)
    digitalWrite(5, HIGH);
    delay(0);
    var = 4;
    
    if (var == 4)
    digitalWrite(11, HIGH);
    delay(0);
    var = 3; }
    
    
    {delay(000);
    if (var == 3)
    digitalWrite(11, LOW);
    delay(0);
    var = 2;
    
    if (var == 2)
    digitalWrite (13, HIGH);
    delay(0);
    var = 3; }
    
  
  
}

Couple of corrections

delay(2500); actually delays for 2.5 seconds

delay(0); is not needed

void loop(){{
digitalWrite(13, HIGH); // Turns LED on pin 13 on
snip
delay(0);}
digitalWrite(12, HIGH); // Turns LED on pin 12 on
snip
}

Those red curly brackets are not needed

Please note that I'm not trying to discourage you or anything, I hope it didn't come out that way anyway.

Now a challenge for you:

Add a second lane, and change your light durations: Green for 7, Yellow for 3, Red for 10.

Time NS lane EW lane
0S Green Red
7S Yellow Red
10S Red Green
17S Red Yellow
20S Green Red

etc...

Try doing this without using delay();

And a couple of hints to get you started:

You can use If Then statements to get the Red Lights to light up while the opposite green or yellow lights are lit

The code looks good, but you have two {{ s after void loop(). You then have a } after delay(0);. If you wanted, you could remove this. (Remember to keep one at the start and one at the end for void loop() ). Also, the number inside the brackets for delay() is the ammount of milliseconds you want the code to pause for, so having delay(0) will not do anything. You put that " delay(2500); // LED on pin 12 remains on for 5 seconds". To stay on for 5 seconds, you will need to put delay(5000);
Well done on the code though, and carry on designing!

Onions.

You posted yours just as I was typing mine, Valalvax! Maybe if I did not stop half way through for some hot chocolate... XD

Onions:
You posted yours just as I was typing mine, Valalvax! Maybe if I did not stop half way through for some hot chocolate... XD

Lol, I was going to post "I BEAT YOU LOLOL IN UR FACE"

But decided against it...

A similar project using ladder logic gave me problems for hours... (course at the time they hadn't taught us about a lot of things, we only really had timers)

Thanks everyone.
I'm going to keep working on it. =D

I really like the idea of doing a double lane stop light setup.

I think it would be an even better project if you could set up 2 breadboards each with a Red Green and Yellow LED set on them. Then use an input devise such as a motion tracker on both boards. Taking a few if statements comparing motion on one compared to the other, with some delays, and an if then statement to make sure you don't "wreck cross traffic" you could make your own motion detecting intersection.

Here is my 2 lane traffic light system (took hours to complete)
Thank you Valalvax for the amazing idea.

LEDs on pins 13-11 control traffic in North and South directions
LEDS on pins 5 -7 control traffic in East and West directions

try it out for yourself =D

// Traffic light code designed by Marquez Santos! (on facebook)
// Connect green LED to pin 13 and 7
// Connect yellow LED to pin 12 and 6
// Connect red LED to pin 11 and 5

int var = 0;

void setup()  { 
  // declare pins 11,12,13,5,6,7 to be outputs:
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
} 

void loop(){{delay(1000);
  
 if (var == 0)
    digitalWrite(5, HIGH);
    delay(0);
    var = 1;
  
  if (var == 1)
    digitalWrite(13, HIGH);
    delay(0);
    var = 0; }
    
 { delay(7000);
    if (var == 0)
    digitalWrite(13, LOW);
    delay(0);
    var = 1; 
    
    
    if (var == 1)
    digitalWrite(12, HIGH);
    delay(0);
    var = 2;}
    
    {    
    delay(5000);
    if (var == 2)
    digitalWrite(11, HIGH);
    delay(0);
    var = 2;
    
    if (var == 2)
    digitalWrite(12, LOW);
    delay(0);
    var = 3;
    }
    
   {  delay(3000);
     if (var == 3)
     digitalWrite(11, HIGH);
     delay(0);
     var = 4;
     
     if (var == 4)
     digitalWrite(5, HIGH);
     delay(0);
     var = 3;
     
      
   }{
    
    delay(3000);
    if (var == 3)
    digitalWrite(11, HIGH);
    delay(0);
    var = 2;
    
    if (var == 2)
    digitalWrite(5, LOW);
    delay(0);
    var = 2;
    
      
    if (var == 2)
    digitalWrite(7, HIGH);
    delay(0);
    var = 4;}
    
 {  delay(5000);
   if (var == 4)
    digitalWrite(11, HIGH);
    delay(0);
    var = 2;
    
    if (var == 2)
    digitalWrite(7, LOW);
    delay(0);
    var = 3; 
    
    if (var == 3)
    digitalWrite(6, HIGH);
    delay(0);
    var = 2; }
    
    { delay(3000);
      if (var == 2)
    digitalWrite(6, LOW);
    delay(0);
    var = 3; 
    
    if (var == 3)
    digitalWrite(5, HIGH);
    delay(0);
    var = 4;
    
    if (var == 4)
    digitalWrite(11, HIGH);
    delay(0);
    var = 3; 
    
    {delay(000);
    if (var == 3)
    digitalWrite(11, LOW);
    delay(0);
    var = 3;
  
  
}}}

Looking good so far... Ready for another challenge? I've always thought about creating a similar 'traffic light' sequence (I'm a beginner too by the way)... and thought about adding a button as a pedestrian crossing. When the button is pushed, wait a few seconds then turn both traffic lights red. Once both lights are red, flash a different green Led for several seconds to signify that the pedestrians can cross, and then repeat… Just a thought…

Would the lights just resume cycle after the "Crosswalk" went off? Or would the loop just start from the beginning. I think it would be challenging code wise, but more intriguing to store the value of which intersections turn it was, and resume from there. Obviously we don't want to upset "traffic" by being unfair. Lol

PS: I must ask, why the extra { } set in there. There only needs to be 1 after void loop { delay(1000); .....etc }

@Seckela
Absolutely... that would be things to consider. I was thinking… say for example it took a total of 10 seconds for either the NS Lane or EW Lane to complete its cycle (from Red to Green and then back again). You could code it so when the pedestrian button is pushed, wait for the current cycle it is on to finish, then flash the pedestrian bit, and then resume with the other lane… i.e.
NS Lane – Currently on Green
Button Pushed (interrupt maybe?) - Wait for NS Lane to return to Red. Flash Pedestrian LED for x amount of time
Resume with EW Lane
???

You could also add a photoresistor for detecting whether a car is in front of the traffic lights (a car presumably blocks the light). A force sensor would be better but those are more expensive ;).

Yeah I like the push button idea much better because I actually have on lol.
I can't really spend much money now since I just recently bought the Arduino and a motor shield for it.
So I'll just use what I have.

@seckela: I used the extra {} because for some strange reason, without them lights won't obey the if statement commands and turn on
out of sequence and never turn off smh.

for example if I wrote

if (var == X)
digitalWrite (7, HIGH);
delay(0);
var = X;

if (var == X)
digitalWrite (7, LOW);
delay(0);
var = X;

all under 1 set of { }

then LEDpin 7 would remain on and never turn off no matter what variables I used.
{took a minute to figure that out =) }

So you never write your if statements without their own set pf { }

ie:

void loop() {
if (x == 0 && y == 0)
{
x = val;
}

else {
if ( x == val)
{
do something;
}
if (y == val)
{
do something else;
}
}
}

The green ones are the "capsule" for your if statement, the red ones being the one for your void loop. The Orange ones are just a show of repeated patter, giving each if statement a bracket group. Also I have all the bracket pairs color coded and an initial if else case in there to show that you can group larger if statements under others to dynamically search, or set variables. I sometimes use that for a button case. Its sloppy but it works something like this:

if (bval == HIGH)
{
if (mval == 0)
{
mval = mval+1;
}
else
{
mval = mval-1;
}
delay(500);
}
if (mval == 1)
{
analogWrite(LED1, 0);
analogWrite(LED2, 0);

}

it checks for a high signal from the button, then checks to see if the mval is 0 or 1 (1 being off, 0 being on) if its on the next case turns the LED set off, and if I put more code from it in, the next if would have checked for mval == 0 and then turned the LEDs on.

Meinaart:
You could also add a photoresistor for detecting whether a car is in front of the traffic lights (a car presumably blocks the light). A force sensor would be better but those are more expensive ;).

IRL they use coils these days, force sensors need replacing too often

So for the "crosswalk button with double lane traffic light" system what type of logic programming would I be using

more if statements or something else?

The only button I have is something similar to this and I only have one =/

Eventually when I'm done I'm going to build a miniature city using LEDs and hotwheels with this code lol

That button will work just fine. For the Logic you can use a case based system like this:

if (cWalkButton == HIGH)
{
delay(1000);
cWalk = 1;
}

if (cWalk == 1)

{ 
lane1 = 0;
lane2 = 0;
digitalWrite(cWalkLight, HIGH);
delay(500)
digitalWrite(cWalkLight, LOW);
delay(500)
digitalWrite(cWalkLight, HIGH);
delay(500)
digitalWrite(cWalkLight, LOW);
delay(500)
cWalk = 0;
}

else
{
//stoplight code here
}

This code could be really sloppy I just wrote it. I hope its understandable. Essentially, you want to set a case for the button to be on HIGH and that to trigger a set of events, other wise you want your lanes to be working as normal. Also remember you don't want the cross walk code to depend on the button constantly being HIGH because that means you'd have to hold it for the durration and it would just loop. You want 1 press to set the condition up, then once forfilled, it to reset to off.

ok and to define those terms you added I just place

int lane1 = 0;
int lane2 = 0;

at the top of the code right?

also wouldn't I have to use another pin as input such as

pinMode (4 , INPUT);

for the button and map it?

You don't HAVE to define it as an input (all pins are inputs by default) but I always do

With only one button you'll only have one lane with a crosswalk, all them other saps are screwed...

If you wanted to actually do it with hotwheels etc you'd need some way to stop the cars, cause they aren't going to obey traffic laws