简体   繁体   中英

python - how to make a function to check if button pressed once or twice?

I am trying to make a task for school with Python. But the problem is that I don't understand how the following function works: So we are trying to make a smarthome with lights and relays(now we use LED). If we push the button once, lights go on, if we push twice within 1.5sec: all lights go on and so on... Also we have to make a function when lights are already on...

So my question is "How do I tell or let python know that I pushed the button once or twice or more or hold it so it does a function?"

This is my code (we work with on the air uploading to Raspberry Pi

#!/usr/bin/env

__author__ = "Zino Henderickx"
__version__ = "1.0"

# LIBRARIES
from gpiozero import LED
from gpiozero import Button
from gpiozero.pins.pigpio import PiGPIOFactory
import time

IP = PiGPIOFactory('192.168.0.207')

# LED's
LED1 = LED(17, pin_factory=IP)
LED2 = LED(27, pin_factory=IP)
LED3 = LED(22, pin_factory=IP)
LED4 = LED(10, pin_factory=IP)


# BUTTONS
BUTTON1 = Button(5, pin_factory=IP)
BUTTON2 = Button(6, pin_factory=IP)
BUTTON3 = Button(13, pin_factory=IP)
BUTTON4 = Button(19, pin_factory=IP)


# LISTS

led [10, 17, 22, 27]
button [5, 6, 13, 19]

def button_1():
    if BUTTON1.value == 1:
        LED1.on()
        time.sleep(0.50)
    if BUTTON1.value == 0:
        LED1.on()
        time.sleep(0.50)


def button_2():
    if BUTTON2.value == 1:
        LED2.on()
        time.sleep(0.50)
    if BUTTON2.value == 0:
        LED2.on()
        time.sleep(0.50)


def button_3():
    if BUTTON3.value == 1:
        LED3.on()
        time.sleep(0.50)
    if BUTTON3.value == 0:
        LED3.on()
        time.sleep(0.50)


def button_4():
    if BUTTON4.value == 1:
        LED4.on()
        time.sleep(0.50)
    if BUTTON4.value == 0:
        LED4.on()
        time.sleep(0.50)

def check_button():
    while True:
        for i in range(BUTTON1):
            toggle_button(i)


def main():
    set_up_led()
    set_up_button()
    check_button()

# MAIN START PROGRAM


if __name__ == "__main__":
    main()

Have you thought about setting up some sort of timer that goes off as soon as a button is pressed? This would then trigger a loop that will check for a given time if the button is pressed again or not. If the time has elapsed, the loop ends and it executes one instruction, and if the button has been reset, the program executes the other instruction.

import time
#define max_time to consider a double execution
max_time = 1
while 1:
    time.sleep(0.001) # do not use all the cpu power
    # make a loop to test for the button being pressed
    if button == pressed:
        when_pressed = time.time()
        while time.time() - when_pressed < max_time:
            time.sleep(0.001) # do not use all the cpu power
            if button == pressed:
                # Instructions 2
        #Instructions 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM