/*
This is a demo of the FreqMeasure library
On an Uno, jumpering pin 3 to pin 8 reports the PWM frequency of 490.20Hz
490.20
490.20
490.20
490.20
490.20
...
With the pin floating with a loose jumper it reports 60Hz hum.
It does not report with pin 8 grounded.
On Wokwi this doesn't appear to work.
I reported this as https://github.com/wokwi/wokwi-features/issues/352 and it is being
tracked in https://github.com/wokwi/avr8js/issues/125
Code: https://wokwi.com/projects/330562550517203540
This is a modificaton of the https://www.pjrc.com/teensy/td_libs_FreqMeasure.html demo code
to add a PWM output on pin 3.
It adds some code to track pin 8;
With the jumper to pin 13 and the switch, you can see the pwm output on the built-in LED and toggle
the connection.
*/
#include <FreqMeasure.h> // https://www.pjrc.com/teensy/td_libs_FreqMeasure.html
void setup() {
Serial.begin(115200);
FreqMeasure.begin();
pinMode(3,OUTPUT); analogWrite(3,127); // 50% duty cycle on pin 3
}
double sum=0;
int count=0;
static uint32_t count8=0;
void loop() {
if (FreqMeasure.available()) {
// average several reading together
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
float frequency = FreqMeasure.countToFrequency(sum / count);
Serial.println(frequency);
sum = 0;
count = 0;
}
}
count8 += digitalRead(8)==LOW;
report();
}
void report(){
static uint32_t last = -1000;
if(millis() - last >= 1000){
last+=1000;
Serial.print("count8: ");
Serial.println(count8);
}
}