-
Notifications
You must be signed in to change notification settings - Fork 14
/
InAudioSensor.ino
50 lines (40 loc) · 1.2 KB
/
InAudioSensor.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
#ifdef USE_AUDIO_INPUT
const int numReadings = 8;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
void loopAudioSensor() {
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(A0);
// add the reading to the total:
total= total + readings[index++];
// advance to the next position in the array:
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
if (average > maxVal) {
maxVal = average;
}
//calculate current volume vs. maximal volume
audioVol = (1.0f/maxVal)*average;
//decrease maximum volume, adjust it
if (maxVal>2) {
maxVal-=2;
} else if (maxVal>1) {
maxVal--;
}
/*
#ifdef USE_SERIAL_DEBUG
Serial.print("avg:");
Serial.print(average, DEC);
Serial.print(" \tav:");
Serial.println(audioVol, DEC);
#endif
*/
}
#endif