3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Flameless candle from an attiny13

Step 3The code

Here is the code I used. I just flash the leds, and try to add some randomness to it. The code could be better using pwm, and power saving features, but I don't know how to do any of that. My first non-arduino circuit, and my first instructable. The effect from the two leds is satisfactory in spite of how I did it. Varying brightness, color, and flickering.

Compile and upload the code to the tiny13, and you are good to go.

Feel free to post better code...

#include <avr/io.h>

int main(void) {
int thePin = 0x0;
long randVal;
srandom(123); //random seed
DDRB = 0x3; // B0-1 set to output

for(;;) {
randVal = random(); // choose a pin
if((randVal % 2) == 0) {
thePin = 0x0;
}
else {
thePin = 0x1;
}

randVal = random(); //high or low
if((randVal % 2) == 0) {
PORTB &= ~(1 << thePin); // x &= ~(1 << n); forces the nth bit of x to be 0. all other bits left alone.
}
else {
PORTB |= (1 << thePin); // x |= (1 << n); forces the nth bit of x to be 1. all other bits left alone.
}
}

}
« Previous StepDownload PDFView All StepsNext Step »
5 comments
Apr 15, 2010. 9:54 PMpurpulhaze says:
Can you explain the code? I'm trying to figure out how to add another led if possible.
Apr 20, 2010. 8:15 AMpurpulhaze says:
from what I can tell seems it would be easier to find another micro controller to add more leds since there are really only 2 general purpose i/o pins and need the other ones ffor programming. 
Oct 7, 2011. 6:54 PMicebat21 says:
How would you modify the code to work on the other pins?
i absolutely love this project BTW.
Oct 20, 2008. 5:51 PMrockyt says:
what are you using to get the code to the ic?
Oct 21, 2008. 1:05 PMfacegarden says:
There is always the lovely Atmel STK-500, the dev board designed to program these things. They aren't as cheap as other options but they make things so easy it really is nice. Plus, most people don't know this, but students get 50% off on development kits from Atmel. You'd have to call them for details but it could save you $50 or so, i forget how much my STK-500 was. -Taylor

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
6
Followers
2
Author:aballen