-
Notifications
You must be signed in to change notification settings - Fork 8
/
sound.ino
57 lines (47 loc) · 1.24 KB
/
sound.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
//thanks to @tolribeiro
//http://toribeiro.com/articles/2016-01/sound-level-meter-using-esp8266-arduino-sound-detector
//for inspiration here particularly around calculating dBs
#include <LiquidCrystal.h>
const float dBAnalogQuiet = 10; // calibrated value from analog input (48 dB equivalent)
const float dBAnalogMedium = 12;
const float dBAnalogLoud = 17;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
#define PIN_ANALOG_IN A2
void setup()
{
lcd.begin(16, 2);
pinMode(A2, INPUT);
}
void loop()
{
int value;
float decibelsValueQuiet = 49;
float decibelsValueMedium = 65;
float decibelsValueLoud = 70;
value = analogRead(PIN_ANALOG_IN);
if (value < 13)
{
decibelsValueQuiet += 20 * log10(value/dBAnalogQuiet);
printDecibels("Quiet :)", decibelsValueQuiet);
}
else if ((value > 13) && ( value <= 23) )
{
decibelsValueMedium += log10(value/dBAnalogMedium);
printDecibels("Medium", decibelsValueMedium);
}
else if(value > 22)
{
decibelsValueLoud += log10(value/dBAnalogLoud);
printDecibels("Loud :(", decibelsValueLoud);
}
delay(500);
}
void printDecibels (const char* volume, float dBs)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(volume);
lcd.setCursor(0, 1);
lcd.print(dBs);
lcd.print("dB");
}