Hello everyone.
i am doing a project that calculating the frequency from a speed sensor, the sensor is generating 10V AC, i’ve converted it to 4-5V DC half-wave.
Now i would like to use the Atmega168 to find out the frequency, first i need to convert the analog to digital, then calculate it.
i tried to write a script, but there is something wrong, could anyone help me? i vey appreciate it.
const int sensorPin = 0;
int analogPin = 0;
int sensorValue = 0;
void setup() {
Serial.begin(9600);
Serial.println("Start measuring");
}
void loop()
{
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);
// start with input low
if (digitalRead(frqPin == 0) {
// wait until pin goes high
while (digitalRead(frqPin) == 0) {}
// get the time when input goes high
timeStart = micros();
// wait until input goes low again
while (digitalRead(frqPin) == 1) {}
// wait until input goes high again
while (digitalRead(frqPin) == 0) {}
// take the actual time in microseconds
timeStop = micros();
// the timedifference is the the time between two rising
// edges of the input pin (= 1 period)
timeStart = timeStop-timeStart;
// normally the result is > 0, otherwise the
// function micros() had an overflow (every 70 min)
// the discard this measuring (or correct the value)
if (timeStart > 0) {
// calculate frequency
// 1000000 microseconds/second, f = 1/t
double f = 1000000.0/timeStart;
// report the measured frequency
SerialPrintln(f);
}
}
}
}
In function 'void loop()':
error: 'frqPin' was not declared in this scop
Where did you declare the variable frqPin? Where did you assign it a value?
what do you mean by the re-invent the pulseIn function
The Arduino IDE provides a function called pulseIn that returns the length of time required for the specified pin to go HIGH, go LOW, or change state. That is what you are trying to write code to do.
Where did you declare the variable frqPin? Where did you assign it a value?
i should change it to "while (digitalRead(frqPin) > 0) )" ?
i am new in programming actually. So some standards of writing code that i don't know, if i would like to find the frequency from the analog input of which is connecting to the speed sensor, the frqPin is needed to be defined?
like define frqPin 0 ?