// Black - ground wire
// Red - Power supply wire
// short leg on LED is cathode and should be connected to the ground
//Define LEDs
const int red = 12;
const int yellow = 11;
const int green = 10;
const int Pot = 2;
// define time delays
int redtime = 3000;
int yellowtime = 1000;
int greentime = 3000;
void setup() {
// put your setup code here, to run once:
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(Pot, INPUT);
//must initialise pins all as off initally
initialise_pins();
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(Pot) == HIGH){
traffic_light();
}
}
// function to initialise pins
void initialise_pins(){
digitalWrite(green, LOW);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
}
// function for traffic lights
void traffic_light(){
digitalWrite(red, HIGH);
delay(redtime);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
delay(greentime);
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(yellowtime);
digitalWrite(yellow, LOW);
}