Skip to content

Instantly share code, notes, and snippets.

@IOT-123
Created May 6, 2018 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IOT-123/9c3a60c1291fa4c604acb018d1a9012f to your computer and use it in GitHub Desktop.
Save IOT-123/9c3a60c1291fa4c604acb018d1a9012f to your computer and use it in GitHub Desktop.
Test file for
/****************************************
Example Sound Level Sketch for the
Adafruit Microphone Amplifier
****************************************/
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int avg = 0;
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(A0);
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
avg = signalMin + (peakToPeak/2);
double volts = (peakToPeak * 5.0) / 1024; // convert to volts
Serial.print(signalMin);
Serial.print(" ");
Serial.print(signalMax);
Serial.print(" ");
Serial.print(avg);
Serial.print(" ");
Serial.println(peakToPeak);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment