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?

asked Mar 30, 2015 at 22:32
1
  • what do you mean by "negative impact"? Are you talking about the speed the application runs? physical wearing of the hardware? Commented Mar 30, 2015 at 22:47

2 Answers 2

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

answered Mar 30, 2015 at 23:05
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.

answered Apr 1, 2015 at 12:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.