??? 09/05/06 20:36 Read: times Msg Score: +1 +1 Informative |
#123715 - Twinkly lights driver. |
I know its a bit early to be thinking about such things, but... I thought I would post this bit of code as a quite clever pulse width modulation scheme which in this case I used to control twinkly lights on a christmas tree.The basic idea is that the inner loop allows the output on the port to transistion smoothly between two values,in this case two random numbers at a rate controlled by the 'step' variable, the maximum brightness is set by the TOTAL variable and the value that step can take is constrained by the requirement that (TOTAL%step==0).In this implementation even more twinklyness is achieved by modifying the step value at random.The longest transistion from one value to the next takes about one second running at 0.5Mips.I am sure people can modify it to achieve surprising results.
# define TOTAL 252 // (<255 !!) int main(void) { uint8_t step=6, step2,reg1, reg2, n=0, up=1, cntr; srand(10); //Seed the rng reg1= rand() ; // The first random number while (1) { reg2= rand() ; // The second random number cntr= TOTAL; // Start with fresh counter while (1) { n+=(up)?1:-1; PORTB=(n>cntr)?reg2:reg1; up=(n==255)?0:((n==0)?1:up); if (cntr==0) break; if (n==0) cntr-= step; } reg1=reg2; step2=rand(); if((TOTAL % step2) ==0) step=step2; } } |
Topic | Author | Date |
Twinkly lights driver. | 01/01/70 00:00 | |
Amazing... | 01/01/70 00:00 | |
we only have stupid lights in England | 01/01/70 00:00 | |
Can you give comparsion | 01/01/70 00:00 | |
yep,ill do that | 01/01/70 00:00 | |
actualy I cheated :-) | 01/01/70 00:00 | |
LFSR | 01/01/70 00:00 | |
yeah i was thinking about that today | 01/01/70 00:00 | |
triac control | 01/01/70 00:00 | |
triac control | 01/01/70 00:00 | |
You after them girlies again? | 01/01/70 00:00 |