Go Down

Topic: Traffic lights code for beginners (Read 7247 times) previous topic - next topic

inicko

Hello dear readers! :)

I just wanted to post a code of a traffic light control, which easily is built on a breadboard.
This is my first post to hopefully help others that are beginners like my self.  :smiley-mr-green:

Anyway..,
You only need 3 leds (red, green & yellow), 3 resistors ? 330?, some wire & an Arduino.

If you need help on how to build the circuit, just click on the link in the code below.

I can post a screenshot of how I built my circuit if requested. Cheers!

Code: [Select]
/*
Trafficlights made basic, by inicko. I made a few changes from; [url]www.makeuseof.com/tag/traffic-light-controller/[/url]   |
<_________________________>----------------------------------------------------------------------------|
-------------------------|
the loop now goes:      |        I found my changes made a more reallistic light pattern than
yellow & red to red     |        the original code. At least this is how the traffic lights are -
red to red & yellow     |        in Sweden as far as I know.
red & yellow to green   |     
green to green & yellow |      Hope you enjoy it!  I've just started with programming, so feel free to change anything
green & yellow to yellow|      if it's wrong or could be written in a better way etc.
yellow to red           |      It works the way I intended anyway, so I'm happy ;-)!
________________________|

*/

// defines the pin outputs as the colored leds for ease //

int red = 13;
int yellow = 12;
int green = 11;

// sets the pins 11-13 as outputs //
void setup(){
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
}

// creates the loop that your Arduino will repeat over & over //
void loop(){
  changeLights();
  delay(3000);
}

// the commands that make the leds light up in a specific order //

void changeLights(){

  // turn off green, then turn yellow on for 2 seconds
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  delay(2000);

  // turn off yellow, then turn red on for 5 seconds
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  delay(5000);

  // red & yellow on for 2 seconds (red already on)
  digitalWrite(yellow, HIGH);
  delay(2000);

  // green on for 5 seconds, turns off red & yellow
  digitalWrite(yellow, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(red, LOW);
  delay(5000);

  // turn on yellow (green already on)
  digitalWrite(yellow, HIGH);
  delay(100);
}





Nikarus

Whats next with this code, adding ultrasonic rangefinders to see the traffic situation so you don't drive up to a red light in the middle of the night with no cars around?

inicko

I was rather thinking of connecting a nuclear bomb that detonates when someone walks or drives when the lights are red

Nikarus

Seems a tadbit costly. Maybe it just deploys a spikestrip under their tires suddenly. But then again, how would you account for legal right turns on red?

bench

where will i get 4way traffic light controller



inicko

Well you could either try coding it by your self with my code for a bit of guidance or simply google 4way traffic light.

You always find answers on google ;-)!

sir? will you please give the schematic diagram of traffic light?

thank you very much Sir.


Godbless   :)

CGwyther

Here is my version it's set up to behave in the British style two sets of lights working in a complimentary manner, your welcome to build on it e.g. four way :-

/*
British trafficlights developed by ConradG based on the program Trafficlights made basic, by inicko. I made a few changes from; www.makeuseof.com/tag/traffic-light-controller/   |
<_________________________>----------------------------------------------------------------------------|
-------------------------|
the loop now goes:      |        I found my changes made a more reallistic light pattern than
yellow & red to red     |        the original code. At least this is how the traffic lights are -
red to red & yellow     |        in Sweden as far as I know.
red & yellow to green   |     
green to green & yellow |      Hope you enjoy it!  I've just started with programming, so feel free to change anything
green & yellow to yellow|      if it's wrong or could be written in a better way etc.It works the way I intended anyway, so I'm happy ;-)!
yellow to red           |     
________________________|      CGwyther Mine operates opposeing lights to control oppersite flow at narrows e.g. old narrow bridge. Yellow uses common pin (= 12) others have own pin.

*/

// defines the pin outputs as the colored leds for ease //

int red = 13;
int yellow = 12;
int green = 11;
int grn = 10;
int rd = 9;

// sets the pins 11-13 as outputs //
void setup(){
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(rd, OUTPUT);
  pinMode(grn, OUTPUT);
 
}

// creates the loop that your Arduino will repeat over & over //
void loop(){
  changeLights();
  delay(2000);
}

// the commands that make the leds light up in a specific order //

void changeLights(){

  // turn off green, then turn yellow on for 2 seconds
  digitalWrite(green, HIGH);
  digitalWrite(rd, HIGH);
  delay(2000);
 
  // turn off green, then turn yellow on for 2 seconds
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  delay(2000);

  // turn off yellow, then turn red on for 5 seconds
  digitalWrite(yellow, LOW);
  digitalWrite(rd, LOW);
  digitalWrite(red, HIGH);
  digitalWrite(grn, HIGH);
  delay(4000);

  // red & yellow on for 2 seconds (red already on)
  digitalWrite(grn, LOW);
  digitalWrite(yellow, HIGH);
  delay(2000);

  // green on for 5 seconds, turns off red & yellow
  digitalWrite(red, LOW);
  digitalWrite(yellow, LOW);
  digitalWrite(green, HIGH);
  digitalWrite(rd, HIGH);
  delay(2000);

}

 

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy