Python Forum
GPIO output timing help needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GPIO output timing help needed
#1
Greetings to the Group

I am a beginner to python so be nice to me.
I am trying to control a GPIO output from a HIGH input and have the output stay HIGH for a specific time. I have written the script and it works sort of.
The problem I'm seeing is, if the input goes LOW and then back to HIGH and back to LOW before the "sleep time" had finished, it does not reset the Output time.
I am using the "sleep time" but I don't think this is the correct way to control the Output timing. As I said I'm very new to programming and if anyone can help me here it would be greatly appreciated. I know the answer is simple but I'm not seeing it.
I have attached the code I have written, below.
import RPi.GPIO as GPIO
from time import sleep    #import time library

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)     # set i/p sig to GPIO pin 4
GPIO.setwarnings(False)
GPIO.setup(26,GPIO.OUT,initial=GPIO.LOW)  #set o/p sig to GPIO pin26, initiallize in low state
GPIO.setup(4,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)  #set internal pulldown resistor
try:
    while True:
        if  (GPIO.input(4)==1):
                GPIO.output(26,1)
                sleep(10)
        else:
            GPIO.output(26,0)
            sleep(.1)
            
finally:
    GPIO.output(26,False)
    GPIO.cleanup
Reply


Messages In This Thread
GPIO output timing - by skid - Jan-21-2018, 02:50 PM
RE: GPIO output timing help needed - by j.crater - Jan-22-2018, 07:09 PM
RE: GPIO output timing help needed - by Larz60+ - Jan-22-2018, 07:25 PM
RE: GPIO output timing help needed - by DeaD_EyE - Jan-23-2018, 09:36 AM
RE: GPIO output timing help needed - by skid - Jan-23-2018, 04:12 PM
GPIO output timing help needed - by skid - Jan-22-2018, 06:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  function return boolean based on GPIO pin reading caslor 2 1,170 Feb-04-2023, 12:30 PM
Last Post: caslor
  class Update input (Gpio pin raspberry pi) caslor 2 788 Jan-30-2023, 08:05 PM
Last Post: caslor
  Webhook, post_data, GPIO partial changes DigitalID 2 983 Nov-10-2022, 09:50 PM
Last Post: deanhystad
Question Timing actions with Python dangermaus33 0 1,003 Apr-19-2022, 10:08 PM
Last Post: dangermaus33
  Inconsistent counting / timing with threading rantwhy 1 1,758 Nov-24-2021, 04:04 AM
Last Post: deanhystad
  Synchronization/Timing Problem quest 5 2,973 Mar-31-2021, 10:26 PM
Last Post: quest
  Timing of a while loop stylingpat 4 6,795 Mar-31-2021, 10:48 AM
Last Post: stylingpat
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 3,916 Feb-19-2021, 06:29 AM
Last Post: LouF
  Picture changing triggered by GPIO q_nerk 2 2,564 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  GPIO high if network IP has good ping duckredbeard 3 2,329 Oct-12-2020, 10:41 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020