螺纹不';不要像我说的那样在RPi上用Python中的GPIOs运行
我正在我的树莓圆周率上创建一个脚本,如果你按下一个按钮,按钮上的led需要闪烁,直到我再次按下按钮。 这是我的代码:螺纹不';不要像我说的那样在RPi上用Python中的GPIOs运行,python,multithreading,raspberry-pi,gpio,Python,Multithreading,Raspberry Pi,Gpio,我正在我的树莓圆周率上创建一个脚本,如果你按下一个按钮,按钮上的led需要闪烁,直到我再次按下按钮。 这是我的代码: #!/usr/bin/python import RPi.GPIO as GPIO import threading from time import sleep GPIO.setmode(GPIO.BCM) pushbutton = 2 led = 3 GPIO.setup(pushbutton, GPIO.IN) GPIO.setup(led, GPIO.OUT) cla
#!/usr/bin/python
import RPi.GPIO as GPIO
import threading
from time import sleep
GPIO.setmode(GPIO.BCM)
pushbutton = 2
led = 3
GPIO.setup(pushbutton, GPIO.IN)
GPIO.setup(led, GPIO.OUT)
class rpiThread(threading.Thread):
def __init__(self):
self.running = False
super(rpiThread, self).__init__()
def start(self):
self.running = True
super(rpiThread, self).start()
def run(self):
self.running = True
while (self.running == True):
GPIO.output(led, GPIO.HIGH)
print "HIGH"
sleep(0.05)
GPIO.output(led,GPIO.LOW)
print "LOW"
sleep(0.05)
def stop(self):
self.running = False
GPIO.output(led, GPIO.LOW)
def main():
myrpiThread = rpiThread()
pushed = GPIO.input(pushbutton)
try:
while(True):
if(pushed == False):
if(GPIO.input(pushbutton) == False):
sleep(0.5)
if(GPIO.input(pushbutton) == False):
myrpiThread.start()
pushed = True
print "The button is pushed"
else:
if(GPIO.input(pushbutton) == True):
GPIO.output(led, GPIO.LOW)
myrpiThread.stop()
pushed = False
print "The button is not pushed"
except KeyboardInterrupt:
print "QUIT"
main()
每次运行脚本时,指示灯都不会每0.05秒闪烁一次。有时它需要2秒钟才能打开,有时它只是不闪烁。
我不知道我做错了什么?有人能帮我找出问题所在吗
GPIO引脚是否可能不用于多线程 当我需要在GPIO中使用线程回调时,我遵循了本教程: . 在注释中,它还对如何避免使用全局变量从线程传回值进行了很好的讨论 这将有助于确切地了解您的按钮是如何接线的,以及您使用的按钮类型,以便我能够更具体地回答您的问题 我编写了以下示例代码,假设按钮从GPIO 2连接到+3.3v 如果您将其连接到地面,请使用“向上拉”=GPIO.PUD\u down“和“GPIO.FALLING” 它还假设您有一个瞬时按钮。当检测到按钮按下时,它会翻转一个全局布尔值。在主循环中检查该值,如果按下=True,则调用闪烁(led)功能
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
pushbutton = 2
led = 3
GPIO.setup(pushbutton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(led, GPIO.OUT)
pushed = False
# this will run in another thread when the button is pushed
def button_callback(channel):
pushed = not pushed
GPIO.add_event_detect(pushbutton, GPIO.RISING, callback=button_callback)
while True:
if pushed:
blink(led)
def blink(led):
GPIO.output(led, True)
sleep(0.5)
GPIO.output(led, False)
我现在没有一个树莓圆周率在我面前测试这段代码,但我会检查它以后。您可能需要针对特定设置修改此设置,但希望它能让您走上正确的方向。当我需要在GPIO中使用线程回调时,我遵循了本教程: . 在注释中,它还对如何避免使用全局变量从线程传回值进行了很好的讨论 这将有助于确切地了解您的按钮是如何接线的,以及您使用的按钮类型,以便我能够更具体地回答您的问题 我编写了以下示例代码,假设按钮从GPIO 2连接到+3.3v 如果您将其连接到地面,请使用“向上拉”=GPIO.PUD\u down“和“GPIO.FALLING” 它还假设您有一个瞬时按钮。当检测到按钮按下时,它会翻转一个全局布尔值。在主循环中检查该值,如果按下=True,则调用闪烁(led)功能
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
pushbutton = 2
led = 3
GPIO.setup(pushbutton, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(led, GPIO.OUT)
pushed = False
# this will run in another thread when the button is pushed
def button_callback(channel):
pushed = not pushed
GPIO.add_event_detect(pushbutton, GPIO.RISING, callback=button_callback)
while True:
if pushed:
blink(led)
def blink(led):
GPIO.output(led, True)
sleep(0.5)
GPIO.output(led, False)
我现在没有一个树莓圆周率在我面前测试这段代码,但我会检查它以后。您可能需要根据您的特定设置修改此设置,但希望它能让您朝着正确的方向前进