--Apple-Mail-3-475337789 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed On Tuesday, Jun 1, 2004, at 02:47 US/Pacific, Paul James E. wrote: > Wasn't there at one time a thread on the list regarding an algorithim > to program a PIC to simulate the flicker of a candle flame? I have > searched my old emails and cannot find it. I can't get to the > archive to check there. If anyone knows of an algorithim to perform > this function, I'd appreciate a pointer to it. Or if someone has > already written a routine to do this and wants to share it, that would > be fine too. But if that is done, I need permission to rewrite or > customize parts of it for my particular purpose. Either way, I'd > appreciate some leads. > Here is "flames.c" (as a text file, hopefully.) Do whatever you want with it... IIRC, there was also a circuit-cellar article on a candle stage prop that did things with a single LED, complete with "wind detector" to make it behave "naturally" when being moved... (Hmm. yeah - october 2002: Light the Way An LED-Based Alternative by Philip Ching & Bruce Land Actors get paid big bucks to feign abstractions such as happiness. But when it comes to imitating the physical world, Hollywood looks to engineers. Philip and Bruce describe how they faked a flame and illuminated an audience with a real-time LED-based candle. p.30 Keywords: Robot, MSP430, air quality, camera, wireless, power supply, radio, motors, gas detector ) Enjoy BillW -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details. --Apple-Mail-3-475337789 Content-Disposition: attachment; filename=flames.c.txt Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; x-unix-mode=0644; name="flames.c.txt" /*=0D * flames.c=0D * October, 2003 by Bill Westfield * Released to the public domain.=0D *=0D * Turn a PIC12f675 (8pin flash) PIC into "simulated fire" by output=0D * pseudo-random PWM to the 5 available output pins, so you can drive=0D * LEDs or lamp drivers or whatever.=0D *=0D * Target language: freeware version of PIC cc5x=0D */=0D =0D #include "12f675.h"=0D =0D // #define PICKIT1 1 /* test on pickit */=0D =0D #pragma config |=3D 0x1184 // Internal oscillator, etc.=0D #pragma config WDTE=3D0 // (not derived - read from the pickit = demo :-)=0D =0D #define LOOPTIME 255=0D #define DELTIME 50=0D =0D char bright1, bright2, bright3, bright4, bright5;=0D =0D #if PICKIT1=0D /*=0D * The LEDs on the PIC-Kit 1 are set up "funny" so that the 8-pin PIC=0D * can make up to 12 LEDs flash. Individually. Since we some on at=0D * the same time, we're limitted to just a couple, but keep code for=0D * the others around just for timing purposes. This way you can debug=0D= * the algorithms without having to move the chip to the target circuit.=0D= */=0D #define led1_off GPIO5 =3D 0=0D #define led1_on GPIO5 =3D 1=0D #define led2_off GPIO1 =3D 0=0D #define led2_on GPIO1 =3D 1=0D =0D /*=0D * Note that GPIO3 is an input only, so these are all no-ops=0D */=0D #define led3_off GPIO3 =3D 0=0D #define led3_on GPIO3 =3D 1=0D =0D #define led4_on led3_on=0D #define led5_on led3_on=0D #define led4_off led3_off=0D #define led5_off led3_off=0D =0D void led_all_on (void)=0D {=0D GPIO5 =3D 1;=0D GPIO1 =3D 1;=0D }=0D #else=0D /*=0D * In the 'real application', we sink current for LEDs connected to V+=0D= * through current limitting resistors, so setting the pin to 0 turns on=0D= * an LED and a 1 turns it off (not that it matters which state is on = and=0D * which state is off, since they're all random anyway.=0D */=0D #define led_all_on(a) GPIO =3D 0=0D #define led1_on GPIO0 =3D 0=0D #define led1_off GPIO0 =3D 1=0D #define led2_on GPIO1 =3D 0=0D #define led2_off GPIO1 =3D 1=0D #define led3_on GPIO2 =3D 0=0D #define led3_off GPIO2 =3D 1=0D #define led4_on GPIO4 =3D 0=0D #define led4_off GPIO4 =3D 1=0D #define led5_on GPIO5 =3D 0=0D #define led5_off GPIO5 =3D 1=0D =0D #endif // PICKIT11=0D =0D void init(void)=0D {=0D /*=0D * set all pins to real, program controlled outputs=0D */=0D =0D ANSEL =3D 0x0;=0D VRCON =3D 0; //Turn Off Voltage Reference Peripheral=0D CMCON =3D 0x07; //Turn Off Comparator Peripheral=0D TMR0 =3D 0; //Clear Timer0=0D INTCON =3D 0;=0D OPTION =3D 0x80; =0D TRISIO =3D 0b001000;=0D GPIO =3D 0;=0D }=0D =0D =0D =0D /*=0D * getbright()=0D *=0D * Get a new set of pseudo-random brightnesses for each output=0D *=0D * generating the random numbers isn't time-critical, but use a macro=0D * so that we can easilly change the algorithm for all outputs without=0D= * changing too much code. We could use pointers to each brightness=0D * or arrays, but that seems a bit gratuitously HLL-like.=0D *=0D * 127 state pseudo-random numer=0D * Shift register with feedback at bits 0,6=0D */=0D =0D #define nextrandom(last) \=0D newbit =3D last & 0b01000000; /* bit 6 */ \=0D if (last & 1) /* bit 0 */ \=0D newbit ^=3D 0b01000000; \=0D last =3D (last>>1) + newbit;=0D =0D void getbright( void)=0D {=0D char newbit;=0D =0D nextrandom(bright1);=0D nextrandom(bright2);=0D nextrandom(bright3);=0D nextrandom(bright4);=0D nextrandom(bright5);=0D }=0D =0D =0D void main(void)=0D {=0D char pwm1, pwm2, pwm3, pwm4, pwm5, level_delay, pwm_delay;=0D =0D init();=0D bright1 =3D 50; // "random" initialization =0D bright2 =3D 20; =0D bright3 =3D 10;=0D bright4 =3D 1;=0D bright5 =3D 100;=0D =0D while (1) { // forever=0D getbright();=0D for (level_delay =3D 50; level_delay !=3D 0; level_delay--) {=0D pwm1 =3D bright1;=0D pwm2 =3D bright2;=0D pwm3 =3D bright3;=0D pwm4 =3D bright4;=0D pwm5 =3D bright5;=0D led_all_on();=0D for (pwm_delay =3D 128; pwm_delay !=3D0; pwm_delay--) {=0D /*=0D * We have a random number in each PWM between 1 and 128=0D * and we're going to have 128 cycles of delay. So each=0D * decremented output can only cross zero once, at which=0D * time we toggle the output state.=0D */=0D if (--pwm1 =3D=3D 0) {=0D led1_off;=0D }=0D if (--pwm2 =3D=3D 0) {=0D led2_off;=0D }=0D if (--pwm3 =3D=3D 0) {=0D led3_off;=0D }=0D if (--pwm4 =3D=3D 0) {=0D led4_off;=0D }=0D if (--pwm5 =3D=3D 0) {=0D led5_off;=0D }=0D } /* time for new PWM cycle */=0D } /* time for new brightness */=0D } /* while forever */=0D }=0D -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details. --Apple-Mail-3-475337789--