Last active
August 28, 2019 08:56
-
-
Save jonathanprozzi/56ba435e8ab04ef7cfed1683e27e0dfc to your computer and use it in GitHub Desktop.
Arduino Classroom Noise-o-Meter
This file contains 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
/**************************************** | |
Classroom Noise-o-Meter | |
Modified code from the Example Sound Level Sketch for the | |
Adafruit Microphone Amplifier | |
****************************************/ | |
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz) | |
unsigned int sample; | |
int greenLED = 13; // choose your LEDs. helpful to descriptively label them | |
int yellowLED = 12; | |
int redLED = 11; | |
void setup() | |
{ | |
pinMode(greenLED, OUTPUT); | |
pinMode(yellowLED, OUTPUT); | |
pinMode(redLED, OUTPUT); | |
Serial.begin(9600); // Serial monitor is helpful to see the readings, but not vital | |
} | |
void loop() | |
{ | |
unsigned long startMillis= millis(); // Start of sample window | |
unsigned int peakToPeak = 0; // peak-to-peak level | |
unsigned int signalMax = 0; | |
unsigned int signalMin = 1024; | |
// collect data for 50 mS | |
// this is helpful to ensure accurate readings. | |
while (millis() - startMillis < sampleWindow) | |
{ | |
sample = analogRead(0); | |
if (sample < 1024) // toss out spurious readings | |
{ | |
if (sample > signalMax) | |
{ | |
signalMax = sample; // save just the max levels | |
} | |
else if (sample < signalMin) | |
{ | |
signalMin = sample; // save just the min levels | |
} | |
} | |
} | |
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude | |
double volts = (peakToPeak * 5.0) / 1024; // uncomment if you want to convert to volts | |
Serial.println(volts); | |
//delay(100); | |
/* this is the logic for activating the LEDs based on the readings | |
* you'll want to tweak the levels based on the average volume levels | |
* in your space. | |
* the delay causes the LED to pause when lit. experiment with altering the delay time! | |
*/ | |
if (volts < 0.5) { | |
digitalWrite(yellowLED, LOW); | |
digitalWrite(redLED,LOW); | |
digitalWrite(greenLED, HIGH); | |
} | |
else if (volts > 0.5 && volts < 1.0) { | |
digitalWrite(redLED, LOW); | |
digitalWrite(greenLED, HIGH); | |
digitalWrite(yellowLED, HIGH); | |
} | |
else if (volts > 1.0 && volts < 2.0) { | |
digitalWrite(redLED, HIGH); | |
digitalWrite(greenLED, HIGH); | |
digitalWrite(yellowLED, HIGH); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment