void loop()
{
if (FreqMeasure.available()) //if there is a frequency measurement available
{
// average several reading together
sum = sum + FreqMeasure.read(); //read frequency and add it to the running sum
count = count + 1; //increment count
if (count > 30) //if 30 readings have been done
{
float frequency = FreqMeasure.countToFrequency(sum / count); //calculate average frequency
//taking into account the speed of the processor
Serial.println(frequency); //print the average frequency
sum = 0; //zero the running sum for next average
count = 0; //zero the count
}
{
if (FreqMeasure.available()) //if there is a frequency measurement available
{
// average several reading together
sum = sum + FreqMeasure.read(); //read frequency and add it to the running sum
count = count + 1; //increment count
if (count > 30) //if 30 readings have been done
{
float frequency = FreqMeasure.countToFrequency(sum / count); //calculate average frequency
//taking into account the speed of the processor
Serial.println(frequency); //print the average frequency
sum = 0; //zero the running sum for next average
count = 0; //zero the count
}
Thanks for answer!
About this line
float frequency = FreqMeasure.countToFrequency(sum / count);
Does FreqMeasure.countToFrequency read the value from sum and divide it by count? Then create a value?
Does FreqMeasure.countToFrequency read the value from sum and divide it by count?
No
The library seems to work by counting the peaks in the input waveform using an interrupt and storing them in an array
FreqMeasure.countToFrequency() appears to take the array values and return a float, the calculation of which depends on the type of processor that the code is running on and hence its frequency. In the example sketch that you posted the value of the parameter is the average of 30 values hence the use of sum / count
skjermlue:
Could anyone explain the lines under "if" ?
Average 30 readings by summing them in 'sum', counting them in 'count' and, when reaching 30, dividing 'sum' by 'count' (always 30). A function is then used to convert the averaged reading to a frequency.
UKHeliBob:
What exactly is it that you are trying to do and what range of frequencies are involved ?
I'm trying to understand what I'm actually programming, not just copy paste and not knowing what and why I'm doing it.
Still not sure why sum / count is in the colon after FreqMeasure.countToFrequency
skjermlue:
Still not sure why sum / count is in the colon after FreqMeasure.countToFrequency
The result of the calculation 'sum / count' is the average of 30 'readings'. The average is passed to the "countToFrequency" function which converts a 'reading' to a frequency.