-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreamer.ino
307 lines (274 loc) · 6.46 KB
/
screamer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <Adafruit_NeoPixel.h>
#define LED_PIN 5
#define LED_COUNT 14
// Brightness min/max 0/255
#define BRIGHTNESS 100
// Time in milliseconds between updates
// About 50 is more responsive and 100 is more steady.
#define INTERVAL_UPDATE 50
#define MIC_PIN A0
// About 10 keeps most background noise from lighting LEDs.
#define NOISE_LEVEL 10
#define INTERVAL_UPDATE_AGC 50
// LED constructor
// Argument 1 = Number of pixels in NeoPixel leds
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel leds(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ400);
// LED variables
unsigned long last_change = 0;
unsigned long last_change_agc = 0;
unsigned long now = 0;
unsigned long scream_start;
unsigned long scream_length;
uint8_t led_warm_up;
// Microphon variables
uint8_t mic = MIC_PIN;
uint8_t micOut;
uint16_t average;
uint16_t sound_level;
uint16_t sound_level_max_interval;
uint16_t sound_level_max;
uint16_t sound_level_max_average;
uint16_t sound_level_average;
uint16_t i;
// State machine
enum {
VU_METER_NORMAL,
VU_METER_GATER_POWER,
WARM_UP,
POWER_GATHERED,
POWER_DISCHARGED,
};
uint8_t state_machine = VU_METER_NORMAL;
void setup() {
Serial.begin(9600);
leds.begin(); // INITIALIZE NeoPixel leds object (REQUIRED)
leds.show(); // Turn OFF all pixels ASAP
leds.setBrightness(BRIGHTNESS);
last_change = millis();
last_change_agc = millis();
}
void loop() {
now = millis();
switch(state_machine)
{
case VU_METER_NORMAL:
if(vu_meter() > 4)
{
state_machine = VU_METER_GATER_POWER;
}
scream_start = now;
break;
case VU_METER_GATER_POWER:
if(vu_meter() == 0)
{
last_change = now;
led_warm_up = 0;
state_machine = WARM_UP;
}
else
{
// Count up power (length of sound)
scream_length = millis() - scream_start;
}
break;
case WARM_UP:
if(warm_up() > 0)
{
led_warm_up = 0;
state_machine = POWER_GATHERED;
}
break;
case POWER_GATHERED:
if(power_gathered() > 0)
{
led_warm_up = 0;
state_machine = POWER_DISCHARGED;
}
break;
case POWER_DISCHARGED:
if(power_discharged() > 0)
{
state_machine = VU_METER_NORMAL;
}
break;
}
Serial.print("state:");
Serial.println(state_machine);
}
// VU Meter effect
uint8_t vu_meter()
{
micOut = analogRead(mic);
// Remove DC component by tracking slow average
average = (micOut + average * 9) / 10;
sound_level = abs(micOut - average);
// Serial.print("mic:");
// Serial.println(micOut);
// Serial.print("avg:");
// Serial.println(average);
// Serial.print("lev:");
// Serial.println(sound_level);
// Remove noise
if(sound_level < NOISE_LEVEL)
{
sound_level = 0;
}
else
{
sound_level -= NOISE_LEVEL;
}
// // Automatic Gain Control
// if(now > last_change_agc)
// {
// last_change_agc = now + INTERVAL_UPDATE_AGC;
// sound_level_average = (sound_level_average + sound_level * 99) / 100;
// Serial.print("snd lev avg:");
// Serial.println(sound_level_average);
// // If we go beyond the number of LEDs invoke AGC control
// if (sound_level_average > LED_COUNT)
// {
// sound_level = sound_level * (LED_COUNT / (float)sound_level_average);
// }
// }
if(now > last_change)
{
last_change = now + INTERVAL_UPDATE;
// Track peak levels
if(sound_level_max < sound_level_max_interval)
{
sound_level_max = sound_level_max_interval;
if(sound_level_max >= LED_COUNT)
{
sound_level_max = LED_COUNT - 1;
}
sound_level_max_average = sound_level_max;
}
sound_level_max = (sound_level_max + sound_level_max_average * 9) / 10;
if(sound_level_max_average > 0)
{
sound_level_max_average--;
}
// Light up the LEDs
for(i = 0; i < LED_COUNT; i++)
{
// Display sound level
if(i < sound_level_max_interval)
{
leds.setPixelColor(i, 100, 0, 0);
}
else
{
leds.setPixelColor(i, 0, 0, 0);
}
// Display peak sound level
if((i == sound_level_max) && (i > 0))
{
leds.setPixelColor(i, 0, 100, 0);
}
}
leds.show();
sound_level_max_interval = 0;
}
else
{
// Track the maxmimum until the next LED update.
if(sound_level_max_interval < sound_level)
{
sound_level_max_interval = sound_level;
}
}
return sound_level_max;
}
// Warmup effect
uint8_t warm_up(void)
{
uint8_t return_value = 0;
if(now > last_change)
{
last_change = now + 25;
led_warm_up++;
// Light up the LEDs
for(i = 0; i < LED_COUNT; i++)
{
leds.setPixelColor(i, led_warm_up, led_warm_up * 0.8f, 0);
}
leds.show();
// Serial.print("glow:");
// Serial.println(led_warm_up);
if(led_warm_up > 10)
{
return_value = 1;
}
else
{
return_value = 0;
}
}
return return_value;
}
// Display the gathered power effect
uint8_t power_gathered(void)
{
uint8_t return_value = 0;
if(now > last_change)
{
last_change = now + 150;
led_warm_up++;
// Light up the LEDs
for(i = 0; i < LED_COUNT; i++)
{
if(i < led_warm_up)
{
if((scream_length / 100) > i)
{
leds.setPixelColor(i, 255, 0, 0);
}
}
}
leds.show();
if(led_warm_up > LED_COUNT)
{
return_value = 1;
}
else
{
return_value = 0;
}
}
return return_value;
}
uint8_t power_discharged(void)
{
uint8_t return_value = 0;
if(now > last_change)
{
last_change = now + 25;
led_warm_up++;
// Light off all the LEDs
leds.setBrightness(100 - led_warm_up);
leds.show();
if(led_warm_up >= 100)
{
// Light off all the LEDs
for(i = 0; i < LED_COUNT; i++)
{
leds.setPixelColor(i, 0, 0, 0);
}
leds.setBrightness(BRIGHTNESS);
leds.show();
return_value = 1;
}
else
{
return_value = 0;
}
}
return return_value;
}