Upgraded the “traffic lights” in my office already

This content is 12 years old. I don't routinely update old blog posts as they are only intended to represent a view at a particular point in time. Please be warned that the information here may be out of date.

Last night I wrote a blog post about the “traffic lights” for my office. The trouble with the original setup was that the light was set to a particular state until I edited the code and uploaded a new version to the Arduino.

I was so excited about having something working that I hadn’t actually finished reading the Arduino Programming For Beginners: The Traffic Light Controller post that I referenced. Scroll down a bit further and James Bruce extends his traffic light sequence to simulate someone pressing a button to change the lights (e.g. to cross the road).

I worked on this to come up with something similar – using a variation on James’ wiring diagram (except with 2 LEDs rather than 3, and using pins 11 and 12 for my LEDs and 2 for the pushbutton switch), I now have a setup which waits for the button to be pressed, then sets the red LED, until the button is pressed again (when it goes green), etc.

My code is available on github but here’s the current version (just in case I lose that version as I get to grips with source control…):

/*
Red/green LED indicator with pushbutton control
Based on http://www.makeuseof.com/tag/arduino-traffic-light-controller/
*/

// 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;
}

Now I’m wondering how long it will be before the kids work out that they too can change the status (like pushing the button at a pedestrian crossing!)…

4 thoughts on “Upgraded the “traffic lights” in my office already

  1. Hi Mark,

    Feel free to ping me (internally or otherwise) if you want any basic help with Git. For one person, the routine should usually be:

    git clone
    # Loop Here :)

    git add
    git commit
    git push
    # Go to “Loop Here”

    Or

    git clone
    # Loop Here :)

    git add
    git commit
    # Go to “Loop Here” until your features you’re coding are working then…
    git push

    It should only start to get complicated for projects with multiple people working on it, or if you start doing branching and merging. Once you go Git* (or Bazaar, or Mercurial) you won’t want to go back to Subversion, or Visual Source Safe, or (from bitter personal experience) Sharepoint.

    * and understand it :)

  2. Thanks Jon – always appreciated. I’ve actually got Git hooked into the Arduino IDE (another blog post I need to write), which is a double-edged sword – on one side it’s easy, on the other it’s not as flexible as the CLI.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.