最近一次通话最近

最近一次通话最近

本文介绍了TraceBack(最近一次通话最近),以及GPIO.setmode(GPIO.BOARD)或GPIO.setmode(GPIO.BCM)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力编写一个脚本,该脚本将打开Pi的BCM引脚17、27和10 1秒钟,然后依次关闭1秒钟100ish次.当我运行脚本时,LED1将打开,然后关闭,程序会因以下错误而关闭:

I have been struggling with a script that will turn a Pi's BCM pins 17, 27, and 10 on for 1 second then off for 1 second sequentially 100ish times. When I run the script LED1 will turn on and then it turns off and the program shuts down with this error:

Traceback (most recent call last):
  File "LedBlink.py", line 47, in <module>
    LED2Blink()
  File "LedBlink.py", line 27, in LED2Blink
    GPIO.setup(LED2, GPIO.OUT)
RuntimeError: Please set pin numbering mode using     GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

import RPi.GPIO as GPIO
import time

LED1 = 17
LED2 = 27
LED3 = 10

GPIO.setmode(GPIO.BCM)

def LED1Blink():
        GPIO.setup(LED1, GPIO.OUT)
        GPIO.output(LED1,True)
        time.sleep(1)
        GPIO.output(LED1,False)
        time.sleep(1)
        GPIO.cleanup()

def LED2Blink():
        GPIO.setup(LED2, GPIO.OUT)
        GPIO.output(LED2,True)
        time.sleep(1)
        GPIO.output(LED2,False)
        time.sleep(1)
        GPIO.cleanup()

def LED3Blink():
        GPIO.setup(LED3, GPIO.OUT)
        GPIO.output(LED3,True)
        time.sleep(1)
        GPIO.output(LED3,False)
        time.sleep(1)
        GPIO.cleanup()

i = 0
while i < 100:
       LED1Blink()
       LED2Blink()
       LED3Blink()
       i + 1
else:
       print "finished loop"

推荐答案

问题是您在每个方法的结尾都调用了GPIO.cleanup().如文档中所述,Note that GPIO.cleanup() also clears the pin numbering system in use.您要的是GPIO.cleanup(channel)代替,其中channel对应于脚本中的LED1, LED2, LED3.

The problem is that you are calling GPIO.cleanup() at the end of each methods. As stated in the documentation, Note that GPIO.cleanup() also clears the pin numbering system in use. What you want is GPIO.cleanup(channel) instead, where channel corresponds to LED1, LED2, LED3 in your script.

最佳做法是仅一次 设置清理频道

The best practice is to setup and cleanup the channels ONLY ONCE, e.g.

import RPi.GPIO as GPIO
import time

LED1 = 17
LED2 = 27
LED3 = 10

GPIO.setmode(GPIO.BCM)
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
GPIO.setup(LED3, GPIO.OUT)

def LED1Blink():
        GPIO.output(LED1,True)
        time.sleep(1)
        GPIO.output(LED1,False)
        time.sleep(1)

def LED2Blink():
        GPIO.output(LED2,True)
        time.sleep(1)
        GPIO.output(LED2,False)
        time.sleep(1)

def LED3Blink():
        GPIO.output(LED3,True)
        time.sleep(1)
        GPIO.output(LED3,False)
        time.sleep(1)

i = 0
if i < 100:
       LED1Blink()
       LED2Blink()
       LED3Blink()
       i + 1
else:
       GPIO.cleanup()
       print "finished loop"

这篇关于TraceBack(最近一次通话最近),以及GPIO.setmode(GPIO.BOARD)或GPIO.setmode(GPIO.BCM)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:19