我想使用 pyfirmata 将这个 Arduino 代码翻译成 python 代码。我怎样才能做到这一点?
int sw = 0;
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(6, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
sw = digitalRead(2);
if (sw == LOW){
digitalWrite(6, LOW);
}
else {
digitalWrite(6, HIGH);
}
}
我试着做
from pyfirmata import Arduino, util
board = Arduino('COM3')
it = util.Iterator(board)
it.start()
button = board.get_pin('d:2:i')
led = board.get_pin('d:6:o')
while True:
sw = button.read()
print(sw)
if sw:
led.write(1)
else:
led.write(0)
但这不起作用,当我打印sw
它时返回None
然后我尝试这样做,但它只是一直返回None
。
from pyfirmata import Arduino, util, INPUT
board = Arduino('COM3')
it = util.Iterator(board)
it.start()
while True:
board.digital[2].mode = INPUT
board.digital[2].enable_reporting()
print(board.digital[2].read())