Skip to content

Instantly share code, notes, and snippets.

@sixstringsg
Last active May 19, 2020 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixstringsg/95e7b569f6a623144d99f11f4a61d217 to your computer and use it in GitHub Desktop.
Save sixstringsg/95e7b569f6a623144d99f11f4a61d217 to your computer and use it in GitHub Desktop.
/* FreqMeasureMulti - Example with serial output
* http://www.pjrc.com/teensy/td_libs_FreqMeasure.html
*
* This example code is in the public domain.
*
* I connected PIN 4 to PIN 6, PIN 4 to PIN 10 and PIN 3 to PIN 9
* Results for Serial Output "n, 1463.99, 50.0000000000, 341520.84"
*
* Uncomment line 95 to see last value change
*
* There is some jitter in the 50 Hz signal which may be interupts
* but more likely just floating point precision
*/
#include <Servo.h>
#include <FreqMeasureMulti.h>
// Create 3 frequencies
Servo freqOut1;
// Measure 3 frequencies at the same time! :-)
FreqMeasureMulti freq1;
FreqMeasureMulti freq2;
FreqMeasureMulti freq3;
void setup() {
Serial.begin(57600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(10);
Serial.println("FreqMeasureMulti Begin");
delay(10);
// setup Servo (50 Hz PWM) output
freqOut1.attach(3, 1000, 4000);
freqOut1.writeMicroseconds(1000);
// setup PWM output
pinMode(4, OUTPUT);
analogWriteResolution(12); // set as bits (default?)
analogWriteFrequency(4, 1464);
analogWrite(4,2048);
freq1.begin(6);
freq2.begin(9);
freq3.begin(10, FREQMEASUREMULTI_MARK_ONLY);
}
float sum1=0, sum2=0, sum3=0;
int count1=0, count2=0, count3=0;
uint8_t n=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) {
Serial.print(n);
Serial.print(", ");
if (count1 > 0) {
Serial.print(freq1.countToFrequency(sum1 / count1) * 14.28);
} else {
Serial.print("(no pulses)");
}
Serial.print(", ");
if (count2 > 0) {
Serial.print(freq2.countToFrequency(sum2 / count2),DEC);
} else {
Serial.print("(no pulses)");
}
Serial.print(", ");
if (count3 > 0) {
Serial.print(freq3.countToNanoseconds(sum3 / count3));
} else {
Serial.print("(no pulses)");
}
Serial.println();
sum1 = 0;
sum2 = 0;
sum3 = 0;
count1 = 0;
count2 = 0;
count3 = 0;
timeout = 0;
n = (n>99) ? 1 : n+1;
// analogWrite(4,(int) ((float) n/100.0f*2048));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment