1

I have managed to construct some python code on a Raspberry PI to control a garage door. However I don't know how to add a timed delay so that the control is limited to the correct movement time.

Any help would be appreciated.

My working code is below.

    import RPi.GPIO as GPIO          
    from time import sleep

    in1 = 24
    in2 = 23
    en = 25
    temp1=1

   GPIO.setmode(GPIO.BCM)
   GPIO.setup(in1,GPIO.OUT)
   GPIO.setup(in2,GPIO.OUT)
   GPIO.setup(en,GPIO.OUT)
   GPIO.output(in1,GPIO.LOW)
   GPIO.output(in2,GPIO.LOW)
   p=GPIO.PWM(en,1000)
   p.start(25)
   print("\n")
   print("The default speed & direction of motor is LOW & Forward.....")
   print("r-run s-stop f-forward b-backward l-low m-medium h-high e-exit")
   print("\n")    

   while(1):
   x = input()
   if x=='r':
   print("run")
   if(temp1==1):
   GPIO.output(in1,GPIO.HIGH)
   GPIO.output(in2,GPIO.LOW)
   print("forward")
   x='z'
   else:
   GPIO.output(in1,GPIO.LOW)
   GPIO.output(in2,GPIO.HIGH)
   print("backward")
   x='z'


   elif x=='s':
   print("stop")
   GPIO.output(in1,GPIO.LOW)
   GPIO.output(in2,GPIO.LOW)
   x='z'

   elif x=='f':
   print("forward")
   GPIO.output(in1,GPIO.HIGH)
   GPIO.output(in2,GPIO.LOW)
   temp1=1
   x='z'

   elif x=='b':
   print("backward")
   GPIO.output(in1,GPIO.LOW)
   GPIO.output(in2,GPIO.HIGH)
   temp1=0
   x='z'

   elif x=='l':
   print("low")
   p.ChangeDutyCycle(25)
   x='z'

   elif x=='m':
   print("medium")
   p.ChangeDutyCycle(50)
   x='z'

   elif x=='h':
   print("high")
   p.ChangeDutyCycle(75)
   x='z'


    elif x=='e':
    GPIO.cleanup()
    break

    else:
    print("<<<  wrong data  >>>")
    print("please enter the defined data to continue.....")
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216

2 Answers2

1

Can't help with PI code but would like to add a little warning. If you rely on time to switch off the motor then any cars, kids, bikes etc would be crushed by the closing door. Can you interface a current sensor to the Pie so that it stops when the motor current exceeds a certain amount? Have a look at These

Mike.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
PommieMike
  • 11
  • 5
0

This question and answer How can I make a time delay in Python? shows how to add a timed delay in your code.

import time
# delay for 10 seconds
time.sleep(10)

However this is not the best solution. Better to use the solutions here : https://raspberrypi.stackexchange.com/questions/69640/add-delay-between-two-gpio-output and I'd also suggest you use pins to capture information such as open or closed from the door perhaps using sensors switches (of which there are various types).

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216