-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMicrophone.ino
49 lines (41 loc) · 965 Bytes
/
Microphone.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
#include "./Microphone.h"
Microphone_t microphone;
static void MIC_Update_();
static const uint8_t MIC_SAMPLING_NUM = 10;
static const uint32_t MIC_MEDIAN = 512;
void MIC_Init() {
microphone.average = 0;
microphone.maximum = 0;
}
static void MIC_Update_()
{
// Serial.println(millis());
uint32_t average = 0;
uint32_t maximum = 0;
for (int8_t i = 0; i < MIC_SAMPLING_NUM; i++) {
uint32_t value = abs(analogRead(PIN_MIC) - MIC_MEDIAN);
average += value;
if (maximum < value) {
maximum = value;
}
}
microphone.average = average / MIC_SAMPLING_NUM;
microphone.maximum = maximum;
// Serial.println(millis());
}
int MIC_GetAverage() {
MIC_Update_();
return (int)microphone.average;
}
int MIC_GetMaximum() {
MIC_Update_();
return (int)microphone.maximum;
}
void MIC_Print() {
MIC_Update_();
Serial.print(F("Mic= "));
Serial.print(microphone.average);
Serial.print(F(", "));
Serial.print(microphone.maximum);
Serial.println(F(""));
}