如何使用按钮使用python和树莓派打开和关闭LED?

时间:2019-11-10 18:21:57

标签: python button raspberry-pi3

我正在尝试使用按钮来打开和关闭LED。我使用了一些在网上找到的示例代码来打开LED,但仅在按下按钮时才能打开。我想运行一个脚本,该脚本检测何时按下了按钮,并且如果指示灯点亮,则指示灯熄灭。如果指示灯熄灭,则指示灯点亮。

我收到以下错误:

  

TypeError:函数正好接受2个参数(给定1个参数)

代码:

public interface UserRepository extends JpaRepository<User, Long> {
    @Query("SELECT u FROM User u inner join u.events e WHERE e.id = ?1")
    public List<User> findUsersByEventId(long eventId);
}

public interface EventRepository extends JpaRepository<Events, Long> {
    @Query("SELECT e FROM User u inner join u.events e WHERE u.id = ?1")
    public List<Events> findEventsByUserId(long userId);
}

1 个答案:

答案 0 :(得分:0)

解决方案:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(13,GPIO.IN) #button
GPIO.setup(21,GPIO.OUT) #led
while True:

    # Turn LED off
    print ("LED off")
    GPIO.output(21, GPIO.LOW)

    # waiting for button press
    while GPIO.input(13) == 1:
        time.sleep(0.2) 

    # Turn LED on
    print ("LED on")
    GPIO.output(21, GPIO.HIGH)


    # waiting for button press
    while GPIO.input(13) == 1:
        time.sleep(0.2)

    print ("LED off")
    GPIO.output(21, GPIO.LOW)

希望此代码对您有帮助。干杯。