0

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 GPIO.output(x, 0)

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.

  • Did you try using another GPIO library, such as pypi.python.org/pypi/wiringpi2 ? See raspi.tv/… – Mark Jul 11 '17 at 5:52
  • Could you use pseudo code to clarify what you are trying to do? At the moment you do not change any GPIO level. They are always set high (they will stay set at 1 until you set them to 0), the cleanup won't really do much to help. – joan Jul 11 '17 at 8:00
  • @Zsophia Norris I see in another comment that you figured this out. Could you please share what you found here in case it might help others that come across this issue? – Cameron Tacklind Aug 8 at 22:20
0

Using Gpiozero, you can achieve your desired effect with the following code:

from gpiozero import LED, Button
from time import sleep
from signal import pause

# Setup the output pins
output1 = LED(27)
output2 = LED(22)
output3 = LED(17)

# Setup the button
button = Button(18)

# Wait for the button to be pressed
button.wait_for_press()

# Do the stuff you want done after the button is pressed here
output1.on()
output2.on()

# Wait for 0.033 sec
sleep(0.033)

output3.on()

# Wait for 1 sec
sleep(1)

# Assuming you want the signals to return to 0 V at the end
output1.off()
output2.off()
output3.off()

pause()

I also recommend checking out the recipes section of the Gpiozero documentation. You will find many options that are easily adaptable to your situation.

  • While I did figure out what was wrong, this answer provides a good alternative. – Zsophia Norris Jul 16 '17 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.