Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <math.h>
  4. #include "WS2811.h"
  5.  
  6. #define BIT(B) (0x01 << (uint8_t)(B))
  7. #define SET_BIT_HI(V, B) (V) |= (uint8_t)BIT(B)
  8. #define SET_BIT_LO(V, B) (V) &= (uint8_t)~BIT(B)
  9.  
  10. #define PAUSE 1000 // msec
  11. #define DELAY 10 // msec
  12. #define CONST_E 2.71828
  13.  
  14. // Define the output function, using pin 0 on port b.
  15. DEFINE_WS2811_FN(WS2811RGB, PORTB, 0)
  16.  
  17. void setup()
  18. {
  19. // Configure pin for output.
  20. SET_BIT_HI(DDRB, 0);
  21. SET_BIT_LO(PORTB, 0);
  22. // Serial.begin(9600);
  23. }
  24.  
  25. float cR = 1,
  26. cG = 0,
  27. cB = 0;
  28.  
  29. // Drive the three pixels in an infinite loop.
  30. void loop(void)
  31. {
  32. // off->red, off->green, off->blue
  33. RGB_t rgb[240] = {
  34. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  35. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  36. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  37. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  38. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  39. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  40. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  41. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  42. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  43. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  44. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  45. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  46. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  47. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  48. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  49. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  50. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  51. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  52. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  53. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  54. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  55. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  56. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
  57. {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}
  58. };
  59. const int Elements = ARRAYLEN(rgb);
  60.  
  61. WS2811RGB(rgb, Elements);
  62. _delay_ms(PAUSE);
  63.  
  64. int col = 0;
  65.  
  66. // loop forever.
  67. for (;;) {
  68. for(int k=0; k<10; k++) {
  69. for(int i = Elements - 1; i >= 1; i--)
  70. {
  71. rgb[i].r = rgb[i - 1].r;
  72. rgb[i].g = rgb[i - 1].g;
  73. rgb[i].b = rgb[i - 1].b;
  74. }
  75.  
  76. long micLevel = analogRead(0);
  77. int log4Level = log(micLevel) / log(4) * 12.5;
  78. int squareLevel = micLevel * micLevel * micLevel / ((long)4 * 1024 * 1024);
  79. float sCurveLevel = (int)(sCurve(micLevel, 4) * 255);
  80.  
  81. float fMicLevel = micLevel / 4.0f / 200.0f;
  82. float quadraticLevel = pow(fMicLevel, 2);
  83.  
  84. //Serial.println(squareLevel);
  85.  
  86. const float one = 1.f;
  87.  
  88. rgb[0].r = rgb[0].g = rgb[0].b = 0;
  89.  
  90. setRgb(&(rgb[0]), (squareLevel / 2.0f));
  91. //the lighting of the LED's works on a 0 to 1 scale
  92. // 0 is off and 1 is full bright and anything in between dims the light
  93. // in the code "(squareLevel / 0.2f))" the number 0.2 restricts the range between on and off
  94. // increasing that number, increases the range but also makes the lights less responsive
  95.  
  96. //if(log4Level > squareLevel && log4Level > sCurveLevel)
  97. //rgb[0].r = log4Level;
  98. //else if(squareLevel > log4Level && squareLevel > sCurveLevel)
  99. // rgb[0].g = quadraticLevel * 255;
  100. //else if(sCurveLevel > log4Level && sCurveLevel > squareLevel)
  101. //rgb[0].b = sCurveLevel;
  102.  
  103. WS2811RGB(rgb, Elements);
  104. //_delay_ms(5);
  105. fadeRgb();
  106. }
  107.  
  108. //_delay_ms(500);
  109.  
  110. col = ++col % 6; // no idea
  111. cycleRgb(col); // makes them blinky
  112. }
  113. }
  114.  
  115. const int phaseLength = 5; // thinking that it dose the color length or the length of the power coming in
  116. const int period = phaseLength * 5;
  117. int iteration = 0;
  118. void fadeRgb() {
  119.  
  120. // Determine which phase we are in
  121. int phase = iteration / phaseLength;
  122. int step = iteration % phaseLength;
  123.  
  124. switch(phase)
  125. {
  126. case 0: // Red on, green increasing, blue off
  127. cR = 1;
  128. cG = step / ((float)phaseLength);
  129. cB = 0;
  130. break;
  131.  
  132. case 1: // Red decreasing, green on, blue off
  133. cR = (phaseLength - step) / ((float)phaseLength);
  134. cG = 1;
  135. cB = 0;
  136. break;
  137.  
  138. case 2: // Red off, green on, blue increasing
  139. cR = 0;
  140. cG = 1;
  141. cB = step / ((float)phaseLength);
  142. break;
  143.  
  144. case 3: // Red off, green decreasing, blue on
  145. cR = 0;
  146. cG = (phaseLength - step) / ((float)phaseLength);
  147. cB = 1;
  148. break;
  149.  
  150. case 4: // Red increasing, green off, blue on
  151. cR = step / ((float)phaseLength);
  152. cG = 0;
  153. cB = 1;
  154. break;
  155.  
  156. case 5: // Red on, green off, blue decreasing
  157. cR = 1;
  158. cG = 0;
  159. cB = (phaseLength - step) / ((float)phaseLength);
  160. break;
  161. }
  162.  
  163. iteration = ++iteration % period;
  164. }
  165.  
  166. void cycleRgb(int col) {
  167. switch(col)
  168. {
  169. case 0:
  170. cR = 1;
  171. cG = 0;
  172. cB = 0;
  173. break;
  174.  
  175. case 1:
  176. cR = 0.5;
  177. cG = 0.5;
  178. cB = 0;
  179. break;
  180.  
  181. case 2:
  182. cR = 0;
  183. cG = 1;
  184. cB = 0;
  185. break;
  186.  
  187. case 3:
  188. cR = 0;
  189. cG = 0.5;
  190. cB = 0.5;
  191. break;
  192.  
  193. case 4:
  194. cR = 0;
  195. cG = 0;
  196. cB = 1;
  197. break;
  198.  
  199. case 5:
  200. cR = 0.5;
  201. cG = 0;
  202. cB = 0.5;
  203. break;
  204. }
  205. }
  206.  
  207. void setRgb(RGB_t* rgb, float val)
  208. {
  209. if (val > 1.0f) val = 1.0f;
  210. rgb->r = (int)(val * cR * 255);
  211. rgb->g = (int)(val * cG * 255);
  212. rgb->b = (int)(val * cB * 255);
  213. }
  214.  
  215. float sCurve(int micInput, float power)
  216. {
  217. float normalised = micInput / 1024.0f;
  218.  
  219. return normalised / (pow(1 + pow(normalised, power), 1.0f / power) );
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement