I've got a simple GPIO setup with my raspberry PI to output three signals when a button is pushed. However, I need to add a 33 millisecond delay between the second and third outputs, and one second after the third in order to allow the input device to register the current. I tried to use time.sleep, but this causes all three outputs to execute AFTER the sleep regardless of its position in the code.
import RPi.GPIO as GPIO import time import os import sys # Set the mode GPIO.setmode(GPIO.BCM) # Setup the button GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Test print print('Online') # Loop while True: input_state = GPIO.input(18) if input_state == False: # Set up the first output GPIO.setup(27, GPIO.OUT) # Output GPIO.output(27, 1) # Set up the second output GPIO.setup(22, GPIO.OUT) # Output GPIO.output(22, 1) # Wait time.sleep(0.033) # Set up the third output GPIO.setup(17, GPIO.OUT) # Output GPIO.output(17, 1) # Wait for a second time.sleep(1) # Cleanup & Restart GPIO.cleanup() os.execl(sys.executable, *([sts.executable]+sys.argv))
It is required for me to reset the GPIO and restart the script with each button press as the receiving device will only register the current once even if it is turned off with
If I press the button, it will wait for 1.033 seconds before executing even the first
GPIO.output. I am trying to figure out how to fix this. Any suggestions?
I tried adding each output into its own function, and even created separate modules which I imported, but the results were all the same.