Understanding code from Frequency measurement

Hey!
Could anyone explain the lines under “if” ?

#include <[color=#cc6600]FreqMeasure[/color].h>

[color=#cc6600]void[/color] [color=#cc6600][b]setup[/b][/color]() {
  [color=#cc6600][b]Serial[/b][/color].[color=#cc6600]begin[/color](57600);
  [color=#cc6600]FreqMeasure[/color].[color=#cc6600]begin[/color]();
}

[color=#cc6600]double[/color] sum=0;
[color=#cc6600]int[/color] count=0;

[color=#cc6600]void[/color] [color=#cc6600][b]loop[/b][/color]() {
  [color=#cc6600]if[/color] ([color=#cc6600]FreqMeasure[/color].[color=#cc6600]available[/color]()) {
    [color=#7e7e7e]// average several reading together[/color]
    sum = sum + [color=#cc6600]FreqMeasure[/color].[color=#cc6600]read[/color]();
    count = count + 1;
    [color=#cc6600]if[/color] (count > 30) {
      [color=#cc6600]float[/color] frequency = [color=#cc6600]FreqMeasure[/color].[color=#cc6600]countToFrequency[/color](sum / count);
      [color=#cc6600][b]Serial[/b][/color].[color=#cc6600]println[/color](frequency);
      sum = 0;
      count = 0;
    }
  }
}
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
    }

See

float FreqMeasureClass::countToFrequency(uint32_t count)
{
#if defined(__AVR__)
 return (float)F_CPU / (float)count;
#elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISK)
 return (float)F_BUS / (float)count;
#elif defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISL)
 return (float)(F_PLL/2) / (float)count;
#elif defined(__arm__) && defined(TEENSYDUINO) && (defined(__IMXRT1052__) || defined(__IMXRT1062__))
 return (float)F_BUS_ACTUAL / (float)count;
#endif
}

NOTE that I am not familiar with the library

By the way, what does the topic title have to do with your question ?

UKHeliBob:

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
   }




See


float FreqMeasureClass::countToFrequency(uint32_t count)
{
#if defined(AVR)
return (float)F_CPU / (float)count;
#elif defined(arm) && defined(TEENSYDUINO) && defined(KINETISK)
return (float)F_BUS / (float)count;
#elif defined(arm) && defined(TEENSYDUINO) && defined(KINETISL)
return (float)(F_PLL/2) / (float)count;
#elif defined(arm) && defined(TEENSYDUINO) && (defined(IMXRT1052) || defined(IMXRT1062))
return (float)F_BUS_ACTUAL / (float)count;
#endif
}




NOTE that I am not familiar with the library

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

The count variable used in the function is the value of the parameter passed to it and is not the same as the count variable in loop(). The library code is at FreqMeasure/FreqMeasure.cpp at master · PaulStoffregen/FreqMeasure · GitHub and, of course, on your PC

What exactly is it that you are trying to do and what range of frequencies are involved ?

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

Still not sure why sum / count is in the colon after FreqMeasure.countToFrequency

Looks like a language problem here

To me a colon is this :
Do you mean brackets like this ( )

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.

UKHeliBob:
Looks like a language problem here

To me a colon is this :
Do you mean brackets like this ( )

I meant brackets, excuse my Norwegian to English translation :sweat_smile:

excuse my Norwegian to English translation

No problem. I certainly don't know what the Norwegian for brackets is !

UKHeliBob:
No problem. I certainly don't know what the Norwegian for brackets is !

But I think the answer to my question is the one Johnwasser typed?