Project 3 – Traffic Lights

You 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 four-state UK system. This project could be used to make a set of working traffic lights for a model railway or for a child's toy town. If you're not from the UK, you can modify the code and colors to make them work like the traffic lights in your own country. First, though, make the project as it is and change it once you know how it works.

Parts Required

Breadboard images

Red Diffused LED images

Yellow Diffused LED images

Green Diffused LED images

3 × 150 ohm Resistors* images

Jumper Wires images

*or whatever value you require for your type of LED

Connect It Up

Connect your circuit as shown in Figure 2-6. This time you connect three LEDs with the anode of each one going to Digital Pins 8, 9 and 10 via a 150Ω resistor (or whatever value you require) each.

Take a jumper wire from ground of the Arduino to the ground rail at the top of the breadboard; a ground wire goes from the Cathode leg of each LED to the common ground rail via a resistor—this time connected to the cathode. (For this simple circuit, it doesn't matter if the resistor is connected to the anode or cathode).

image

Figure 2-6. The circuit for Project 3 – Traffic Lights (see insert for color version)

Enter the Code

Enter the code from Listing 2-3, check it, and upload to your Arduino. The LEDs will now move through four states that simulate the UK traffic light system, as seen in Figure 2-7. If you have followed Projects 1 and 2, both the code and the hardware for Project 3 will be self-explanatory. I shall leave you to examine the code and figure out how it works.

Listing 2-3. Code for Project 3

// Project 3 - Traffic Lights

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() {
  
        digitalWrite(redPin, HIGH); // turn the red light on
        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
  
}
image

Figure 2-7. The four states of the UK traffic light system (image by Alex43223 from WikiMedia) (see insert for color version)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.97.53