Go Down

Topic: Traffic light code (beginner level coding) (Read 11388 times) previous topic - next topic

ThunderBob

Feb 23, 2011, 09:18 pm Last Edit: Feb 25, 2011, 06:38 pm by ThunderBob Reason: 1
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 =)

Code: [Select]
// 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

Code: [Select]
// 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; }
   
 
 
}

Valalvax

#1
Feb 23, 2011, 09:52 pm Last Edit: Feb 24, 2011, 05:41 pm by Valalvax Reason: 1
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

Onions

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.
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

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
My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) :D

Valalvax


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)

ThunderBob

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

seckela

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.
Damn the Man, Save the Empire

ThunderBob

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

Code: [Select]
// 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;
 
 
}}}

Funnell7

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…

seckela

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
Damn the Man, Save the Empire

seckela

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

Funnell7

#11
Feb 25, 2011, 12:53 pm Last Edit: Feb 25, 2011, 04:14 pm by Funnell7 Reason: 1
@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
???

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 ;).

ThunderBob

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 =) }

seckela

#14
Feb 25, 2011, 07:51 pm Last Edit: Feb 25, 2011, 07:56 pm by seckela Reason: 1
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.
Damn the Man, Save the Empire

Go Up