If you are on this page then I'm assuming you have done the "Getting Your Brain (Arduino) to think" BLINK test and understand that SKETCH.
Now you will use your breadboard and Arduino to create a traffic light crossing.
STEPS:
TRAFFIC LIGHT SKETCH
/* This program mimics the operation of traffic lights.
It will teach you about creating more than one output on the Arduino.
Current limiting resistors must be used on the LEDs to keep current
below maximum for the LED and below 40mA limit of I/O of Arduino */
int greenled = 12; // green led pin number
int orangeled = 13; // orange led pin number
int redled = 14; // red led pin number
void setup()
{
pinMode(12, OUTPUT); //Pinmodes of the leds
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
}
void loop()
{
digitalWrite(redled, HIGH); //turns on red light
delay(5000); //waits 5000 ms (5 seconds)
digitalWrite(redled, LOW); //turns off red light
digitalWrite(greenled, HIGH); // turns on green light
delay(3000); //waits 3000ms (3 seconds)
digitalWrite(greenled, LOW); //turns off green light)
digitalWrite(orangeled, HIGH); // turns on orange light
delay(1000); // 2000 ms (2 seconds)
digitalWrite(orangeled, LOW); //orange light off
}
For this project we will now add a button switch. When this button is pressed the lights change to red allowing the pedestrian to cross.
/* This program will change the traffic light to red to allow Mr Woody to cross the road!*/
int red = 11;
int amber = 12;
int green = 13;
int button = 2;
void setup() {
pinMode (red, OUTPUT);
pinMode (amber, OUTPUT);
pinMode (green, OUTPUT);
pinMode (button, INPUT);
digitalWrite (green, HIGH); //start with green light ON
}
void loop () {
/*check for a push of the button*/
int state = digitalRead(button);
if (state == HIGH) {
changeLights(); // function to change lights
}
}
void changeLights () {
digitalWrite (green, LOW); //turn green light off
digitalWrite (amber, HIGH);//turn amber on
delay (2000); //amber on for 2 seconds
digitalWrite (amber , LOW); //switch amber off
digitalWrite (red, HIGH); // switch red light on
delay (6000);// red light on for 6 seconds
digitalWrite (red, LOW); //red off
digitalWrite (green, HIGH); //turn green back on
}
STEPS: