experiment 2 - traffic lights In the last experiment I wired up LEDs to each one of the PiFace’s outputs and cycled through them. In this one, I decided to try a slightly more “real world” application, and build a set of traffic lights.

Since I live in the UK, these are the UK three light traffic lights (red/amber/green) and follow the UK light sequence – RED, RED/AMBER, GREEN, GREEN, AMBER, RED.

The Circuit

The circuit here is very similar to the one used for the previous experiment, but will only use six of the available eight control outputs. Each light contains a set of three LEDs connected to one of the pins of the PiFace’s output interface, one red, one amber and one green.

See the attached diagram.

The Software

The code for this experiment is where the extra complexity resides, since we must drive each light in the correct sequence and obeying certain rules:

  • The lights must transition in the correct, UK, sequence, i.e. RED, RED/AMBER, GREEN, and then GREEN, AMBER, RED.
  • For traffic safety, the green light must transition to red before the red light transitions to green.

To avoid repeating myself, I created a simple class to drive the lights.

Each class is initialised with the pin number of each light, and an initial status. The Toggle method will transition the light; RED, RED/AMBER, GREEN if status is RED, and GREEN, AMBER, RED if status is GREEN.

The main loop of the program waits for a period of time, then toggles the lights, starting with the green one.

Here we have it in action…


Following on from my post yesterday, I would like to introduce something I quickly hacked together to show how the kind of thing I was talking about might work.

As discussed in yesterday’s article, I already use twitter to provide a data feed for selective traffic alerts.

Street Level URLs provides a simple wrapper around this search, providing you with a nice summary page and the option to access the data in a number of different formats.

It uses twitter for the actual data and uses mod_rewrite rules to wrap the actual search up into an addressable URL.

Have fun!

» Street Level URLs

I like feeds and APIs.

Feeds and APIs provide ways for others to access a service and to recombine the data in new and unexpected ways. Ways that have consistently been proven to be beneficial to both parties (which makes google’s increasing antipathy towards them an interesting, not to mention short sighted, trend).

Anyway, it was one morning when I was attempting to find a route to work for my girlfriend which bypassed the numerous arterial route crashes that had happened that morning and I found myself pondering thus

… wouldn’t it be cool if roads and junctions had permanent URLs, and better yet if you could get a data feed on them?

This would let you do many cool things, for example you could enter your route to work and get a status of the traffic en route – or at the very least attach a particular traffic blackspot (in our case the 13 bends of death on the A4074) to ifttt and get SMS alerts if there was a problem.

Giving roads and junctions addressable urls would be an obvious extension to the google maps API, but given that Google won’t even let you embed a map in a page if it contains a traffic data overlay it seems unlikely they’ll provide such access to their data. Other sources such as the Yahoo’s traffic API has long since been shut down.

So, what alternative traffic data sources could we use?

One possible data source we could use would be to parse a twitter search for the road in question. We both currently use ifttt hookups to get alerts for certain key roads, so the basic concept is sound.

This isn’t perfect, for example there is no understanding of the context of a message – so for example a message saying “No traffic problems on the A4074” and “Terrible crash on the A4074” would both trigger the alert, but only the latter would indicate a problem.

The other problem of course is that it also relies on people tweeting, but in effect this would actually pull in quite a diverse range of secondary sources – in my case, for example, it also pulls in any source that feed into the local radio station – which includes reports from their traffic spotter plane.

As an individual without access to data from traffic sensors, or any ability to collect data directly (unlike, say, google who can use position reports from android phones), we are pretty much limited to collecting data from secondary sources as far as I can see.

What other sources could we use?