Feb 28, 2013


We are now going to create a set of traffic lights that will change from green to red, via amber, and back again, after a set length of time using the 4-state system. This project could be used on a model railway to make a set of working traffic lights or for a childs toy town.we have connected 3 LEDs with the Anode of each one going to Digital Pins 8, 9 and 10, via a 220ohm resistor each.

We have taken a jumper wire from Ground to the Ground rail at the top of the breadboard and a ground wire goes from the Cathode leg of each LED to the common ground rail.

Enter the code
Enter the following code, check it and upload.
int ledDelay = 10000; // delay in between changes
int redPin = 10;
int yellowPin = 9;
int greenPin = 8;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
void loop() {
// turn the red light on
digitalWrite(redPin, HIGH);
delay(ledDelay); // wait 5 seconds
digitalWrite(yellowPin, HIGH); // turn on yellow
delay(2000); // wait 2 seconds
digitalWrite(greenPin, HIGH); // turn green on
digitalWrite(redPin, LOW); // turn red off
digitalWrite(yellowPin, LOW); // turn yellow off
delay(ledDelay); // wait ledDelay milliseconds
digitalWrite(yellowPin, HIGH); // turn yellow on
digitalWrite(greenPin, LOW); // turn green off
delay(2000); // wait 2 seconds
digitalWrite(yellowPin, LOW); // turn yellow off
// now our loop repeats
In upcoming posts we will see about how to make this project more interactive.

No comments:

Post a Comment