Guest User

msgeq7 radiate

a guest
Feb 18th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN 2
  4. #define COLOR_ORDER GRB
  5. #define CHIPSET WS2812B
  6. #define NUM_LEDS 144
  7.  
  8. #define BRIGHTNESS 200
  9. #define FRAMES_PER_SECOND 60
  10. #define MSGEQ7_STROBE_PIN 4
  11. #define MSGEQ7_RESET_PIN 5
  12. #define MSGEQ7_LEFT_PIN A1
  13. #define MSGEQ7_RIGHT_PIN A0
  14.  
  15.  
  16. int filter_min = 100;
  17. int filter_max = 1016;
  18.  
  19. int prev_value;
  20.  
  21. int left_value;
  22. int right_value;
  23.  
  24. float lowPass_audio = 0.18;
  25. uint8_t band;
  26. int left[7];
  27. int right[7];
  28. int mono[7];
  29. int mapped[7];
  30. uint8_t zero;
  31. uint8_t three;
  32. uint8_t six;
  33.  
  34. int HALF_POS = (NUM_LEDS * 0.5) - 1;
  35.  
  36.  
  37. CRGB leds[NUM_LEDS];
  38.  
  39. void setup() {
  40. delay(3000); // sanity delay
  41. FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  42. FastLED.setBrightness( BRIGHTNESS );
  43. }
  44.  
  45. void loop()
  46. {
  47. // Add entropy to random number generator; we use a lot of it.
  48. // random16_add_entropy( random());
  49. initializeAudio();
  50. READ_AUDIO(); // run simulation frame
  51. radiate();
  52.  
  53. }
  54.  
  55. void initializeAudio() {
  56. pinMode(MSGEQ7_LEFT_PIN, INPUT);
  57. pinMode(MSGEQ7_RIGHT_PIN, INPUT);
  58. pinMode(MSGEQ7_RESET_PIN, OUTPUT);
  59. pinMode(MSGEQ7_STROBE_PIN, OUTPUT);
  60. digitalWrite(MSGEQ7_RESET_PIN, LOW);
  61. digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
  62. }
  63.  
  64. void READ_AUDIO() {
  65.  
  66. float left_volume = 0.0;
  67. float right_volume = 0.0;
  68. int mono_volume = 0;
  69. float mono_factor = 0;
  70.  
  71.  
  72. digitalWrite(MSGEQ7_RESET_PIN, HIGH);
  73. digitalWrite(MSGEQ7_RESET_PIN, LOW);
  74. delayMicroseconds(1);
  75.  
  76. for (int band = 0; band < 7; band++)
  77. {
  78.  
  79. digitalWrite(MSGEQ7_STROBE_PIN, LOW);
  80. delayMicroseconds(36);
  81.  
  82. prev_value = left[band];
  83. left_value = analogRead(MSGEQ7_LEFT_PIN);
  84. left_value = constrain(left_value, filter_min, filter_max);
  85. left_value = map(left_value, filter_min, filter_max, 0, 255);
  86. left[band] = prev_value + (left_value - prev_value) * lowPass_audio;
  87. left_volume += left[band];
  88.  
  89.  
  90. prev_value = right[band];
  91. right_value = analogRead(MSGEQ7_RIGHT_PIN);
  92. right_value = constrain(right_value, filter_min, filter_max);
  93. right_value = map(right_value, filter_min, filter_max, 0, 255);
  94. right[band] = prev_value + (right_value - prev_value) * lowPass_audio;
  95. right_volume += right[band];
  96.  
  97. digitalWrite(MSGEQ7_STROBE_PIN, HIGH);
  98. delayMicroseconds(1);
  99.  
  100. mono[band] = (left[band] + right[band]) * 0.5;
  101. mono_volume += mono[band];
  102.  
  103. }
  104.  
  105. mono_factor = float(HALF_POS) / mono_volume;
  106.  
  107. for (int band = 0; band < 7; band++)
  108. {
  109. mapped[band] = mono[band] * mono_factor;
  110. }
  111. }
  112.  
  113.  
  114.  
  115.  
  116. void radiate() {
  117.  
  118. zero = mono[0];
  119. three = mono[4];
  120. six = mono[6];
  121.  
  122. leds[HALF_POS] = CRGB(zero, three, six);
  123.  
  124. // high frequencies = blue = FAST SPEED
  125. EVERY_N_MILLISECONDS(11) {
  126. for (int i = NUM_LEDS; i > HALF_POS; i--) {
  127. leds[i].blue = leds[i - 1].blue;
  128. }
  129. }
  130.  
  131. // mid frequencies = green = MEDIUM SPEED
  132. EVERY_N_MILLISECONDS(30) {
  133. for (int i = NUM_LEDS; i > HALF_POS; i--) {
  134. leds[i].green = leds[i - 1].green;
  135. }
  136. }
  137.  
  138. // low frequencies = red = SLOW SPEED
  139. EVERY_N_MILLISECONDS(58) {
  140. for (int i = NUM_LEDS; i > HALF_POS; i--) {
  141. leds[i].red = leds[i - 1].red;
  142. }
  143. }
  144.  
  145. // for mirroring to the other half of the strip
  146. for (int i = NUM_LEDS; i > HALF_POS; i--) {
  147. leds[HALF_POS - (i - HALF_POS)] = leds[i - 1];
  148. }
  149. FastLED.show();
  150. }
Advertisement
Add Comment
Please, Sign In to add comment