Setting pins high or low safely

I'm just a hobbiest programmer, luckily C was the first real, compiled language I learned so I'm taking to this Arduino thing like a fish to water. Having a blast and no problem making things work.

I have a programming question about controlling relays. I have a sketch that is controlling relays according to a timed schedule. I have that working fine. What I am doing is I only switch the state of the pin controlling the relay if it needs to. In other words, I only turn a relay on if it is already off (and vice versa). I'm thinking this may not be necessary and a waste of clock cycles to do the check (though it is trivial).

Should I worry about this? Is there a standard procedure here? Is there an advantage to either way (checking or not checking)?

I hope that makes sense. Thanks, in advance, for any answers.

Don't check. The only case where checking might be useful is if you're going to be doing a gigantic calculation that you have probably already done (eg if you're computing Fibonacci numbers, and you just computed 98 and 99 and now you want to computer 100)

Is there anything else in attached to the signal that could turn a relay on or off other than a command from an Arduino? Normally there wouldn't be and if that's the case you could just invert the bit in the control register that's used to set the pin HIGH or LOW.

"Should I worry about this?"
No.

"Is there a standard procedure here?"
No.

"Is there an advantage to either way (checking or not checking)?"
You could declare some error if the pin was not in a previously commanded state.
That would require storing that state (in an array perhaps) for comparison.

I think a better check might be to monitor the other side of the relay, make sure the output actually changed. Could be a down supply, stuck relay, something shorting an output, etc. How critical is the thing you are swithching?

There's nothing wrong with your idea, but that shouldn't generally be necessary since your software should always know what the state is. Sometimes we save the state in a variable (rather than directly reading the pin) if your program doesn't automatically "remember" the state.

And if something goes wrong, such as if your program totally crashes, it won't know to read the state and it won't know what the state should be anyway. :stuck_out_tongue: Sometimes a [u]watchdog timer[/u] is used to reset the system if something goes haywire. If it's a super-critical application like life-support or something, you need something else monitoring your system, rather than having it monitor itself.

As far as wasting clock cycles... so what??? :wink: Unless you are having issues with not processing fast enough... The CPU is always doing something and unless it's in the "sleep mode" it's always executing instructions as fast as it can, even if it's execuiting "no-ops" (do nothing instructions). On a Windows (or other multitasking machine) it's bad practice to eat-up CPU cycles, but on a non-multitasking system like the Arduino it's OK as long as your program doesn't need to be doing something else.

Thanks guys. I really appreciate it. My suspicion is confirmed, I was over thinking it.

Providing secondary checks is a good idea, one I have considered. I may have to get another Arduino board and some various sensors to babysit the first one. :slight_smile: just kidding.

Jimmy60:
Providing secondary checks is a good idea, one I have considered. I may have to get another Arduino board and some various sensors to babysit the first one. :slight_smile: just kidding.

Quis custodiet ipsos custodes? :wink: