【问题标题】:Raspberry pi button delay time to a relayRaspberry pi 按钮延迟到继电器的时间
【发布时间】:2017-06-30 04:06:22
【问题描述】:

我是 Raspberry pi 和 python 的新手,在处理一些代码时遇到了一些麻烦 我希望按下一个按钮并让与该按钮对应的 gpio 引脚触发继电器打开给定的时间然后关闭。我让它与下面的代码一起工作,但是通过使用'time.sleep(my-variable)',它在这段时间内保持了树莓派,我无法做任何其他事情。 我所追求的是能够按下一个按钮并让继电器动作说 10 秒,在这 10 秒内能够按下另一个按钮来触发另一个继电器并做同样的事情而不会捆绑 pi

我下面的代码首先检查 input_state_LHS 是否等于 false,然后清除 LCD 显示屏,在一行上将文本写入 LCD,然后在下一行写入我的变量(LHS_feedtime)的值,然后用下一行 time.sleep 的时间,这是我希望摆脱的一点,但我无法弄清楚执行它的代码。

if input_state_LHS == False:
        ## calls the LCD_Clear function which has to be in the same folder as this file
        mylcd.lcd_clear()
        mylcd.lcd_display_string("LHS Feedtime",1,2)
        mylcd.lcd_display_string(str(round(LHS_feedtime, 2)) + " sec" , 2,5)
        GPIO.output(27, GPIO.input(12) )
        time.sleep(LHS_feedtime)
        mylcd.lcd_clear()
        mylcd.lcd_display_string("Flatson Feeding", 1)
        mylcd.lcd_display_string("Systems", 2,4)
        GPIO.output(27, GPIO.input(12) )
        menuitem = 0

感谢您的帮助

【问题讨论】:

    标签: python if-statement raspberry-pi


    【解决方案1】:

    您需要的功能在 Python 标准库类 threading.Timer 中。当您启动计时器时,它会启动另一个线程,该线程由时间延迟和对您指定的函数的调用组成。与 time.sleep() 会在此时停止您的主线程相比,使用 Timer 您的主线程将继续运行。

    这大概是你想要的:

    from threading import Timer
    
    def turn_off_lcd():
            mylcd.lcd_clear()
            mylcd.lcd_display_string("Flatson Feeding", 1)
            mylcd.lcd_display_string("Systems", 2,4)
            GPIO.output(27, GPIO.input(12) )
    
    if input_state_LHS == False:
            ## calls the LCD_Clear function which has to be in the same folder as this file
            mylcd.lcd_clear()
            mylcd.lcd_display_string("LHS Feedtime",1,2)
            mylcd.lcd_display_string(str(round(LHS_feedtime, 2)) + " sec" , 2,5)
            GPIO.output(27, GPIO.input(12) )
            t = Timer(LHS_feedtime, turn_off_led)
            t.start()
            menuitem = 0
    

    【讨论】:

      【解决方案2】:

      给你,这将不断循环代码,直到 10 秒过去。但它会不断打印“10 seconds havent passed”(你可以删除这一行。)你会注意到,这段代码不使用 time.sleep(),因此不会阻止脚本。

      import time
      
      #Get the initial system time
      timebuttonpressed = time.strftime("%H%M%S")
      elapsedtime = time.strftime("%H%M%S")
      
      while True:
          elapsedtime = time.strftime("%H%M%S")
          if input_state_LHS == False:
              #Get the new system time, but only set timesample2
              timebuttonpressed = time.strftime("%H%M%S")
      
              ## calls the LCD_Clear function which has to be in the same folder as this file
              mylcd.lcd_clear()
              mylcd.lcd_display_string("LHS Feedtime",1,2)
              mylcd.lcd_display_string(str(round(LHS_feedtime, 2)) + " sec" , 2,5)
              GPIO.output(27, GPIO.input(12) )
      
          #Check if 10 seconds have passed
          if((int(elapsedtime) - int(timebuttonpressed)) == 10):
              timebuttonpressed = time.strftime("%H%M%S")
              mylcd.lcd_clear()
              mylcd.lcd_display_string("Flatson Feeding", 1)
              mylcd.lcd_display_string("Systems", 2,4)
              GPIO.output(27, GPIO.input(12) )
              menuitem = 0
      
          print("10 seconds havent passed...")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多