Traffic Lights

Start the Arduino Integrated Development Environment (IDE), click on the "File" menu item at the top left, open example sketches (programs), and open the "Blink" example as you did in the first assignment.

Here is the sketch you should be seeing:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

The text that is in between /* and */ is the header comment. It is ignored by the Arduino microcontroller, but it helps the human programmer understand what the sketch is for. You should get into the habit of writing your own header at the beginning of every sketch you create. Change the header comment right now to make it your own for this assignment. The name of the new sketch will be "Traffic Lights", and it will turn on and off two sets of three LEDs to imitate the traffic lights at an intersection of two roads. You should replace "This example code is in the public domain." with your name and the current date.

Now that you have created a device to turn one LED on and off, this assignment is to expand the device to control six LEDs - the red yellow and green lights for two different directions at an intersection.

Start out by writing the requirements for the traffic lights at an intersection. Think about your previous experience with traffic lights, or go and observe the actual traffic lights at a simple intersection (not including special left turn signals.) What is the sequence of lights to be turned on and off? How much time is there between each step?

In the first section of the sketch, you will have to create six different names for the LEDs and assign them the numbers of six different digital output pins (you could use 8 to 13). In the second section, the setup routine, you need to initialize each of the six output pins for output. In the third section, the loop, you need to turn on and off the right combination of lights for appropriate times. You may use shorter times than is usual for real traffic lights so that testing doesn't take a very long time, but you should be able to explain how the numbers you use are related to the real times.

Groups need to work together to set up the Arduino, breadboard and LEDs, but each student must write their own sketch.

Use comments (words following // ) to explain how your sketch works. Make sure you save your sketch frequently.

Write a short essay in a web page about traffic lights, describing why they are important for driver and pedestrian safety. Investigate and discuss some of the concerns about traffic light timing such as why the light turning green should not take place at exactly the same time as the opposing light is turning red. Discuss (and add them to your device if you can) some other traffic light properties such as flashing green for left turns, walk/don't walk signals, pedestrian buttons, etc. What differences are there in other provinces or other countries?

Evaluation:

Level 1: Some lights work but with mistakes. Some description in essay.
Level 2: All lights work. Some description in comments and essay.
Level 3: All lights work properly. Useful comments. Good description of requirements and safety concerns.
Level 4-: All lights work properly. Useful comments. Excellent essay with requirements, safety concerns and at least one extra feature added.
Level 4+: All lights and extras work properly. Useful comments. Outstanding essay with extra features linked to safety concerns.

Next assignment

Back to the main menu.