This is something I've been meaning to make for ages. My kids love toy cars (ok, lets face it, all kids love toy cars!). There's been lots of car related fun in our household (some of which has been documented on filthwizardry: "Toy cars and trucks from recycling", "shower curtain village playmat" and "home made toy carwash") and one of the kids favourite pastimes in the car is to shout "RED means STOP!" and "GREEN: GO!" (as loudly as humanly possible). I figured it was about time I used this as an excuse to spend some time in the garage making something with flashing LEDs :)
Between Lin and myself we've managed to amass quite a bit of cheap 'junk' that can be hacked together for something like this. I was going to make something flimsy using a battery pack and a drinking straw, but I was persuaded to make something a bit more kid-proof using a dollar store wooden box, an Altoids tin and the tube from a black felt-tip pen. Here's a picture of all the bits (there are three 150 ohm resistors are missing):
The plastic domes are the discarded remnants of 25c kids toys purchased from a vending machine in our local Taqueria, they looked like they'd make good LED covers (and they were used as wheels in another crafty distraction: "Toy cars and trucks from recycling"). The chip is an ATtiny13.
First up was to drill some holes: two in the box, one on the side for the switch and one on the top for the pen tube; one in the base of the altoids tin (again, for the pen tube) and three in the back of the tin for the LEDs.
Then I painted it black.
The wiring for the base/box is pretty simple, just an AA battery pack wired to a switch with the main wires threaded out of the hole in the top of the box. I stuck the pen casing in at this point and threaded the wires through to the top.
Now it's time for the wiring.
First off, I hot glued the base to the Altoids tin. Then I glued the LEDs into their holes with the cathodes all facing in one direction so I could solder them together to form a ground rail.
I soldered on three, 150 ohm, resistors to an 8-pin dip socket (pins 5, 6 and 7), soldered the positive lead from the base to pin 8 on the socket. Then I connected the LEDs to the resistors and finally connected the ground pin (pin 4) to the ground rail. That's it!
When I was writing the code I didn't think ahead about which pins were going to be connected where in the final project and ended up with the red and green pins mixed up (in software), so the initial sequence was backwards; but then that's why I used a socket rather than directly soldering the uC ;). Easy to fix with a quick software update.
Here's the final working project (yes, the yellow works too, I just didn't take a picture of it):
I've included the code below. As always, it's pretty simple:
### code starts here ###
(I found a nice way of displaying code in blogger. I talk about setting it up here)
/*
* trafflic_lights.c
*
* Created on: Aug 29, 2009
* Author: Paul
*/
#include <avr/io.h>
#include <avr/delay.h>
#define RED_LED PB2
#define YELLOW_LED PB1
#define GREEN_LED PB0
#define RED_DELAY 10
#define YELLOW_DELAY 3
#define GREEN_DELAY 15
uint8_t i = 0;
uint8_t j = 0;
void delay_seconds(uint8_t pSecs)
{
for(i = 0; i < pSecs; i++)
{
for(j = 0; j < 4; j++)
_delay_ms(250);
}
}
int main(void)
{
DDRB = 0xFF;//set all to output
//start traffic light sequence
while(1)
{
PORTB = (1 << RED_LED);
delay_seconds(RED_DELAY);
PORTB = (1 << YELLOW_LED);
delay_seconds(YELLOW_DELAY);
PORTB = (1 << GREEN_LED);
delay_seconds(GREEN_DELAY);
}
}