我无法用python读取模拟引脚,pyfirmata

rqdpfwrv  于 2022-12-17  发布在  Python
关注(0)|答案(1)|浏览(84)

我试图读取按钮的模拟引脚。但无法读取。总是出现此错误。
我的代码:

import pyfirmata
import time
from pyfirmata import util

port = "COM4" # port number
board = pyfirmata.Arduino(port)

it = util.Iterator(board)
it.start()

pin_redLed = 8 # red LED pin number

board.analog[11].enable_reporting()


while True : 
            print (board.analog[11].read())
            time.sleep(1)

错误信息:enter image description here

b0zn9rqh

b0zn9rqh1#

我解决了:

import pyfirmata
import time
from pyfirmata import util

port = "COM4" # port number
board = pyfirmata.Arduino(port)

it = pyfirmata.util.Iterator(board)
it.start()

pin_redLed = 8 # red LED pin number
pin_button = 10 # button pin number

board.digital[pin_button].mode = pyfirmata.INPUT

while True : 
        sw = board.digital[pin_button].read()
        if sw is True :
                board.digital[pin_redLed].write(1)
        else :
                board.digital[pin_redLed].write(0)
        time.sleep(0.1)

相关问题