I want to make an output of the arduino "lets say pin2" switch to high when frequency of lets say pin 0 is above 40hz. I've read up on the pulseIn function and came up with the following code, but it doesn't work. Feeding the input with a 5v 50% duty 30-50hz and led just stays dim...
Can anybody elaborate?
const int input = 0;
const int output = 2;
void setup() {
pinMode(input, INPUT);
pinMode(output, OUTPUT);
}
void loop() {
int Speed;
Speed = 500000/pulseIn(input, HIGH);
{
if (Speed > 40);
digitalWrite(output, HIGH);
if (Speed < 39);
digitalWrite(output, LOW);
}
}
Let me try this again, "Speed" is an int, int has a maximum of 32767. Now, which number above is above that maximum, because simply writing something like 32770 / 2 does NOT give you 16385, instead it will give you -32766 / 2 because it rolled over.
So here is your correction, put an L (meaning long, or 32 bit). I was hoping you would spot the error yourself.