Created
May 6, 2018 22:40
-
-
Save IOT-123/bcb714fb9e901820fff7ae086becc655 to your computer and use it in GitHub Desktop.
Test file for
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
/**************************************** | |
Example Sound Level Sketch for the | |
Adafruit Microphone Amplifier | |
****************************************/ | |
#define USE_SOFTWARE_SERIAL 1 | |
#if (USE_SOFTWARE_SERIAL) | |
#include <SoftwareSerial.h> | |
#define PIN_RX 1 | |
#define PIN_TX 4 | |
SoftwareSerial mySerial(PIN_RX, PIN_TX); | |
#endif | |
#define PIN_SENSOR A3 | |
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz) | |
unsigned int sample; | |
void setup() | |
{ | |
mySerial.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(PIN_SENSOR); | |
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 | |
mySerial.print(signalMin); | |
mySerial.print(" "); | |
mySerial.print(signalMax); | |
mySerial.print(" "); | |
mySerial.println(peakToPeak); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment