PaulStoffregen / FreqMeasureMulti

Measures the elapsed time during each cycle of up to 8 input frequencies.
34 stars 9 forks source link

Incorrect frequency reading #15

Closed craigyjp closed 1 year ago

craigyjp commented 1 year ago

Description

I have a nice square wave of around 100hz going into pin 22 of a Teensy 4.1 but the demo code is reporting a frequency of around 800hz regardless of what I do.

Steps To Reproduce Problem

Trying to read the squarewave output of a VCO from 16hz to 8khz, readings are super inconsistent at lower frequencies.

Hardware & Software

Board Shields / modules used none Arduino IDE version 2.10 Teensyduino version Operating system & version Any other software or hardware?

Arduino Sketch

/* FreqMeasureMulti - Example with serial output

// Measure 3 frequencies at the same time! :-) FreqMeasureMulti freq1; FreqMeasureMulti freq2; FreqMeasureMulti freq3;

define MUX_ENABLE 37

void setup() { Serial.begin(57600); while (!Serial) ; // wait for Arduino Serial Monitor delay(10); pinMode(MUX_ENABLE, OUTPUT); digitalWrite(MUX_ENABLE, HIGH); Serial.println("FreqMeasureMulti Begin"); delay(10); freq1.begin(22); freq2.begin(2); freq3.begin(3); }

float sum1=0, sum2=0, sum3=0; int count1=0, count2=0, count3=0; elapsedMillis timeout;

void loop() { if (freq1.available()) { sum1 = sum1 + freq1.read(); count1 = count1 + 1; } if (freq2.available()) { sum2 = sum2 + freq2.read(); count2 = count2 + 1; } if (freq3.available()) { sum3 = sum3 + freq3.read(); count3 = count3 + 1; } // print results every half second if (timeout > 500) { if (count1 > 0) { Serial.print(freq1.countToFrequency(sum1 / count1)); } else { Serial.print("(no pulses)"); } Serial.print(", "); if (count2 > 0) { Serial.print(freq2.countToFrequency(sum2 / count2)); } else { Serial.print("(no pulses)"); } Serial.print(", "); if (count3 > 0) { Serial.print(freq3.countToFrequency(sum3 / count3)); } else { Serial.print("(no pulses)"); } Serial.println(); sum1 = 0; sum2 = 0; sum3 = 0; count1 = 0; count2 = 0; count3 = 0; timeout = 0; } }

Errors or Incorrect Output

If you see any errors or incorrect output, please show it here. Please use copy & paste to give an exact copy of the message. Details matter, so please show (not merely describe) the actual message or error exactly as it appears.