Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The file that is currently on an Arduino Uno with a serial number of 74132343530351609150
/*
Based on http://www.makeuseof.com/tag/arduino-traffic-light-controller/
USE_GITHUB_USERNAME=mark-wilson
*/
// Pins for coloured LEDs
int red = 11;
int green = 12;
int light = 0;
int button = 2; // Pushbutton on pin 2
int buttonValue = 0; // Button defaults to 0 (LOW)
void setup(){
// Set up pins with LEDs as output devices and switch for input
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(button,INPUT);
}
void loop(){
// Read the value of the pushbutton switch
buttonValue = digitalRead(button);
if (buttonValue == HIGH){
changeLights();
delay(15000); // Wait 15 seconds before reading again
}
}
void changeLights(){
// Change the lights based on current value: 0 is not set; 1 is green; 2 is red
switch (light) {
case 1:
turnLightRed();
break;
case 2:
turnLightGreen();
break;
default:
turnLightRed();
}
}
void turnLightGreen(){
// Turn off the red and turn on the green
digitalWrite(red,LOW);
digitalWrite(green,HIGH);
light = 1;
}
void turnLightRed(){
// Turn off the green and turn on the red
digitalWrite(green,LOW);
digitalWrite(red,HIGH);
light = 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.