有人可以帮我用这个算法点亮3个LED吗?

时间:2015-09-18 18:49:52

标签: python algorithm raspberry-pi

所以我得到了一个Raspberry Pi,我一直在玩一些基本的算法和设置。

我用一个按钮和3个LED制作了一个电路。当按下按钮时,我希望第一个LED点亮,如果按住按钮2秒钟,第二个LED亮起,如果按住按钮2秒钟,第三个LED亮起。然后当释放按钮时,所有三个LED熄灭。现在只有第一个LED点亮。

有人可以查看我的代码并帮助我弄清楚它为什么不起作用?

我不认为面包板上的物理布线存在问题,但如果我们发现代码有任何问题,我也会在那里寻找问题。

这是我的Python代码。

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(12, GPIO.IN)
GPIO.output(11, False)
GPIO.output(13, False)
GPIO.output(15, False)

def seconds_passed(oldepoch):
    if time.time() - oldepoch >= 2:
        return True

while True:
    input_value = GPIO.input(12)
    if input_value == False:
        print "The button has been pressed, lighting first LED."
        GPIO.output(11, True)
        while input_value == False:
            input_value = GPIO.input(12)
        if input_value == False and seconds_passed() == True:
            print "The button is still being pressed, lighting second LED."
            GPIO.output(13, True)
            while input_value == False:
                input_value = GPIO.input(13)
            if input_value == False and seconds_passed() == True:
                print "The button is still being pressed, lighting third LED."
                GPIO.output(15, True)
                while input_value == False:
                    input_value = GPIO.input(15)
        print "The button has been released, extinguishing LEDs."
    if input_value == True:
        GPIO.output(11, False)
        GPIO.output(13, False)
        GPIO.output(15, False)

拜托,谢谢!

1 个答案:

答案 0 :(得分:2)

你有一个无限循环

while True:
    input_value = GPIO.input(12)
    if input_value == False:
        print "The button has been pressed, lighting first LED."
        GPIO.output(11, True)

*** here ***
*** while input_value == False:
            input_value = GPIO.input(12)

你需要摆脱这个循环来点亮LED 2和3,但是在释放按钮之前你永远不会离开while循环

        if input_value == False ... 

然后在释放按钮时进行此测试,但是此测试需要按下按钮才能尝试点亮LED 2和3.因为按钮必须被释放才能到达此处,所以从不匹配。 “if”下面的所有代码都没有运行。

如果代码到目前为止,你有同样的问题,LED 2和3都有自己的永久循环。

您需要不同的设计。像这样(未经测试的)伪代码:

import time

button_now_pressed = False

while True:
    # Update button state each time through the loop

    button_was_pressed = button_now_pressed
    button_now_pressed = not GPIO.input(12)


    # Process button state, compared to
    # previous run through the loop    

    if (not button_was_pressed) and button_now_pressed:
        # button was pressed since last loop run

        # save time button was pressed
        press_time = time.time()

        # Light LED 1


    if button_was_pressed and (not button_now_pressed):
        # button was released since last loop run
        # turn LEDs off


     if button_was_pressed and button_now_pressed:
        # button stayed down

        # how long has it been pressed? 
        current_time = time.time()
        if current_time - press_time > 2:
            # light LED 2
        if current_time - press_time > 4:
            # light LED 3

(我假设您的代码中GPIO.input()通常返回True,按下按钮时返回False。我实际上并不知道这是否正确。