The relay should be on when there is a 1 Hz frequency detected on an Arduino pin, but off when the frequency is say 20 % higher or say 20% lower ?
You have to work out a way of measuring frequency by counting pulses in a unit time and, on that basis, switch a pin.
6v6gt:
The relay should be on when there is a 1 Hz frequency detected on an Arduino pin, but off when the frequency is say 20 % higher or say 20% lower ?
Yes, please be more specific so we don't waste time.
btnVal = digitalRead(btnPin);
if (btnVal == HIGH) { // assumes btn is LOW when pressed
lastBtnPressMillis = millis(); // btn not pressed so reset clock
}
if (millis() - lastBtnPressMillis > = interval) {
// button has been pressed for longer than interval
}
Think of the button as you input pulse. You may need to swap LOW for HIGH.
I think if you set the interval a bit longer than the usual period between pulses (say 1050 for a 1000msec period) the clock will always reset before the interval expires as long as the pulses keep coming.
You maintain a time stamp variable. Continually check the input. Any time the input is active you set the variable to millis() time. Also continually compare the current millis() time with that variable. If the difference is more than 2000 ms, you switch the relay.
It's really that simple. Two if statements inside loop().
Unless you really do have microsecond long pulses, as mentioned above. Then you have to use interrupts.
I suppose the next question is what level of latency is acceptable ? If it is sufficiently high, then the duty cycle is irrelevant and need not be constant. If it is low, then the signal has to be correspondingly 'well behaved' for an accurate measurement.
it really depends on what else you are doing in your code.
if you are only scanning for an alarm or some such, and then only trip when it is out of range, your scan times will be orders of magnitude faster than your pulse.
however, if you do a lot of things, there is a chance you can miss the event.
if your scan rate is 10 times faster than the shortest event, you are in great shape
if your scan rate is less than 4 times faster, you are close to missing it, any error and you might.
I think I would look at both a software and hardware solution.
you can charge a cap quickly and have it decay slowly.
watch the voltage with an ADC pin
did we get a clear statement about what should happen if the pulse turns into a constant high signal ?
the absence of a signal would drop out the relay.
the pulse would activate the relay
but what result should happen if there is a continuous high signal on the line ?
and for the purpose of the question, it does not matter, but just what generates the pulse ?