I have a project where I need to count inputs from a hall-effect sensor, determine RPMs, and control a throttle with a servo through PID regulation. BUT....
- Using the streamlined code below and monitoring serial, I get crazy high numbers on the variable "count".
- If I rem-out the "myservo.attach" line, the FreqCount (reads pin 5) function works fine.
- If I attach the servo or do not attach the servo, the result is the same.
SO... Is this a "noise thing"? Any suggestions for troubleshooting?
Many thanks!
/* FreqCount - Example with serial output
- FreqCount Library, for Measuring Frequencies in the 1 kHz to 5 MHz Range
- This example code is in the public domain.
*/
#include <FreqCount.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
FreqCount.begin(1000);//Begin frequency counting. GateInterval is the time in milliseconds for each measurement. Using 1000 provides direct frequency output without mulitplying or dividing by a scale factor.
//Lower gate interval to increase sampling frequency, but multiple the calculated RPM accordingly, as needed.
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
if (FreqCount.available()) //Returns true when a new measurement is available. Only a single measurement is buffered, so it must be read before the next gating interval.
{
unsigned long count = FreqCount.read();//Returns the most recent measurement, an unsigned long containing the number of rising edges seen within the gating interval.
Serial.println(count);
}
}