K1 K2 分别接树莓派上GPIO口上,G接地
代码是复制别人的
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
button = 4
GPIO.setup(button, GPIO.IN,pull_up_down=GPIO.PUD_UP)
press_times=0
while True:
if GPIO.input(button) == 0:
press_times+=1
elif press_times>0:
if press_times<4:
#4为我自己设置的长短按钮分界线,大家可以根据自己的按按钮习惯通过代码自己设定
print("短按按钮")
else:
print("长按按钮")
press_times = 0
time.sleep(.1)