Page 1 of 1

pin-init in main; no knows in subroutines?

Posted: Wed Nov 11, 2020 4:56 pm
by BL007
Hello,


I've in the main progamm a pin initialized as output and it works. Then I did make subroutines and put this in another files for a better overview. The file I integrated wirth import. The Call of the subroutines takes place after the pin initialization.

But MicroPython shows me an error than the subroutines don't knows the pin definition. I did try continue: In the subroutines file nothing imports and nothing definitions are known.

How can I pass on the imports and definitions?

Re: pin-init in main; no knows in subroutines?

Posted: Wed Nov 11, 2020 11:12 pm
by jimmo
BL007 wrote:
Wed Nov 11, 2020 4:56 pm
How can I pass on the imports and definitions?
This should work the same way it does in regular Python. The best way to solve this is to pass the pin as an argument when you call the subroutines in the other file.

main.py:

Code: Select all

import driver

p = machine.Pin(1, mode=machine.Pin.OUT)

driver.do_stuff(p)
driver.py:

Code: Select all

def do_stuff(p):
  p.value(1)