我在这里工作的交通信号灯和一个工作交叉路口,我基本上完成了我的代码,我似乎无法让我的按钮为我的红绿灯工作。这是我的作业代码
` import RPi.GPIO as GPIO
import time
#setup
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#LED setup and Button
led = [19,13,6,22]
button = 21
# GPIO output channel
GPIO.setup(19, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(button, GPIO.IN, pull_up_down = GPIO.PUD_UP)
#on and off function
#Run traffic light loop till button is pressed. Red=19,yellow=6,green=13.
try:
while True:
GPIO.output(19,GPIO.HIGH)
time.sleep(7)
GPIO.output(19,GPIO.LOW)
GPIO.output(13,GPIO.HIGH)
time.sleep(10)
GPIO.output(13,GPIO.LOW)
GPIO.output(6,GPIO.HIGH)
time.sleep(3)
GPIO.output(6,GPIO.LOW)
#When button is pressed
if (GPIO.input(button) == False):
for i in range (0,16, 1):
GPIO.output(22, GPIO.HIGH)
time.sleep(.25)
GPIO.output(22, GPIO.LOW)
time.sleep(.25)
except KeyboardInterrupt:
print("Reset Program")
finally:
GPIO.cleanup()`
有人可以帮我解释一下发生了什么吗?
答案 0 :(得分:0)
如果您没有提供任何运行时错误,则很难诊断。假设您的代码运行良好且没有错误,并且所有连线都以正确方式连接(请参阅https://pinout.xyz/以根据BCM值连接到正确的标头引脚),然后检查代码。
通过它的外观,如果你有缩进,你有你的if语句按钮按下无限循环(while True
),所以它实际上不会检查看到如果按下按钮,就像完成灯光一样,它就会重新开始!
下面我写了一个建议的更新脚本,这也使代码更具可读性。 注意,我建议将引脚更改为更适合于通用GPIO使用的引脚。
作为旁注,您可能需要查看gpiozero 作为Pi上标准GPIO库的替代方案。
import RPi.GPIO as GPIO
import time
# setup
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# LED setup and Button
red_led = 17 # bcm 17 = header pin 11
yellow_led = 27 # bcm 27 = header pin 13
green_led = 22 # bcm 22 = header pin 15
other_led = 23 # bcm 23 = header pin 16
button = 24 # bcm 24 = header pin 18
# GPIO output channel
GPIO.setup(red_led, GPIO.OUT)
GPIO.setup(green_led, GPIO.OUT)
GPIO.setup(yellow_led, GPIO.OUT)
GPIO.setup(other_led, GPIO.OUT)
GPIO.setup(button, GPIO.IN, pull_up_down = GPIO.PUD_UP)
# Run traffic light loop till button is pressed.
# Red=0, Green=1, Yellow=2
light = 0
def change_lights():
global light, red_led, green_led, yellow_led
light = (light+1)%3 # each call iterates one step through a 0,1,2 loop
if light == 0:
GPIO.output(red_led,GPIO.HIGH)
GPIO.output(yellow_led,GPIO.LOW)
GPIO.output(green_led,GPIO.LOW)
elif light == 1:
GPIO.output(red_led,GPIO.LOW)
GPIO.output(yellow_led,GPIO.LOW)
GPIO.output(green_led,GPIO.HIGH)
elif light == 2:
GPIO.output(red_led,GPIO.LOW)
GPIO.output(yellow_led,GPIO.HIGH)
GPIO.output(green_led,GPIO.LOW)
def button_pressed():
global other_led
for i in range(16):
GPIO.output(other_led, GPIO.HIGH)
time.sleep(.25)
GPIO.output(other_led, GPIO.LOW)
time.sleep(.25)
try:
GPIO.output(red_led,GPIO.HIGH)
while True:
start_time = time.time() # marks the current time
# we now poll the current time at the same time as
# polling to see if the button was pressed
while time.time() < start_time+7:
if (GPIO.input(button) == False):
button_pressed()
change_lights() # we cycle to the next light
start_time = time.time() # reset the start time
while time.time() < start_time+10:
if (GPIO.input(button) == False):
button_pressed()
change_lights() # we cycle to the next light
start_time = time.time() # reset the start time
while time.time() < start_time+3:
if (GPIO.input(button) == False):
button_pressed()
change_lights() # we cycle to the next light
except KeyboardInterrupt:
print("Reset Program")
finally:
GPIO.cleanup()
道歉,因为我无法保证代码能够完美运行,现在无法轻松访问我的Pi和电子产品。
轮询按钮和时间的替代方法是使用中断(我不建议在这样一个简单的应用程序中使用。
聚苯乙烯。通常使用全局变量并不是一种好的做法,但我认为你可以在这一点上侥幸成功......