-
Notifications
You must be signed in to change notification settings - Fork 0
/
KY-037.ino
39 lines (39 loc) · 1004 Bytes
/
KY-037.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
/*
KY-037 Sound Detection Sensor + Arduino
modified on 16 Apr 2019
by Mohammadreza Akbari @ Electropeak
https://electropeak.com/learn/
*/
int sensor_value = 0;
int threshold = 540; //Enter Your threshold value here
int abs_value = 0;
int ledCount = 10; //number of Bargraph LEDs
int bargraph[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // Bargraph pins
void setup() {
Serial.begin(9600); // setup serial
for (int i = 0; i <= ledCount; i++) // Define bargraph pins OUTPUT
{
pinMode(bargraph[i], OUTPUT);
}
for (int i = 0; i <= 9; i++)
{
digitalWrite(i, LOW);
}
}
void loop() {
sensor_value = analogRead(A0);
abs_value = abs(sensor_value - threshold);
int ledLevel = map(abs_value, 0, (1024 - threshold), 0, ledCount);
for (int i = 0; i < ledCount; i++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (i < ledLevel) {
digitalWrite(bargraph[i], HIGH);
Serial.println(i);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(bargraph[i], LOW);
}
}
}