Hi there, I'm brand new to Arduino (and programming in general) so please bare with me.
I'm trying to create a wireless device that will alarm when it disconnects (ie, out of range). I've got a transmitter toggling a high and low signal every 2000 ms. My idea is to have my receiver start a timer when the signal flips, and if it is still in the same sate after 2100 ms, sound an alarm.
the sketch seems to not work, then eventually the alarm get stuck on. Been banging my head for the last couple of days trying different things, so any help would be much appreciated!
I'm trying to create a wireless device that will alarm when it disconnects (ie, out of range). I've got a transmitter toggling a high and low signal every 2000 ms. My idea is to have my receiver start a timer when the signal flips, and if it is still in the same sate after 2100 ms, sound an alarm.
loop() {
What time is it now? Millis().
What state is the input in now? digitalread().
What state was the input it last time I read it? That's stored in a variable.
When did the state last flip? That's stored in a variable, too.
If the input has changed state since the last time I read it {
put the new state in the variable
put the current millis in the variable.
}
else {
if it's been more than (say) 2100 millis since last state change {
sound an alarm
}
else {
meh, do nothing
}
}
}
I would consider using a faster burst rate and then looking for the burst for a any one out of 3 or so where a missed burst or two does not trigger the alarm. RF is subject to all sorts of outside interference (other transmitters, phase of the moon, something blocking the signal etc.). Give it multiple chances to reset the counter (start counting up with a count time of the expected burst timing - if your counter exceeds the limit you specify, then you are indeed out of range, when you receive a burst, you reset the counter). Basing the alarm on a single missed reception will probably end up driving you nuts.