I'm not familiar with IEC 61131-3 but what you're doing is not how python is designed to work.
Code outside of functions in a python module is expected to be run once, and once only, the first time a given interpreter imports the module. This code is intended to setup the module not to do the actual work.
Further, I'm not sure you fully understand how variable scoping works in python.
If you define switch in the main script it is accessable to imported modules by the same name. And you should really only define it once at the start of your program. It's probably better to pass it as an argument to a function though - doing that is certainly more portable.
With the above in mind:
Your module becomes:And you main script becomes:Note: there are a few other possible issues with your posted code, for example switch = Button("BCM27") should be switch = Button(27)
Code outside of functions in a python module is expected to be run once, and once only, the first time a given interpreter imports the module. This code is intended to setup the module not to do the actual work.
Further, I'm not sure you fully understand how variable scoping works in python.
If you define switch in the main script it is accessable to imported modules by the same name. And you should really only define it once at the start of your program. It's probably better to pass it as an argument to a function though - doing that is certainly more portable.
With the above in mind:
Your module becomes:
Code:
def do_stuff(foo): if foo.is_pressed: print("Door is open") return foo.is_pressed:
Code:
from gpiozero import Buttonimport GPIO_Door_And_RelaysNew_Phase = Trueswitch = Button(27)try: while True: print ("GPIO_Door updated") if GPIO_Door_And_Relays.do_stuff(switch) New_Phase = True
Statistics: Posted by thagrol — Sat Jan 06, 2024 4:27 pm