I am running a relay that turns on while button is being held down and then turns off when you let the button go (not a button de-bounce issue). I can write this two ways: one is simple but potentially more load on Arduino vs. other more elegant but complicated. Is there a strong recommendation for one over another
loop
if button is low then set relay_pin low; otherwise, set relay_pin high.
//this approach keeps checking button still low and keeps setting relay_pin low continuously. Not sure how bad is it to keep setting pin low if it's already in that state. I am guessing it's ok?//
Loop
if button low and pressed_state==false then set relay_pin low, pressed_state=true
if button high and pressed_state==true then set relay_pin high, pressed_state=false
//here i deal with one more variable but arduino sets pins only when there is a change.
Definitely OK either way. A third way would be to omit the arduino and just hook the button to the gate of a simple mosfet. All it takes is exactly one button, one mosfet and one resistor to get the job done. Throw in one capacitor and one more resistor for a very basic but perfectly adequate debounce.
Is either option more "green" meaning more efficient: uses less power?
Let's say you spent $1 on your Arduino. Either approach uses the full investment; you are not running multiple procedures. Therefore, neither approach has an economic offset/return.
If you are learning, you will preference the better way with experience; right now you are asking a good question, but there is little bias in my mind as long as you understand there are multiple ways to implement the solution.
Some here may say that "cleaner" code is the more "obvious" code; that means that a knowledgeable person would immediately know what the code purpose is without overly pondering over the sketch.
When coding, "best" is often defined as "it works as intended in a consistent manner."
Thank you. Actually, it's fairly complicated system with lots of peripherals and one wireless and one wired remote and lots of functions. So, I was trying to make each code as efficient as possible not to bog down the system. But this helps. thank you.
Then "clarity" and "efficiency" are going to be important. Both can be achieved.
I usually tell project developers to know their projects; that is, you should profile your code in sections and take note of loop() time and SRAM/flash memory resources. As you assemble the parts, resource usage increases but you will have your profile notes to determine the impact of the pieces. If you hit a resource issue, your profile data collected during dev-test will be very valuable.
All the more reason to not do something with software if it can be very easily done with hardware. But I assume we're not seeeing full specifications here and there are reasons why you want to solve this in software. Those (hidden) requirements will impact the choice of an appropriate solution, so all comments above are only marginally relevant.
Hi. Yes, too much to document here - it's a big project where arduino is in the background to help with some functions and with extremely tight time-line. This is the reason we are trying to see if there is some efficiency gained via software but there is really no time to do much else. We need to move on even if arduino-portion were to be completely disabled.
P.S. based on everything I learned, if time was not an issue, i would tear down the system and build it all over again but this is for the next time... thank you.