-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound Detector Sparkfun
58 lines (47 loc) · 1.35 KB
/
Sound Detector Sparkfun
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
// The following link contains the original work. https://github.com/sparkfun/Sound_Detector
//GND → GND
//VCC → 5V
//Gate → Pin 2
//Envelope → A1
#define PIN_GATE_IN 2
#define IRQ_GATE_IN 0
#define PIN_LED_OUT 13
#define PIN_ANALOG_IN A1
// soundISR()
// This function is installed as an interrupt service routine for the pin
// change interrupt. When digital input 2 changes state, this routine
// is called.
// It queries the state of that pin, and sets the onboard LED to reflect that
// pin's state.
void soundISR()
{
int pin_val;
pin_val = digitalRead(PIN_GATE_IN);
digitalWrite(PIN_LED_OUT, pin_val);
}
void setup()
{
Serial.begin(9600);
// Configure LED pin as output
pinMode(PIN_LED_OUT, OUTPUT);
// configure input to interrupt
pinMode(PIN_GATE_IN, INPUT);
attachInterrupt(IRQ_GATE_IN, soundISR, CHANGE);
// Display status
Serial.println("Initialized");
}
void loop()
{
int value;
// Check the envelope input
value = analogRead(PIN_ANALOG_IN);
// Convert envelope value into a message
//Was considered as minimum at 21 dB. Then we proceeded to write down the Voltage and the
//following equation was solved. "8" is the registred value in 21 dB.
//21=20*log(8/x) x~2.79
Serial.print(20*log(value/2.8));
Serial.print(" dB");
Serial.print('\n');
// pause for 1 second
delay(100);
}