Jan-31-2023, 10:16 AM
Hi, i am not sure how to express it right but i wonder if there is a way to make a function to return a boolean value based of the Gpio readings
some times i have different functions that need to know if the same pins are "True" before do something.. so i have to type to all of them the same thing again and again...
how could make a def to check this one and return a value and call the def every time i want to check the state of them ?
Here is a sample code :
some times i have different functions that need to know if the same pins are "True" before do something.. so i have to type to all of them the same thing again and again...
Quote:while GPIO.input(Switch_3) == True and GPIO.input(Switch_4) == True :
how could make a def to check this one and return a value and call the def every time i want to check the state of them ?
Here is a sample code :
import RPi.GPIO as GPIO import time GPIO.cleanup() # Cleans previous Gpio setup if program crashes without clean it first Switch_1 = 21 # Switch that read "1" - True when is the normal / working Switch_2 = 20 # Switch that read "1" - True when is the normal / working Switch_3 = 23 # Switch that read "1" - True when is the normal / working Switch_4 = 24 # Switch that read "1" - True when is the normal / working # SETUP GPIO METHOD # GPIO.setmode(GPIO.BCM) GPIO.setup(Switch_1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(Switch_2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(Switch_3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(Switch_4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) def stopAll(): while GPIO.input(Switch_3) == False or GPIO.input(Switch_4) == False: print("Error") if GPIO.input(Switch_3) == False: print("Switch No3 is Open") if GPIO.input(Switch_4) == False: print("Switch No4 is Open") def main(): while True: print("Startin Loop") while GPIO.input(Switch_3) == True and GPIO.input(Switch_4) == True : print("Do something") if GPIO.input(Switch_3) == False or GPIO.input(Switch_4) == False : stopAll() print("Starting....") time.sleep(2.0) if __name__ == "__main__": main()