Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带延时的覆盆子pi关机按钮_Python_Raspberry Pi - Fatal编程技术网

Python 带延时的覆盆子pi关机按钮

Python 带延时的覆盆子pi关机按钮,python,raspberry-pi,Python,Raspberry Pi,我有一个raspberry Pi,我使用以下脚本在GPIO上添加了一个关机按钮: import RPi.GPIO as GPIO import os gpio_pin_number=21 GPIO.setmode(GPIO.BCM) GPIO.setup(gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP) try: GPIO.wait_for_edge(gpio_pin_number, GPIO.FALLING) os.sy

我有一个raspberry Pi,我使用以下脚本在GPIO上添加了一个关机按钮:

import RPi.GPIO as GPIO
import os

gpio_pin_number=21
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    GPIO.wait_for_edge(gpio_pin_number, GPIO.FALLING)
    os.system("sudo shutdown -h now")
except:
    pass

GPIO.cleanup()
它就像一个符咒,但不止一次我是无意中按下了按钮,所以为了避免这个问题,我想修改脚本,使它需要你按住按钮N秒,而不是仅仅按下它


因此,如果有人能给我指出正确的方向(我不是很擅长python),我会很高兴。

睡眠N秒,使用GPIO.input读取GPIO值,如果仍然为0,则运行关机命令。–larsks 1分钟前编辑

import RPi.GPIO as GPIO
import os
import time

gpio_pin_number=21
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    GPIO.wait_for_edge(gpio_pin_number, GPIO.FALLING)
    time.sleep(2)
    if GPIO.input(gpio_pin_number) == 0:
        break

os.system("sudo shutdown -h now")

以上要求您按住按钮两秒钟。

睡眠N秒,使用GPIO.input读取GPIO值,如果仍然为0,则运行关机命令。–larsks 1分钟前编辑

import RPi.GPIO as GPIO
import os
import time

gpio_pin_number=21
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    GPIO.wait_for_edge(gpio_pin_number, GPIO.FALLING)
    time.sleep(2)
    if GPIO.input(gpio_pin_number) == 0:
        break

os.system("sudo shutdown -h now")
以上要求您按住按钮两秒钟