Fire Effect for Arduino (ATtiny85 proof)

And now for something completely different.
Since our visit to Schwarzwald, Germany, misses Viking and me are in a very creative mood. All we want to do is make cheesy things like diorama’s of houses with snow, little streetlanterns, spruce trees and more Christmassy goodness.
Allready familiar with the holy Arduino system, my mind spun around and I thought of making a fireplace LED thingy for use in little tiny houses with little tiny fireplaces, using the one of the smallest Arduino’s I could find. The ATtiny 85.
The ATtiny is a 8 pin computer with the utmost abilities, here used as a randomised LED blinker with some Viking logic in it.
For a lifelike fireplace effect I think you need three colors : red, orange (amber) and yellow. For a more lifelike effect I think you need full randomisation for each color, timewise and for the intensity.
Normally it would be easy in an Arduino Uno 328P, Mega 2560, or a Micro or any other child of this family, to program such an easy peasy gadget. For the ATtiny85 that’s another kind of cook as we say in Holland. Not that it’s that hard, but it needs some extra lines of code.
Output 0 and Output 1 are analogue outputs and can be used simply by writing a value from 0 to 255 to it, for intensity control. For Output 2, it is harder since this is a digital output pin and can only be set to 1 or 0 (on or off). So we need a sort of pwm calculation to give the pin a software pwm (pulse width modulation) output. The pin is switched on and a calculation determines how many microseconds later the pin has to be switched off, followed by a calculated wait.
I am not going to bore you any longer with my lame explanations, here are the circuit and the sketch.
Happy building and happy fireplace moments 🙂

And here is the sketch :


// LED Fire Effect V3 (usable for ATtiny85)

int ledPin1=0;    // this must be an analogue pin
int ledPin2=1;    // this must be an analogue pin
int ledPin3=2;    // this can be a digital pin
int led3pwm=0;
int tv=135;       // max time value in milliseconds for time randomisation
int tc=1000/255;
// time constant for pwm, 1000 microseconds devided by max analogue value

void setup()
{
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
}

int cntr1=1;                            // randomised time counter for LED1
int cntr2=1;                            // randomised time counter for LED2
int cntr3=1;                            // randomised time counter for LED3

void loop() {

cntr1–;
if (cntr1<=0) {
cntr1=(random(tv));                 // randomise time counter for LED1
analogWrite(ledPin1,random(127)+128); // write random value + basic value to LED1
}
cntr2–;
if (cntr2<=0) {
cntr2=(random(tv));                  // randomise time counter for LED2
analogWrite(ledPin2,random(127)+128);    // write random value + basic value to LED2
}
cntr3–;
if (cntr3<=0) {
cntr3=(random(tv));                  // randomise time counter for LED3
led3pwm=(random(127)+128);           // write random value + basic value to LED3 variable
}
digitalWrite(ledPin3,true);               // turn on LED3
delayMicroseconds (tc*led3pwm);           // delay for calculated pwm-on time
digitalWrite(ledPin3,false);              // turn off LED3
delayMicroseconds (tc*(255-(led3pwm)));   // delay for calculated pwm-off time
}

 

Dit bericht is geplaatst in knutsel. Bookmark de permalink.