Add each reading to a running total as you take it then calculate the average at the end of the hour. Make sure that you choose the right type of variable for the running total.
What "readings"? The average of an audio signal is always zero. The instantaneous value (one sample taken periodically) has little relationship to the audio waveform unless the Nyquist criterion is met (2*f samples/t).
what do you mean "Add each reading to a running total" and how can i go about this
At the start of the hour set the total to zero. Then as each reading is taken add it to the total. At the end of the hour divide the total by the number of readings. There is no need to store each reading.
Max level readings are what is being serial printed (e.g 2.7, 2.1, 2.4, 2.4, 2.3, 2.7 (volts))
After one hour i need the calculated average level (e.g 2.5), like an SPL meter
what do you mean "Add each reading to a running total" and how can i go about this
Does your microphone module have a built in peak detector, or does it output audio? If it's audio, how do you know that the peak occurs at the time when you happen to sample it? Also, how do you handle the DC offset (A/D is 0-5V typically, while audio is AC, and has +/- swings)?
UKHeliBob:
At the start of the hour set the total to zero. Then as each reading is taken add it to the total. At the end of the hour divide the total by the number of readings.
Do i need to do this manually? Because I need the code to do all of that for me.
The idea is someone could press a button when they get in the studio and monitor their hearing exposure. (This is a prototype for a college project remember)
When the 'average number' is stored in the SQL database it will then be displayed on a localhost website via PHP
If you want someone to write the code for you then ask a moderator to move this thread to Gigs and Collaborations or have a go yourself and get help here.
aarg:
Does your microphone module have a built in peak detector, or does it output audio? If it's audio, how do you know that the peak occurs at the time when you happen to sample it? Also, how do you handle the DC offset (A/D is 0-5V typically, while audio is AC, and has +/- swings)?
Ive just lifted the code from the adafruit website.
Aarg, It doesn't really matter about when it samples as its only a rubbish prototype. Your questions are slighlty confusing to me e.g. "Also, how do you handle the DC offset (A/D is 0-5V typically, while audio is AC, and has +/- swings)?". Im using an arduino that has a 0 - 5 v AD converter... haha
Also i dont want the code to be written for me, i just want to be pointed in the right direction of research
/****************************************
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 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 * 3.3) / 1024; // convert to volts
Serial.println(volts);
}