【问题标题】:Counting Seconds When Push Button Pressed For 5 Seconds按下按钮 5 秒时计数秒数
【发布时间】:2021-12-15 01:46:16
【问题描述】:

所以我想使用 Python 在 Raspberry Pi 中创建一个系统,以通过电报通知和 LED 来判断开关按钮是否已“打开”5 秒,但我坚持让程序在开关按钮时计数 5 秒一直在。到目前为止,我的 Python 代码是:

import RPi.GPIO as io
from telethon import TelegramClient

api_id = ******
api_hash = "*********************"

client = TelegramClient('anon', api_id, api_hash)

io.setmode(io.BCM)
io.setwarnings(False)
io.setup(4, io.IN, pull_up_down=io.PUD_UP)
io.setup(24, io.OUT)

while True:
    if io.input(4) == 1:
        if time.time(5):
            io.output(24, 1)
            me = client.get_me()
            client.send_message('test', 'The switch button has been ON for 5 seconds')
        else:
            pass
    else:
        pass

如何修改程序,如果开关打开5秒,它会打开LED并发送通知?

【问题讨论】:

    标签: python raspberry-pi telethon


    【解决方案1】:

    time.time() 只返回自纪元时间以来的秒数。所以要知道是否已经过了 5 秒,你需要保存按下按钮的时间,然后继续检查它是否已经过了 5 秒。

    这可以通过轮询或事件检测来完成。下面是如何通过事件检测来做到这一点,这是更可靠的方法:

    button_pressed_time = None
    
    def button_pressed(channel):
        global button_pressed_time 
        button_pressed_time = time.time()
    
    def button_released(channel):
        global button_pressed_time 
        button_pressed_time = None
        #io.output(24, 1) --> you'll need this if you want to turn off the LED when the button is released
    
    io.add_event_detect(4, io.RISING, callback=button_pressed)
    io.add_event_detect(4, io.FALLING, callback=button_released)
    
    while True:
        if button_pressed_time != None and time.time() - button_pressed_time >= 5:
            io.output(24, 1)
            me = client.get_me()
            client.send_message('test', 'The switch button has been ON for 5 seconds')
            button_pressed_time = None
    
    

    有关轮询和事件检测的更多信息:https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/

    【讨论】:

      【解决方案2】:

      time.time() 不接受任何参数。调用时返回

      the time in seconds since the epoch as a floating point number

      您需要从开关打开的那一刻起计算 5 秒的经过时间。 您需要添加三个变量来实现此目的:

      • 定时器:用于计算经过的时间,每次打开开关都需要重新设置。
      • 开关的状态:它只有助于重置一次计时器(当开关打开时)。
      • 消息的状态(无论是否发送):这将有助于只发送一次消息。

      最终的代码应该是这样的(重点是逻辑,我对树莓派上的python不太熟悉):

      import RPi.GPIO as io
      from telethon import TelegramClient
      import time
      
      api_id = ******
      api_hash = "*********************"
      
      client = TelegramClient('anon', api_id, api_hash)
      
      io.setmode(io.BCM)
      io.setwarnings(False)
      io.setup(4, io.IN, pull_up_down=io.PUD_UP)
      io.setup(24, io.OUT)
      
      led_on_start = 0 #initialise timer
      led_on = False #initialise led state to "off"
      message_sent = False #initialise message to "not sent"
      while True:
          if io.input(4) == 1:
              if not led_on: #reset the timer when the switch is turned ON
                  led_on_start = time.time() 
                  led_on = True    
              if not message_sent and time.time() - led_on_start > 5:
                  io.output(24, 1)
                  me = client.get_me()
                  client.send_message('test', 'The switch button has been ON for 5 seconds')
                  message_sent = True
          else:
              if led_on: #reset your variables when the switch is turned OFF
                  io.output(24, 0)
                  message_sent = False
                  led_on = False
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-10
        • 1970-01-01
        • 2018-11-05
        • 2018-06-05
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 2018-06-07
        相关资源
        最近更新 更多