Coding query ... if __name__ == '__main__': and pin hold #10396
-
Having discovered:
I don't understand what happens in this code: from machine import Pin
import utime
pin32 = Pin(32, Pin.OUT, hold=False)
pin32.off()
def rotate_valve():
pin32.on()
print (f'pin32 is on')
utime.sleep(0.1)
def main():
global pin32
while True:
pin32.off() # make sure it is off every loop
print (f'pin32 is off')
utime.sleep(0.1)
utime.sleep(1)
rotate_valve()
utime.sleep(1)
# hold the GPIO state during lightsleep()
pin32 = Pin(32, Pin.OUT, hold=True) # motor drive (line in question)
if __name__ == '__main__':
main() If I don't put A keyword should provide the relief I need. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
You misunderstand how function-local variables get created in python. Any assignment to a name (e.g. |
Beta Was this translation helpful? Give feedback.
pin32 = Pin(32, Pin.OUT, hold=False)
does not merely change a parameter ofpin32
. For python, this creates a new instance of aPin
object with the new parameters, even when this is only a reference to the already-existing Pin object (due to the most-likely singleton characteristic of Pin objects).After that, the old (maybe global) pin32 instance is…