Programming challenge: Traffic lights

IMG_1538We used:

  • 1 laptop with Crumble software installed
  • Crumble controller & Sparkle baton
  • Crumble-friendly switch & ultrasonic distance measuring sensor
  • 1 Crumble-friendly battery box; 3 AA batteries
  • Croc-leads and a micro-USB cable

The traffic light sequence can be programmed to run on a single Sparkle (change all the Sparkle commands in the program below to read “set sparkle 0 to..”), but using 3 chained together or, as here, a sparkle baton is more “true to life”. The traffic lights are set to be the last 3 sparkles on the baton (index numbers 5, 6, and 7 being green, amber, and red, respectively). The program is shown below:

traffic_lights

The sequence simply repeats every 3 seconds, but we can add a switch (here connected between the +ve battery terminal and Input A) to trigger the sequence. In the program below, the green light stays on until the button is pressed. 

button_cross

The program was then modified further so that the sparkle at the other end of the baton (index number 0) functions as a “pedestrian crossing”. When the “traffic lights” are green the pedestrian crossing is red. When the button is pressed the traffic lights cycle through amber to red and then the pedestrian crossing sparkle changes to green. After 5 seconds the pedestrian crossing sparkle changes back to red and the traffic lights continue through red-amber to green.pedestrian_cross

For the final challenge we also added an ultrasonic distance sensor with its “trigger” (T) connected to input “C” on the Crumble and “echo” (E) connected to “B”.

As in the program above, the button triggers the traffic lights to change to red. The “pedestrian light” (i.e. Sparkle 0) then remains green until the ultrasonic distance sensor detects a “car” approaching (in this case represented by a hand waving less than 4cm away). The “car” triggers the pedestrian crossing to turn red and the traffic lights to change to green.

Screen Shot traffic_ultra

Unfortunately, the resulting tangle of wires is not very attractive. (Note: I have used the “power out” pads on the Sparkle baton as power inputs for the ultrasonic distance sensor.)

Next challenge: construct something to hold the baton vertical and the ultrasonic module in the right position to detect a lego car!

FullSizeRender-3

Comments are closed.