Advertisement
dragonflydiy

Pumpkin fade C code for ATTiny85

Dec 17th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. /* pumpkin fade - drives a single RGB LED from an ATTiny85
  2. Uses 3 channel PWM on digital pins 0,1,4
  3. John Ridley October 28 2014
  4. Released into public domain */
  5.  
  6. #define F_CPU 8000000
  7.  
  8. #include <avr/io.h>
  9. #include <util/delay.h>
  10.  
  11. #define RED 0
  12. #define GREEN 1
  13. #define BLUE 2
  14.  
  15.  
  16. // stepnum = 0 -> 64
  17. int slider(int from, int to, int stepnum)
  18. {
  19. int diff = from-to;
  20. return from - ((diff*stepnum)/64);
  21. }
  22.  
  23.  
  24. void main()
  25. {
  26. /* Port 0,1,4 to output */
  27. DDRB = 1<<DDB4 | 1<<DDB1 | 1<<DDB0;
  28.  
  29. /* both timers to fast PWM mode, no prescaler */
  30. TCCR0A = 2<<COM0A0 | 2<<COM0B0 | 3<<WGM00;
  31. TCCR0B = 0<<WGM02 | 1<<CS00;
  32. TCCR1 = 0<<PWM1A | 0<<COM1A0 | 1<<CS10;
  33. GTCCR = 1<<PWM1B | 2<<COM1B0;
  34.  
  35. /* these are the colors we fade between */
  36. int colors[8][3] = {
  37. {255,0,0},
  38. {0,0,255},
  39. {255,102,178}, // pink
  40. {0,255,0},
  41. {255,153,51}, // orange
  42. {51,255,255}, // teal
  43. {255,0,255}, // magenta
  44. {128,255,102}, // yellow
  45. };
  46.  
  47. int curcolor = 0;
  48. int numcolors = 8;
  49.  
  50. while (1)
  51. {
  52. int nextcolor = curcolor+1;
  53. if (nextcolor >= numcolors)
  54. nextcolor = 0;
  55.  
  56. for (int fade = 0; fade < 64; fade++)
  57. {
  58. int red = slider(colors[curcolor][RED],
  59. colors[nextcolor][RED],
  60. fade);
  61. int green = slider(colors[curcolor][GREEN],
  62. colors[nextcolor][GREEN],
  63. fade);
  64. int blue = slider(colors[curcolor][BLUE],
  65. colors[nextcolor][BLUE],
  66. fade);
  67.  
  68. OCR0A = red;
  69. OCR0B = green;
  70. OCR1B = blue;
  71. _delay_ms(3);
  72. }
  73. curcolor = nextcolor;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement