Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #include "SPI.h"
  2. #include "WS2801.h"
  3.  
  4. int spectrumReset=5;
  5. int spectrumStrobe=4;
  6. int spectrumAnalog=0; //0 for left channel, 1 for right.
  7.  
  8. // Spectrum analyzer read values will be kept here.
  9. int Spectrum[7];
  10.  
  11. //WS2801 strip = WS2801(32);
  12.  
  13. int dataPin = 2;
  14. int clockPin = 3;
  15.  
  16. //WS2801 strip = WS2801(32, dataPin, clockPin);
  17. WS2801 strip = WS2801(160, dataPin, clockPin);
  18.  
  19. void setup() {
  20. Serial.begin(9600);
  21. spectrum_init();
  22.  
  23. strip.begin();
  24. strip.show(); // all off
  25.  
  26. //rainbowCycle(5);
  27.  
  28. }
  29.  
  30. int pos = 0;
  31.  
  32. void loop() {
  33. // Some example procedures showing how to display to the pixels
  34.  
  35. //colorWipe(Color(255, 0, 0), 10);
  36. //colorWipe(Color(0, 255, 0), 10);
  37. //colorWipe(Color(0, 0, 255), 10);
  38. //rainbow(10);
  39. //rainbowCycle(5);
  40. unsigned int Counter, Counter2;
  41. readSpectrum();
  42.  
  43. Counter = (Spectrum[0]/10);
  44. //Counter = ((Spectrum[0] + Spectrum[1]+ Spectrum[2]+ Spectrum[3]+ Spectrum[4]+ Spectrum[5]+ Spectrum[6])/7)/10;
  45. Serial.println(Counter);
  46. if(pos > 256 * 5){ // 5 strips 25 colors
  47. pos = 0;
  48. }
  49.  
  50. if(Counter > 5){
  51. pos += Counter;
  52. //pos += 2;
  53. rainbowCycleOne(pos);
  54. }
  55. else{
  56. pos += 2;
  57. rainbowCycleOne(pos);
  58. delay(10);
  59. }
  60. //}
  61.  
  62. /*
  63. for (pos=0; pos < 256 * 5; pos++) { // 5 cycles of all 25 colors in the wheel
  64. rainbowCycleOne(pos);
  65. }*/
  66. }
  67.  
  68. void rainbow(uint8_t wait) {
  69. int i, j;
  70.  
  71. for (j=0; j < 256; j++) { // 3 cycles of all 256 colors in the wheel
  72. for (i=0; i < strip.numPixels(); i++) {
  73. strip.setPixelColor(i, Wheel( (i + j) % 255));
  74. }
  75. strip.show(); // write all the pixels out
  76. delay(wait);
  77. }
  78. }
  79.  
  80. // Slightly different, this one makes the rainbow wheel equally distributed
  81. // along the chain
  82. void rainbowCycle(uint8_t wait) {
  83. int i, j;
  84.  
  85. for (j=0; j < 256 * 5; j++) { // 5 cycles of all 25 colors in the wheel
  86. for (i=0; i < strip.numPixels(); i++) {
  87. // tricky math! we use each pixel as a fraction of the full 96-color wheel
  88. // (thats the i / strip.numPixels() part)
  89. // Then add in j which makes the colors go around per pixel
  90. // the % 96 is to make the wheel cycle around
  91. strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
  92. }
  93. strip.show(); // write all the pixels out
  94. delay(wait);
  95. }
  96. }
  97.  
  98.  
  99. void rainbowCycleOne(int j) {
  100. int i;
  101.  
  102. for (i=0; i < strip.numPixels(); i++) {
  103. // tricky math! we use each pixel as a fraction of the full 96-color wheel
  104. // (thats the i / strip.numPixels() part)
  105. // Then add in j which makes the colors go around per pixel
  106. // the % 96 is to make the wheel cycle around
  107. strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
  108. }
  109. strip.show(); // write all the pixels out
  110. //delay(5);
  111. }
  112.  
  113. // fill the dots one after the other with said color
  114. // good for testing purposes
  115. void colorWipe(uint32_t c, uint8_t wait) {
  116. int i;
  117.  
  118. for (i=0; i < strip.numPixels(); i++) {
  119. strip.setPixelColor(i, c);
  120. strip.show();
  121. delay(wait);
  122. }
  123. }
  124.  
  125. /* Helper functions */
  126.  
  127. // Create a 24 bit color value from R,G,B
  128. uint32_t Color(byte r, byte g, byte b)
  129. {
  130. uint32_t c;
  131. c = r;
  132. c <<= 8;
  133. c |= g;
  134. c <<= 8;
  135. c |= b;
  136. return c;
  137. }
  138.  
  139. //Input a value 0 to 255 to get a color value.
  140. //The colours are a transition r - g -b - back to r
  141. uint32_t Wheel(byte WheelPos)
  142. {
  143. if (WheelPos < 85) {
  144. return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  145. } else if (WheelPos < 170) {
  146. WheelPos -= 85;
  147. return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  148. } else {
  149. WheelPos -= 170;
  150. return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  151. }
  152. }
  153.  
  154. void readSpectrum()
  155. {
  156.  
  157. byte Band;
  158. for(Band=0;Band <7; Band++)
  159. {
  160. Spectrum[Band] = (analogRead(spectrumAnalog) + analogRead(spectrumAnalog) ) >>1; //Read twice and take the average by dividing by 2
  161. digitalWrite(spectrumStrobe,HIGH);
  162. digitalWrite(spectrumStrobe,LOW);
  163. }
  164. }
  165.  
  166. void spectrum_init()
  167. {
  168. //Setup pins to drive the spectrum analyzer.
  169. pinMode(spectrumReset, OUTPUT);
  170. pinMode(spectrumStrobe, OUTPUT);
  171.  
  172. //Init spectrum analyzer
  173. digitalWrite(spectrumStrobe,LOW);
  174. delay(1);
  175. digitalWrite(spectrumReset,HIGH);
  176. delay(1);
  177. digitalWrite(spectrumStrobe,HIGH);
  178. delay(1);
  179. digitalWrite(spectrumStrobe,LOW);
  180. delay(1);
  181. digitalWrite(spectrumReset,LOW);
  182. delay(5);
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement