Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark-wilson/3220401 to your computer and use it in GitHub Desktop.
Save mark-wilson/3220401 to your computer and use it in GitHub Desktop.
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