Home > other >  Counting Seconds When Push Button Pressed For 5 Seconds
Counting Seconds When Push Button Pressed For 5 Seconds

Time:12-15

So i want to create a system in Raspberry Pi using Python to tell through telegram notification and LED if the switch button has been "ON" for 5 seconds, but I stuck in make the program for counting 5 second if the switch button has been on. My Python code so far is:

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

How to modify the program to if the switch has been ON for 5 second, it will turn on the LED and send a notification?

CodePudding user response:

time.time() simply returns the number of seconds since epoch time. So to know whether or not 5 seconds has passed, you need to save the time when the button is pressed, then keep checking against it to see if 5 seconds has passed.

This can be done either by polling or event detection. Here is how to do it by event detection, which is the more reliable method:

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

More info on polling and event detection: https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/

CodePudding user response:

time.time() does not accept any arguments. When called, it returns

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

You need to count 5 seconds of elapsed time from the moment the switch is turned ON. You need to add three variables to achieve this:

  • a timer: it will be used to count the elapsed time and it needs to be reset every time the switch is turned ON.
  • the state of the switch: it will help with resetting the timer only once (when the switch is turned ON).
  • the state of the message (whether it is sent or not): it will help with sending the message only once.

The final code would look like this (focused on logic, I am not familiar with python on Raspberry Pi):

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
  • Related