运行 LED 闪烁和蜂鸣器同时发声的功能
Run functions for LED flashing and Buzzer sound simultaneously
我正在做一个 Raspberry Pi 安全小项目,该项目有 LED、压电蜂鸣器、9 位数字键盘和 LCD 屏幕(完整资源在这里:https://github.com/kevbo423/RPHSP/blob/master/Keypad.py)。
简而言之,当用户使用键盘输入错误的密码时,我希望 LED 闪烁,同时蜂鸣器熄灭。我一辈子都想不出如何做到这一点。我已创建并单独工作的 LED 和蜂鸣器功能,但无法让它们同时熄灭。
# Function for error LED feedback
def errorLED():
for i in range(3):
GPIO.output(6,1)
time.sleep(0.2)
GPIO.output(6,0)
GPIO.output(12,1)
time.sleep(0.2)
GPIO.output(12,0)
# Function for Buzzer Alarm Output
def buzzerAlarm():
for i in range(3):
GPIO.output(21,1)
time.sleep(0.5)
GPIO.output(21,0)
time.sleep(0.5)
我研究过使用线程模块,但是 运行 当我尝试执行线程时蜂鸣器发出多次蜂鸣声时出现了问题。下面是我创建的测试代码,用于多次 运行 蜂鸣器线程...
# Buzzer Test
buzzerSound = threading.Thread(target=buzzerAlarm())
buzzerSound.start()
time.sleep(3)
buzzerSound.start()
time.sleep(3)
buzzerSound.start()
time.sleep(3)
...以及我收到的错误消息。
Traceback (most recent call last):
File "Keypad.py", line 277, in <module>
buzzerSound.start()
File "/usr/lib/python2.7/threading.py", line 730, in start
raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
我使用线程模块的方向是否正确?如果是这样,我应该终止 BuzzerAlarm()
函数内部的蜂鸣器线程还是不应该使用 .start()
到 运行 函数?还是我需要完全做其他事情?如果线程不是最好的方法,我愿意接受建议。
start()
It must be called at most once per thread object.
因此,如果您想再次启动它,您应该创建一个新的 Thread
对象。例如:
buzzer_thread = threading.Thread(target=buzzerAlarm())
led_thread = threading.Thread(target=errorLED())
led_thread.start()
buzzer_thread.start()
time.sleep(3)
# Second time:
buzzer_thread = threading.Thread(target=buzzerAlarm())
led_thread = threading.Thread(target=errorLED())
led_thread.start()
buzzer_thread.start()
你可以考虑将其重构为一个函数:
def buzzer_and_led():
buzzer_thread = threading.Thread(target=buzzerAlarm())
led_thread = threading.Thread(target=errorLED())
led_thread.start()
buzzer_thread.start()
# Wait until both the buzzer and the LED stop
while led_thread.is_alive() or buzzer_thread.is_alive():
pass
首先不是多线程答案,这个答案应该仍然有效。
好的,快回答。你可以做的是结合这两个功能
# Function for error LED feedback
def errorLED():
for i in range(3):
GPIO.output(6,1)
time.sleep(0.2)
GPIO.output(6,0)
GPIO.output(12,1)
time.sleep(0.2)
GPIO.output(12,0)
# Function for Buzzer Alarm Output
def buzzerAlarm():
for i in range(3):
GPIO.output(21,1)
time.sleep(0.5)
GPIO.output(21,0)
time.sleep(0.5)
这会变成
#Combined function
def combined():
for i in range(3):
GPIO.output(6,1) #Error LED
GPIO.output(21,1) #Alarm Buzzer
time.sleep(0.25) #Notice times changed by 0.05 so it can do the combination easyer
GPIO.output(6,0)
GPIO.output(12,1)
time.sleep(0.25)
GPIO.output(12,0)
#Repeats Code
GPIO.output(6,1)
GPIO.output(21,0) #Only difference is turning off the buzzer/alarm
time.sleep(0.25)
GPIO.output(6,0)
GPIO.output(12,1)
time.sleep(0.25)
GPIO.output(12,0)
告诉我这是否有效,我已经有一段时间没有使用我的 Raspberry PI
我正在做一个 Raspberry Pi 安全小项目,该项目有 LED、压电蜂鸣器、9 位数字键盘和 LCD 屏幕(完整资源在这里:https://github.com/kevbo423/RPHSP/blob/master/Keypad.py)。
简而言之,当用户使用键盘输入错误的密码时,我希望 LED 闪烁,同时蜂鸣器熄灭。我一辈子都想不出如何做到这一点。我已创建并单独工作的 LED 和蜂鸣器功能,但无法让它们同时熄灭。
# Function for error LED feedback
def errorLED():
for i in range(3):
GPIO.output(6,1)
time.sleep(0.2)
GPIO.output(6,0)
GPIO.output(12,1)
time.sleep(0.2)
GPIO.output(12,0)
# Function for Buzzer Alarm Output
def buzzerAlarm():
for i in range(3):
GPIO.output(21,1)
time.sleep(0.5)
GPIO.output(21,0)
time.sleep(0.5)
我研究过使用线程模块,但是 运行 当我尝试执行线程时蜂鸣器发出多次蜂鸣声时出现了问题。下面是我创建的测试代码,用于多次 运行 蜂鸣器线程...
# Buzzer Test
buzzerSound = threading.Thread(target=buzzerAlarm())
buzzerSound.start()
time.sleep(3)
buzzerSound.start()
time.sleep(3)
buzzerSound.start()
time.sleep(3)
...以及我收到的错误消息。
Traceback (most recent call last):
File "Keypad.py", line 277, in <module>
buzzerSound.start()
File "/usr/lib/python2.7/threading.py", line 730, in start
raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
我使用线程模块的方向是否正确?如果是这样,我应该终止 BuzzerAlarm()
函数内部的蜂鸣器线程还是不应该使用 .start()
到 运行 函数?还是我需要完全做其他事情?如果线程不是最好的方法,我愿意接受建议。
start()
It must be called at most once per thread object.
因此,如果您想再次启动它,您应该创建一个新的 Thread
对象。例如:
buzzer_thread = threading.Thread(target=buzzerAlarm())
led_thread = threading.Thread(target=errorLED())
led_thread.start()
buzzer_thread.start()
time.sleep(3)
# Second time:
buzzer_thread = threading.Thread(target=buzzerAlarm())
led_thread = threading.Thread(target=errorLED())
led_thread.start()
buzzer_thread.start()
你可以考虑将其重构为一个函数:
def buzzer_and_led():
buzzer_thread = threading.Thread(target=buzzerAlarm())
led_thread = threading.Thread(target=errorLED())
led_thread.start()
buzzer_thread.start()
# Wait until both the buzzer and the LED stop
while led_thread.is_alive() or buzzer_thread.is_alive():
pass
首先不是多线程答案,这个答案应该仍然有效。
好的,快回答。你可以做的是结合这两个功能
# Function for error LED feedback
def errorLED():
for i in range(3):
GPIO.output(6,1)
time.sleep(0.2)
GPIO.output(6,0)
GPIO.output(12,1)
time.sleep(0.2)
GPIO.output(12,0)
# Function for Buzzer Alarm Output
def buzzerAlarm():
for i in range(3):
GPIO.output(21,1)
time.sleep(0.5)
GPIO.output(21,0)
time.sleep(0.5)
这会变成
#Combined function
def combined():
for i in range(3):
GPIO.output(6,1) #Error LED
GPIO.output(21,1) #Alarm Buzzer
time.sleep(0.25) #Notice times changed by 0.05 so it can do the combination easyer
GPIO.output(6,0)
GPIO.output(12,1)
time.sleep(0.25)
GPIO.output(12,0)
#Repeats Code
GPIO.output(6,1)
GPIO.output(21,0) #Only difference is turning off the buzzer/alarm
time.sleep(0.25)
GPIO.output(6,0)
GPIO.output(12,1)
time.sleep(0.25)
GPIO.output(12,0)
告诉我这是否有效,我已经有一段时间没有使用我的 Raspberry PI