Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将arduino代码翻译成python pyfirmata?_Python_Python 3.x_Arduino_Arduino Uno_Firmata - Fatal编程技术网

如何将arduino代码翻译成python pyfirmata?

如何将arduino代码翻译成python pyfirmata?,python,python-3.x,arduino,arduino-uno,firmata,Python,Python 3.x,Arduino,Arduino Uno,Firmata,我想用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);

我想用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())