Mic responsive RGB led strip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

neopixel-rainbow-organ.ino 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <Adafruit_NeoPixel.h>
  2. #ifdef __AVR__
  3. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  4. #endif
  5. #define DEBUG true
  6. #define LED_COUNT 9 // Number of LEDs in strip
  7. #define BUFFSIZE 72 // at least LED_COUNT!
  8. #define HUESTEP ((65536/LED_COUNT)+23)
  9. #define BASELINE_FACTOR 0.5
  10. #define DECAY_FACTOR 0.2
  11. #define MICPIN A7 // microphone
  12. #define LED_PIN 4
  13. // Declare our NeoPixel strip object:
  14. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800);
  15. // Argument 1 = Number of pixels in NeoPixel strip
  16. // Argument 2 = Arduino pin number (most are valid)
  17. // Argument 3 = Pixel type flags, add together as needed:
  18. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  19. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  20. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  21. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  22. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  23. #define AVGSIZE 4
  24. int rawBuff[BUFFSIZE] = {0};
  25. int avgBuff[BUFFSIZE] = {0};
  26. int index = 0, hue = 0;
  27. unsigned long lastTime, thisTime;
  28. void setup() {
  29. if (DEBUG) {
  30. Serial.begin(230400);
  31. }
  32. lastTime = millis();
  33. index = 0;
  34. strip.begin(); // Initialize pins for output
  35. strip.show(); // Turn all LEDs off ASAP
  36. }
  37. void loop() {
  38. thisTime = millis();
  39. rawBuff[index] = analogRead(MICPIN);
  40. int nextIndex = (index + 1) % BUFFSIZE;
  41. int minVal = 1024, maxVal = -1;
  42. for (int i = 0; i < BUFFSIZE; i++) {
  43. avgBuff[i] = 0;
  44. for (int j = 0; j < AVGSIZE ; j++) {
  45. avgBuff[i] += rawBuff[(i + BUFFSIZE - j) % BUFFSIZE];
  46. }
  47. avgBuff[i] /= AVGSIZE;
  48. minVal = min(minVal, avgBuff[i]);
  49. maxVal = max(maxVal, avgBuff[i]);
  50. }
  51. int baseline;
  52. for (int i = 0; i < LED_COUNT; i++) {
  53. int value = 0;
  54. if (i) {
  55. value = (BASELINE_FACTOR * baseline) +
  56. ((1.0 - BASELINE_FACTOR) *
  57. constrain(
  58. map(avgBuff[(index + BUFFSIZE - i) % BUFFSIZE], minVal, maxVal , -255, 255),
  59. 0, 255) *
  60. pow(1.0 - DECAY_FACTOR, i));
  61. } else {
  62. baseline = constrain(
  63. map(avgBuff[(index + BUFFSIZE - i) % BUFFSIZE], minVal, maxVal , -255, 255),
  64. 0, 255);
  65. value = baseline;
  66. }
  67. strip.setPixelColor(i, strip.ColorHSV(hue , 255, value));
  68. hue = (hue + HUESTEP) % 65536;
  69. if (DEBUG) {
  70. Serial.print(minVal);
  71. Serial.print(' ');
  72. Serial.print(maxVal);
  73. Serial.print(' ');
  74. Serial.print(baseline);
  75. Serial.print(' ');
  76. Serial.print(value);
  77. Serial.print('\n');
  78. }
  79. }
  80. strip.show();
  81. index = nextIndex;
  82. lastTime = thisTime;
  83. delay(10);
  84. }