如何在Python中为硬件con添加定时延迟

2024-09-24 06:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经设法在树莓PI上构造了一些python代码来控制车库门。但是,我不知道如何添加一个定时延迟,以便控制被限制在正确的移动时间。你知道吗

任何帮助都将不胜感激。你知道吗

我的工作代码如下。你知道吗

    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.....")

Tags: outputgpiosetupoutenlowforwardprint
2条回答

这个问答How can I make a time delay in Python?展示了如何在代码中添加定时延迟。你知道吗

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

然而,这并不是最好的解决方案。最好使用这里的解决方案:https://raspberrypi.stackexchange.com/questions/69640/add-delay-between-two-gpio-output我还建议您使用pin来捕获信息,例如门的打开或关闭,或者使用传感器开关(其中有各种类型)。你知道吗

不能帮助PI代码,但想添加一个小警告。如果你依靠时间来关掉马达,那么任何汽车、小孩、自行车等都会被关上的门压碎。你能把电流传感器连接到饼图上吗?这样当电机电流超过一定值时它就会停止工作?看看These

迈克。你知道吗

相关问题 更多 >