RE: http://forum.arduino.cc/index.php?topic=64219.0
RE: http://forum.arduino.cc/index.php?topic=557364.0
hello, I has been try this frequency counter code without using any library and It was really great.
But I face a few problem. Since I need to to another task while scanning the frequency.
I need to lock a certain frequency, if the frequency match with the target I need.
I need to do analogPrint (PWM). And this delay function make the PWM bounching.
Can anyone help me with this code?
How to modify it from “delay(sampleTime);” to using Millis?
Sorry for my bad english,
hello everyone, I has been try this frequency counter code without using any library and It was really great.
But I face the problem. Since I need to to another task while scanning the frequency.
I need to lock a certain frequency, if the frequency match with the target I need.
I need to do analogPrint (PWM). And this delay function make the PWM bounching.
Can anyone help me with this code?
How to modify it from “delay(sampleTime);” to using Millis?
// Frequency counter sketch, for measuring frequencies low enough to execute an interrupt for each cycle
// Connect the frequency source to the INT0 pin (digital pin 2 on an Arduino Uno)
volatile unsigned long firstPulseTime;
volatile unsigned long lastPulseTime;
volatile unsigned long numPulses;
void isr() {
unsigned long now = micros();
if (numPulses == 1)
{
firstPulseTime = now;
}
else
{
lastPulseTime = now;
}
++numPulses;
}
void setup() {
Serial.begin(19200); // this is here so that we can print the result
pinMode(3, OUTPUT); // put a PWM signal on pin 3, then we can connect pin 3 to pin 2 to test the counter
analogWrite(3, 128);
}
// Measure the frequency over the specified sample time in milliseconds, returning the frequency in Hz
float readFrequency (unsigned int sampleTime)
{
numPulses = 0; // prime the system to start a new reading
attachInterrupt(0, isr, RISING); // enable the interrupt
delay(sampleTime);
detachInterrupt(0);
return (numPulses < 3) ? 0 : (1000000.0 * (float)(numPulses - 2))/(float)(lastPulseTime - firstPulseTime);
}
void loop()
{
float freq = readFrequency(1000);
Serial.println(freq);
delay(1000);
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.