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