Created
November 15, 2018 12:49
-
-
Save dzhlobo/84b494606fdbfe1ce37dfb19261bf27d to your computer and use it in GitHub Desktop.
Arduino loudness detector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int frame_size = 300; | |
long int total = 0; | |
const int NUMBER_OF_LEDS = 6; | |
int leds[NUMBER_OF_LEDS] = {7, 6, 5, 4, 3, 2}; | |
int thresholds[NUMBER_OF_LEDS] = {5, 10, 15, 20, 25, 30}; | |
void setup() { | |
Serial.begin(9600); | |
for (int i = 0; i < NUMBER_OF_LEDS; i++) { | |
pinMode(leds[i], OUTPUT); | |
} | |
} | |
void loop() { | |
int volume = analogRead(A0); | |
long int average = total / frame_size; | |
int current_threshold = 0; | |
while (current_threshold < NUMBER_OF_LEDS && average > thresholds[current_threshold]) { | |
current_threshold++; | |
} | |
show_leds(current_threshold); | |
total = total - average / 2; | |
total = total + volume; | |
Serial.println(average); | |
} | |
void show_leds(int number) { | |
for (int i = 0; i < number; i++) { | |
digitalWrite(leds[i], HIGH); | |
} | |
for (int i = number; i < NUMBER_OF_LEDS; i++) { | |
digitalWrite(leds[i], LOW); | |
} | |
} | |
void run_led(int delay_ms) { | |
for (int i = 0; i < 6; i++) { | |
digitalWrite(leds[i], HIGH); | |
delay(delay_ms); | |
digitalWrite(leds[i], LOW); | |
} | |
} | |
void top_up(int delay_ms) { | |
disable_all(); | |
delay(delay_ms); | |
for (int i = 0; i < 6; i++) { | |
digitalWrite(i, HIGH); | |
delay(delay_ms); | |
} | |
} | |
void disable_all() { | |
for (int i = 0; i < NUMBER_OF_LEDS; i++) { | |
digitalWrite(leds[i], LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment