Hello,
Im trying to measure frequency from a signal generator and there is a problem that everyone so far has been stumped by that i ask... The problem is it does track the frequency but it cannot produce a steady value eg. at 50Hz it will measure 50Hz the jump to 52.3 and 48.7 which is not ideal for the program im trying to run any thoughts would be greatly appreciated!
Alot of stuff coded out as i will need later on if problem is sorted
Thanks
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13 ; // select the pin for the LED
int sensorValue = 0; // variable to store the sensor value
int relayPin = 8;
int maxValue = 0;
const int numReadings = 10;
bool maxDetected = false;
int startTime = 0;
int endTime = 0;
int totalTime = 0;
double freq = 0;
int average = 0;
bool Passed500Yet = false;
bool ValueOutput = false;
int OutputCount = 0;
//#include <FreqMeasure.h>
//double sum=0;
//int count=0;
//float frequency;
void setup()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT:
pinMode(relayPin, OUTPUT); // declare the ledPin as an OUTPUT:
Serial.begin(9600);
Serial.print("\n");
//Serial.begin(57600);
//FreqMeasure.begin();
}
void loop()
{
sensorValue = analogRead(sensorPin); // read the sensor value
//digitalWrite(ledPin, HIGH); // turn the ledPin on
//delay(sensorValue); // wait for ms
//digitalWrite(ledPin, LOW); // turn the ledPin off
// delay(sensorValue); // wait for ms
//Serial.print("Sensor value = ");
//Serial.print(sensorValue);
//Serial.print("\n");
if (sensorValue > 500)
{
Passed500Yet = true;
}
// else
// {
// Passed500Yet = false;
// }
/* if ((sensorValue > 510)&& (Passed500Yet == true)&&(ValueOutput == false))
{
// Passed510Yet = true;
if (startTime == 0)
{
Serial.print("SensorValue = ");
Serial.print(freq);
Serial.print(" Hz \n");
startTime = millis();
}
else
{
endTime = millis();
totalTime = endTime - startTime;
//Serial.print("CycleTime = ");
//Serial.print(totalTime);
//Serial.print(" ms \n");
startTime = 0;
freq = (1.0/totalTime)*1000;
Serial.print("Freq = ");
Serial.print(freq);
Serial.print(" Hz \n");
ValueOutput = true;
}
Serial.print("SensorValue = ");
Serial.print(sensorValue);
Serial.print(" StartTime = ");
Serial.print(startTime);
Serial.print(" EndTime = ");
Serial.print(endTime);
Serial.print(" TotalTime = ");
Serial.print(totalTime);
Serial.print(" /n ");
}
if ((sensorValue < 500) && (Passed500Yet == true))
{
Passed500Yet = false;
ValueOutput = false;
}
*/
if (sensorValue > 500)
{
if (sensorValue > maxValue)
{
maxValue = sensorValue;
}
if ((sensorValue < maxValue) && (maxDetected == false))
{
//Serial.print("MAX \n");
maxDetected = true;
maxValue = 0;
if (startTime == 0)
{
startTime = millis();
}
else
{
endTime = millis();
totalTime = endTime - startTime;
//Serial.print("CycleTime = ");
//Serial.print(totalTime);
//Serial.print(" ms \n");
startTime = 0;
freq = (1.00/totalTime)*1000;
OutputCount = OutputCount + 1;
if (OutputCount % 20 == 0)
{
Serial.print("Freq = ");
Serial.print(freq);
Serial.print(" Hz \n");
delay(1);
}
}
}
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
if (sensorValue < 300)
{
maxDetected = false;
}
// if (FreqMeasure.available()) {
// average several reading together
//sum = sum + FreqMeasure.read();
//count = count + 1;
// if (count > 30) {
// frequency = FreqMeasure.countToFrequency(sum / count);
// Serial.println(frequency);
// sum = 0;
// count = 0;
}