Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* pumpkin fade - drives a single RGB LED from an ATTiny85
- Uses 3 channel PWM on digital pins 0,1,4
- John Ridley October 28 2014
- Released into public domain */
- #define F_CPU 8000000
- #include <avr/io.h>
- #include <util/delay.h>
- #define RED 0
- #define GREEN 1
- #define BLUE 2
- // stepnum = 0 -> 64
- int slider(int from, int to, int stepnum)
- {
- int diff = from-to;
- return from - ((diff*stepnum)/64);
- }
- void main()
- {
- /* Port 0,1,4 to output */
- DDRB = 1<<DDB4 | 1<<DDB1 | 1<<DDB0;
- /* both timers to fast PWM mode, no prescaler */
- TCCR0A = 2<<COM0A0 | 2<<COM0B0 | 3<<WGM00;
- TCCR0B = 0<<WGM02 | 1<<CS00;
- TCCR1 = 0<<PWM1A | 0<<COM1A0 | 1<<CS10;
- GTCCR = 1<<PWM1B | 2<<COM1B0;
- /* these are the colors we fade between */
- int colors[8][3] = {
- {255,0,0},
- {0,0,255},
- {255,102,178}, // pink
- {0,255,0},
- {255,153,51}, // orange
- {51,255,255}, // teal
- {255,0,255}, // magenta
- {128,255,102}, // yellow
- };
- int curcolor = 0;
- int numcolors = 8;
- while (1)
- {
- int nextcolor = curcolor+1;
- if (nextcolor >= numcolors)
- nextcolor = 0;
- for (int fade = 0; fade < 64; fade++)
- {
- int red = slider(colors[curcolor][RED],
- colors[nextcolor][RED],
- fade);
- int green = slider(colors[curcolor][GREEN],
- colors[nextcolor][GREEN],
- fade);
- int blue = slider(colors[curcolor][BLUE],
- colors[nextcolor][BLUE],
- fade);
- OCR0A = red;
- OCR0B = green;
- OCR1B = blue;
- _delay_ms(3);
- }
- curcolor = nextcolor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement