Created
March 25, 2017 16:15
-
-
Save jonathanprozzi/953ec83f17ffcc201d008e4ad4ef6527 to your computer and use it in GitHub Desktop.
Code for the Noise Monitor project in the Arduino for Educators workshop.
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
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz) | |
unsigned int sample; | |
int greenLED = 13; | |
int yellowLED = 12; | |
int redLED = 11; | |
void setup() | |
{ | |
pinMode(greenLED, OUTPUT); | |
pinMode(yellowLED, OUTPUT); | |
pinMode(redLED, OUTPUT); | |
Serial.begin(9600); | |
} | |
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 | |
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; // convert to volts | |
// volts = volts *100; | |
Serial.println(sample); | |
if (sample < 535) { | |
digitalWrite(yellowLED, LOW); | |
digitalWrite(redLED,LOW); | |
digitalWrite(greenLED, HIGH); | |
} | |
else if (sample > 535 && sample < 800) { | |
digitalWrite(redLED, LOW); | |
digitalWrite(greenLED, HIGH); | |
digitalWrite(yellowLED, HIGH); | |
delay(2000); | |
} | |
if (sample > 800) { | |
digitalWrite(greenLED, HIGH); | |
digitalWrite(yellowLED, HIGH); | |
digitalWrite(redLED, HIGH); | |
delay(2000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment