/
ExampleCode.ino
78 lines (63 loc) · 2.24 KB
/
ExampleCode.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
/***************************
* Example Sketch for the SparkFun MEMS Microphone Breakout Board
* Written by jenfoxbot <jenfoxbot@gmail.com>
* Code is open-source, beer/coffee-ware license.
*/
// Connect the MEMS AUD output to the Arduino A0 pin
int mic = A0;
// Variables to find the peak-to-peak amplitude of AUD output
const int sampleTime = 50;
int micOut;
void setup() {
Serial.begin(9600);
}
void loop() {
int micOutput = findPTPAmp();
VUMeter(micOutput);
}
// Find the Peak-to-Peak Amplitude Function
int findPTPAmp(){
// Time variables to find the peak-to-peak amplitude
unsigned long startTime= millis(); // Start of sample window
unsigned int PTPAmp = 0;
// Signal variables to find the peak-to-peak amplitude
unsigned int maxAmp = 0;
unsigned int minAmp = 1023;
// Find the max and min of the mic output within the 50 ms timeframe
while(millis() - startTime < sampleTime)
{
micOut = analogRead(mic);
if( micOut < 1023) //prevent erroneous readings
{
if (micOut > maxAmp)
{
maxAmp = micOut; //save only the max reading
}
else if (micOut < minAmp)
{
minAmp = micOut; //save only the min reading
}
}
}
PTPAmp = maxAmp - minAmp; // (max amp) - (min amp) = peak-to-peak amplitude
double micOut_Volts = (PTPAmp * 3.3) / 1024; // Convert ADC into voltage
//Uncomment this line for help debugging (be sure to also comment out the VUMeter function)
//Serial.println(PTPAmp);
//Return the PTP amplitude to use in the soundLevel function.
// You can also return the micOut_Volts if you prefer to use the voltage level.
return PTPAmp;
}
// Volume Unit Meter function: map the PTP amplitude to a volume unit between 0 and 10.
int VUMeter(int micAmp){
int preValue = 0;
// Map the mic peak-to-peak amplitude to a volume unit between 0 and 10.
// Amplitude is used instead of voltage to give a larger (and more accurate) range for the map function.
// This is just one way to do this -- test out different approaches!
int fill = map(micAmp, 23, 750, 0, 10);
// Only print the volume unit value if it changes from previous value
while(fill != preValue)
{
Serial.println(fill);
preValue = fill;
}
}