pin-init in main; no knows in subroutines?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
BL007
Posts: 16
Joined: Wed Sep 02, 2020 6:38 am

pin-init in main; no knows in subroutines?

Post by BL007 » Wed Nov 11, 2020 4:56 pm

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?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

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

Post by jimmo » Wed Nov 11, 2020 11:12 pm

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)

Post Reply