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

2 comments:

Angel said...

Wow that is awesome! your kids must have been really surprised when the pretend fire really lit up!

M.K. Greenwood said...

I may try this with a few of the candle LEDs the dollar store has. They flicker by themselves quite well, so no programming required (which is good because I SUCK at that).