Score:0

How to give seconds of delay before after Button press

tv flag

I want a 3 second delay after a condition is satisfied which should not stop any other process.

When I press a button for 3 seconds I want a return value true (timer should only start after button is pressed) else it must be false. Any suggestions how to do it? Here's what I have tried but is not working

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)


def button():
    if GPIO.input(3) == GPIO.LOW:
      start_time =time.time()
      if (time.time() - start_time) >= 5:
        return True
      else:
        return False
    else:
      return False
David avatar
cn flag
What version of Ubuntu are you using?
Eldho K K avatar
tv flag
ubuntu server 20.04
Score:0
tv flag

Here is the solution

def button():
    global start_time, end_time
    if GPIO.input(3) == GPIO.HIGH:
        start_time = time.time()
        return False
    if GPIO.input(3) == GPIO.LOW:
        end_time = time.time()
        if (end_time - start_time) >= 3:
            return True    
        else:
            return False
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.