8Views1Replies

Author Options:

why write the script this way? Answered

great class rhank you!
could you please expain why you have written the sketch this way? instead of, for instance something (that to my noob mind seems easier) like:
if(digitalRead(BUTTON_PIN) == LOW)
digitalWrite(LED_PIN, HIGH);

it seems like the return function makes a loop that only proceeds if the value of "current" changes, if i undertsand correctly. whats the advantage of this?
is this like a state change detection code? thank you

Discussions

0
None
bekathwia

Best Answer 2 days ago

Yep, you got it, it's state-change detection. For an LED what you propose is acceptable, but for internet messages, you want to be conservative about how much extraneous data you are sending. You don't don't want to constantly be sending the same message, just one message when the state changes. Thanks for the excellent question.