Unpacking the Arduino: making some traffic lights

Standard

My long-awaited Arduino Duemilanove board arrived today. Great! Now I have a use for all the miscellaneous electronic kit I have been randomly purchasing over the past week-and-a-half.

After 10 minutes, it was connected, and the obligatory “Hello World” flashing-LED had been created. Woo! Sure it’s simple, but my entry into the world of physical computing. Just a few minutes later, I’d expanded that solitary LED into a chain of 5 blinking LEDs (I know what you’re thinking: “that’s awesome, Rich”).

My first project: timer-based traffic lights

Given that I don’t have any force-sensors or other sensing gadgetry laying around just yet, I decided to model a simple, timer-based traffic light model comprising of three LEDs, and four combinations of light:

  • Red
  • Red + Amber
  • Green
  • Amber

(In the UK, that’s the sequence of traffic lights we use to dictate: “Stop”, “Proceed if it’s OK”, “Go”, “Get ready to stop”.)

I decided that, for the purposes of my first adventure, I would run the lights on a simple 2.5 second timer (long enough to figure out if the sequence is right, quick enough to prevent instantaneous brain boredom, waiting for them to change).

So, without further ado, the grand unveiling, here is my first Arduino circuit:

Arduino Traffic Lights (Timer version)

Given that’s so messy, here’s a circuit diagram, created using Fritzing (which is awesome, by the way – go get it, it’s free):

Ignore the fact it says “Arduino Diecimila”, I’m actually using the Arduino Duemilanove which I’m told is the newest one. I don’t think Fritzing has been updated with the new part just yet, but of course the bits you’re interested in are the pin connections…

Here’s a video of the traffic light sketch in action:

For posterity, I’ve also shown the sketch used to create this marvellous display of light:

/* Richard's Traffic Light Program */

int pinRedLed = 2;
int pinYelLed = 3;
int pinGrnLed = 4;
int lightState = 0;

void setup() {
    pinMode(pinRedLed, OUTPUT);    /* Set the LED pins to output */
    pinMode(pinYelLed, OUTPUT);
    pinMode(pinGrnLed, OUTPUT);
    digitalWrite(pinRedLed, HIGH);  /* Turn the RED light on, so we don't cause any traffic accidents while we initialise πŸ™‚ */
}

void loop() {

    lightState++;
    if (lightState > 3) {            // If the light state > 3, reset it to 0 (red).
      lightState = 0;
    }       

  delay(2500);

  /* Display correct light sequence */
  if (lightState == 0) { // Red
    digitalWrite(pinRedLed, HIGH);
    lightOff(pinYelLed);
    lightOff(pinGrnLed);
  }
  if (lightState == 1) { // Red and Amber
    digitalWrite(pinRedLed, HIGH);
    digitalWrite(pinYelLed, HIGH);
    lightOff(pinGrnLed);
  }
  if (lightState == 2) { // Green
    digitalWrite(pinGrnLed, HIGH);
    lightOff(pinYelLed);
    lightOff(pinRedLed);
  }
  if (lightState == 3) { // Amber-only
    digitalWrite(pinYelLed, HIGH);
    lightOff(pinRedLed);
    lightOff(pinGrnLed);
  }

}

void lightOff(int pin) {
  digitalWrite(pin, LOW);
}

So what’s next?
Tomorrow, or as soon as I get some more spare time, I’m going to modify this sketch to wait for a button press before changing the light sequence (instead of working on a timer). Actually, thinking about it, it would be good to have the lights operating autonomously on a timer, but as soon as a button is pushed, stop the traffic (go to Amber, then Red), wait for 10 seconds to let our pedestrians cross the road safely, then let the traffic pass again (go to Red + Amber, then Green) and then resume timer functionality. Ooo! That sounds exciting! πŸ™‚

/Rich

Advertisements

2 thoughts on “Unpacking the Arduino: making some traffic lights

Comments are closed.