PWM problem

I am totally new to the Arduino world (and programming) buying my first UNO a few weeks back with the thought I could easily repair a Fuel flow gage when the sending transducer died and learn about Arduino along the way.
I had found a better flow transducer but its output (i.e. pulses/Gal) is not the same as the OEM flow transducer. (And they stopped making the OEM due to quality issues.)
My thought was to simply read the frequency of the transducer, apply a correction factor and then out put the new frequency to the Fuel Flow Display. The frequency needed is quite low 1~500Hz.
I have tried two “period” measuring libraries both of which can read the pulses from the NEW flow transducer. The first one was from Martin Nawrath, “FreqPeriod” http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/ and the second was from PJRC, “FreqMeasure” https://www.pjrc.com/teensy/td_libs_FreqMeasure.html. My problem comes in trying to send the corrected frequency from the UNO. I thought I had the answer with a library called PWM http://forum.arduino.cc/index.php/topic,117425.0.html . This function can set the PWM frequency to any value from 1Hz to 2MHz. It also worked great “independently”! When I try to run the two functions together the PWM does not work.
I think the problem is I need to be able to STOP the “FreqMeasure” function before running the PWM function. I thought the “FreqMeasure.end” was supposed to do that, but it does not and the “FreqPeriod” does not have such a command. When I comment out either function the remaining works as expected. I just cut & pasted the example sketches into the following: Can someone please show me where I’m going wrong. Thanks!
#include <FreqMeasure.h>
#include <PWM.h>

int32_t LCDout;
float frequency;
int LCD=9;
float sum;
//=====================
void setup() {
InitTimersSafe();
FreqMeasure.begin();
Serial.begin(115200);
}
//==========================
void loop() {
sum = FreqMeasure.read();
frequency = FreqMeasure.countToFrequency(sum);
Serial.print("\t input: ");
Serial.print(frequency);
Int32_t LCDout = 2.5635 * frequency +9.8263;
Serial.print("\t output ");
Serial.println(LCDout);
FreqMeasure.end;
//set the PWM frequency to the new
SetPinFrequencySafe(LCD,LCDout);
delay(50);
sendFREQ();
}
//============================
void sendFREQ(){
//set dutycycle for PWM and send frequency
pwmWrite(LCD,125);
delay(50);
return;
}

If you want to send out pulses with a frequency varying between 1 and 500 Hz you don't need any library. While it is very crude this will work at 500 Hz

void loop() {
   digitalWrite(outPin, HIGH);
   delay(1);
   digitalWrite(outPin, LOW);
   delay(1);
}

Of course it would be better not to use delay() as it prevents the Arduino from doing anything else. Look at how millis() is used for timing in the demo [several things at a time](http://Several things at the same time - Arduino Forum)

Note that PWM is not intended for variable frequency - only variable duty factor.

...R

500hz without delay

void loop() 
{
bool state=(millis()&1);
digitalWrite(outPin, state);
}

Jwhalin:
I think the problem is I need to be able to STOP the “FreqMeasure” function before running the PWM function. I thought the “FreqMeasure.end” was supposed to do that, but it does not and the “FreqPeriod” does not have such a command. When I comment out either function the remaining works as expected. I just cut & pasted the example sketches into the following: Can someone please show me where I’m going wrong. Thanks!

Both libraries probably use timer1. I think your strategy for using FreqMeasure.end could work, but you don't call FreqMeasure.begin in loop(), only in setup(). So if it works it only does so the first time.

As suggested, it might be a lot easier to use another method for generating a square wave. How accurate does it have to be?

    FreqMeasure.end;