• Did you try it at all? does it work?

    The idea is:

    • If pulledUp==true, the pin is currently set to input_pullup, so there's no need to change pin state, hence the if (!pulledUp) pinMode. This is the normal state for reading the pulses of light when the LED is not on
    • if pulledUp==false, we have turned it off to save power when the LED is on, but to get a usable reading we have to turn it the pullup on temporarily

    As it happens, because of the order of the if (!pulledUp) pinMode stuff we can just do what we want by setting pulledUp.

  • well, it works but trying to understand how it saves power.
    Just because we set variable pulledUp to true does not mean that we set pinmode to input_pullup
    first line only sets the pinmode to input_pullup if pulledUp is false

    if (!pulledUp) pinMode(D2,"input_pullup");
    

    so I am trying to understand when does it actually set pinmode, if the condition when everything is ok is setting it to false...

About

Avatar for Gordon @Gordon started