Hi,
This is a modified code from the 'Arduino Cookbook' Book - page 186
It currently prints loads to the serial monitor! How could i edit this so it prints one reading a second?
Ive thought about using millis() but i cant think where to implement it!
const int middleValue = 512; //the middle of the range of analog values
const int numberOfSamples = 128; //how many readings will be taken each time
int sample; //the value read from the microphone each time
long signal; //the reading once you have removed the DC offset
long averageReading; //the average of that loop of readings
long runningAverage = 0; //the running average of calculated values
const int averagedOver = 16; //how quickly new values affect running average
//bigger numbers mean slower
void setup()
{
Serial.begin(9600);
}
void loop() {
long sumOfSquares = 0;
for (int i=0; i<numberOfSamples; i++) { //take many readings and average them
sample = analogRead(0); //take a reading
signal = (sample - middleValue); //work out its offset from center
signal *= signal; //square it to make all the values positive
sumOfSquares += signal; //add to the total
}
averageReading = sumOfSquares/numberOfSamples; //calculate running average
runningAverage=(((averagedOver-1)*runningAverage)+averageReading)/averagedOver;
Serial.println(runningAverage);
}