Sunday, August 30, 2009

Toy Traffic Lights



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

Tuesday, August 25, 2009

Led camp fire



A few days ago I came home from work and was rather surprised to find a, full-sized, indoor camp-out taking up the living room... you have to see it to believe it (the original was bigger than the version above!):- filthwizardry.blogspot.com. Well, the kids loved it and my lovely lady inspired me to make a flame free campfire to add to the fun. I figured it'd be easy to do with a uC and a few red/orange LEDs -all of which I had lying around the place.

To get the basic set-up I cannibalized the innards of the glow in the dark balls project and jammed a few LEDs in the right places; this let me start messing with the code. In the second image you can see my test set-up. I ended up using my LED tester to provide power to the breadboard and my ATtiny header board for quickly cycling the code on the chip. I was happily surprised that both of these worked and were useful - I've been getting frustrated jamming frayed power leads into the breadboard and it'd only just occurred to me that I could use the LED tester.



All I wanted the code to do was randomly switch the LEDs on and off fast enough to mimic flickering flames. I messed around with the speed of flickering a bit until I was happy.

For those who are interested, I've included the code at the bottom of the page. It's a pretty simple affair. I take no credit for the random number generator, in the past I've just used library code for this, but I happened across that nice bit of code online and fancied using it (I think this is the original source but I found it here). The code is in a slightly incomplete state as I was also messing around with altering the delay between each set of LED updates. Shorter delays give the impression of an angry/quick burning fire and longer delays give rise to a soothing fire. I quite like the idea of having a random drift for the delay to make the fire more interesting, but got tired at around midnight and decided to simplify.

Lin suggested housing everything in the top section of a solar garden light case (which had been destroyed during a particularly energetic play-date). All that needed modifying was to add a small hole for the switch. I also cut down the LEDs so they took up less space and were more secure:



Then I added some protection to the top and a cardboard base to keep the innards in place (I used a hot-glue gun to secure both parts).



And that's it! Very simple.

Here's the contraption hidden in the kids camp fire waiting to be discovered:



and a little video of it in action:



p.s. anyone notice the freaky kiddie scarecrow in the first image? Have a look at the whole filthwizardry post, that thing freaks me out on a daily basis! I think it's the eyes... "she's got the cold dead eyes of a killer" ;)

### code starts here ###

/*
* candle.c
*
* Created on: Aug 21, 2009
* Author: Paul
*/
#include <avr/io.h>
#include <avr/delay.h>

/*
* pseudorandom
* return the next pseudo-random number (PRN) using a standard maximum
* length xor-feedback 16-bit shift register.
* This returns the number from 1 to 65535 in a fixed but apparently
* random sequence. No one number repeats.
*/
uint16_t randreg = 10;

static uint16_t pseudorandom16 (void)
{
uint16_t newbit = 0;

if (randreg == 0) {
randreg = 1;
}
if (randreg & 0x8000) newbit = 1;
if (randreg & 0x4000) newbit ^= 1;
if (randreg & 0x1000) newbit ^= 1;
if (randreg & 0x0008) newbit ^= 1;
randreg = (randreg << 1) + newbit;
return randreg;
}

int main(void)
{
uint8_t i = 0;
uint16_t j = 0;
uint8_t delay = 20;
DDRB = 0xFF;

while(1)
{
j = pseudorandom16();
for(i=0;i<5;i++)
{
if(pseudorandom16() > j)
{
PORTB ^= (1<<i);
}
}
_delay_ms(delay);
}
}

Monday, August 10, 2009

Home cooked PCB etching - part 1

I saw this excellent Instructables on easy PCB etching and have been itching to give it a try ever since. I popped into the local Radioshack to pick up the bits, but was dissapointed that they had neither the ferric chloride solution nor the copper boards. So, when we were up in Santa Rosa for the mini Maker's Fair (see Lin's blogpost for more on that) I ran into the Radioshack up there and was lucky (they'd only just restocked them). Tonight, after the battle of the bed-times, I thought I'd give it a test run. For a proper run, I'm going to have to decide on a board to make, get some transfer paper and print out the design at work (no laser printer at home).

The instructable mentioned that a sharpie should be enough to mask the board, so I drew out a test pattern:

Imaginative, eh? I guess I could have gone computer programing 101 and used "Hello World!" instead.

So, next step was to add ferric chloride solution to a sponge and get scrubbing. The only plastic gloves we've got in the house are 'small' so I figured I'd risk it and just try keeping my fingers out of the way as much as possible. I used a dollar store, sponge on a stick, dabbed on a small amount of ferric chloride solution and got scrubbing.

About 5 minutes later, nothing had happened. A bit of an anti-climax from the promised '"1-minute etch" but I persevered for a little while longer and added a bit more solution to the sponge. I couldn't tell if the black liquid that was forming was to do with the etching process, or if it was just me scrubbing off the sharpie marks...

Another 5 mins of scrubbing and a few more dabs of solution yielded a glimmer of hope:

You can clearly see some of the copper being cleared from the top of the board. A few more minutes and most of the board was clear. Being impatient, I stopped there and used a "Mr Clean magic eraser" to clean off the remaining sharpie marks (no nail polish remover in our house, but plenty of need for powerful grime removal ;) ).



Sweet! It works! Time to create something more functional!