Skip to content

Instantly share code, notes, and snippets.

@giantmolecules
Last active June 8, 2017 20:31
Show Gist options
  • Save giantmolecules/53ca7652d89ace787980f0815ce9a2fb to your computer and use it in GitHub Desktop.
Save giantmolecules/53ca7652d89ace787980f0815ce9a2fb to your computer and use it in GitHub Desktop.
/****************************************
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(115200);
}
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
Serial.print("raw:");
Serial.println(peakToPeak);
Serial.print('volts:");
Serial.println(volts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment