Step 3The code
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 Step | Download PDFView All Steps | Next Step » |






































Check out AVRFreaks for other tutorials on avrs and avr programming.
PORTB is the variable representing the pins on the attiny.
PORTB &= ~(1 << thePin);
and
PORTB |= (1 << thePin);
is me flipping the bit(a single pin) on and off
to add another led you can
1) simply connect the second led to the same pin
2) add a transistor to the pin, and connect both led to the transistor
3) add the new led to a new pin so it will work independently, you will need to update the code too
i absolutely love this project BTW.
I chose these two pins because my ICSP programmer uses the other 6, though I believe you can use 6 of the pins for IO... two of them will always be needed for VDD and GND
This means you can get 6 leds working here with one pin to a led... you can get even more working with a multiplex, or charlieplex setup.. but again I have not looked at the datasheet in some time.
The programer
http://www.adafruit.com/index.php?main_page=product_info&cPath=16&products_id=46
The board is something liek this
http://www.evilmadscientist.com/article.php/avrtargetboards