(this activity uses thinking and inquiry skills )
Learning Goals:
Students will consolidate their skills in:
controlling LEDs with the Arduino
uploading programs to the Arduino
building simple LED circuits on a breadboard
controlling Arduino timing using delay()
Challenge Activity
Use your knowledge of typical traffic signal lights and what you have learned about controlling LEDs with the Arduino to wire three LEDs one red, one green, and one yellow. Write an Arduino Sketch to simulate the normal operation of traffic lights. A normal traffic light cycle may take up to 2 minutes you should shorten the cycle to 30 or 45 seconds but try to approximate the proportional time for red yellow and green.
Guided Activity
The following steps will guide you through extending the blink program into a traffic light simulation, by copying portions of code to reuse and expand the program.
Start by opening the blink program from Arduino built-in examples or opening your blink sketch from the first activity. In this activity, we will modify the previous sketch to create your stoplight program. Text highlighted in red is the item being discussed, comments in bold italics, instructions for you in green, the older code will stay black.
Click image to enlarge
STEP 1
/* The /* and */ mark the beginning and end of a block comment.
The comment text is not part of the program but provides the programmer with information about the program.
Block comments at the start of the program act as the title page
Blink Change the comment lines to explain your new program
Turns on an LED for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
The int keyword creates a variable to store an integer
//int create a space in memory to store an integer call the space redLed and store the number 13 in the space.
int redLed = 13;
Create 2 more integer variables for the green and yellow LEDs
void setup() {
// initialize the digital pin as an output.
pinMode(redLed, OUTPUT); // connects the redLed pin (13) to the output circuitry in the Arduino
Create two more pinMode statements for the Green and Yellow LEDs
}
void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
STEP 2
Your sketch should now look more like this
/*
This program will simulate a traffic light
Turing on each LED in sequence
This Code was written by Mr. M Quosai for computer technology TEJ 2OI
Nov 2011
*/
int redLed = 13;
int greenLed = 12;
int yellowLed = 11;
void setup() {
// initialize the digital pin as an output.
pinMode(redLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
}
void loop() {
digitalWrite(redLed, HIGH); // set the LED on
delay(1000); // wait for a second
The digitalWrite keyword sends HIGH (1) or LOW(0) signal to one data pin Add 4 more digitalWrite statements to turn on and off the green and yellow LEDs
digitalWrite(redLed, LOW); // set the LED off
delay(1000); // wait for a second
}
STEP 3 Your sketch should now look more like this
try the Sketch like this
the red and green LEDs will appear to work properly but the yellow will blink very shortly you may not see it at all.
add or remove delays and adjust the timing to simulate a normal traffic light.
/*
This program will simulate a traffic light
Turing on each LED in sequence
This Code was written by Mr. M Quosai for computer technology TEJ 2OI
Nov 2011
*/
int redLed = 13;
int greenLed = 12;
int yellowLed = 11;
void setup() {
// initialize the digital pin as an output.
pinMode(redLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
}
void loop() {
digitalWrite(yellowLed, LOW); // set the LED off
digitalWrite(redLed, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(redLed, LOW); // set the LED off
digitalWrite(greenLed, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(greenLed, LOW); // set the LED off
digitalWrite(yelowLed, HIGH); // set the LED on
Step 4 At this point, you save a functional program that controls three LEDs simulating a traffic light. It should look like this.
/*
This program will simulate a traffic light
Turing on each LED in sequence
This Code was written by Mr. M Quosai for computer technology TEJ 2OI
Nov 2011
*/
int redLed = 13;
int greenLed = 12;
int yellowLed = 11;
void setup() {
// initialize the digital pin as an output.
pinMode(redLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
}
void loop() {
delay(5000); // wait for a second
digitalWrite(yellowLed, LOW); // set the LED off
digitalWrite(redLed, HIGH); // set the LED on
delay(15000); // wait for a second
digitalWrite(redLed, LOW); // set the LED off
digitalWrite(greenLed, HIGH); // set the LED on
delay(10000); // wait for a second
digitalWrite(greenLed, LOW); // set the LED off
digitalWrite(yelowLed, HIGH); // set the LED on
}
Step 5 Wait a minute! if you have copied the circuit above when you run the code it says there are some errors. Arduino is very particular if you use yellowLed in one place and yelowLed or Yellowed in another it sees each of them as a different word. it is a good habit to check your code often, even every time you enter a line. Can you find the error?