0

I am using an Arduino in an automation project which will be using a state machine to conduct a one-hour scenario. Currently, the Arduino reads each input pin, runs its state update script, then sets each update pin. 99.99% of the time, each output pin will be set to the same state that it had in the last step. Although it is better (in my opinion) to only set the pin when a setting changes, I was wondering if constantly checking and setting pins would have a big negative impact on the hardware?

Would checking a pin's current state (digitalRead or other method) before setting the pin improve anything?

  • what do you mean by "negative impact"? Are you talking about the speed the application runs? physical wearing of the hardware? – BrettAM Mar 30 '15 at 22:47
4

It does not hurt the Arduino to set an output pin to high if it was already set to high. If you are concerned about performance, it is more efficient to just set the output pin compared to reading the pin and then setting it only if it's different

1

Would checking a pin's current state (digitalRead or other method) before setting the pin improve anything?

So instead of:

SetPin(OUTPUTPIN,true);

You want to do:

if(!PinIsSet(OUTPUTPIN)){
 SetPin(OUTPUTPIN,true):
}

To avoid writing to a pin (making it high) when it's already high/enabled?

Well doing so will waste a little amount of your programs' speed (it'll need to check each pin before setting it). But when you set a pin, when it's already high, it simply doesn't even change. And even then it's not required to check it first, Microcontrollers are basically made for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.