arduino reading input as "none" pyfirmata

190 Views Asked by At

so, i am trying to get an input from a joystick hooked upto an arduino in python and then multiplying it's input value by 1023, but every time i try to multiply the input, the arduino returns the value of x and y as None, but as soon as i get rid of the multiplication, it works as normal, what could be the problem? here's my code:

import pyfirmata
import time

board = pyfirmata.Arduino('COM3')
it = pyfirmata.util.Iterator(board)  
it.start()
board.analog[0].mode=pyfirmata.INPUT
board.analog[0].mode=pyfirmata.INPUT
board.analog[0].enable_reporting()
board.analog[1].enable_reporting()
board.digital[6].mode=pyfirmata.INPUT

while True:
    x=board.analog[0].read()
    y=board.analog[1].read()
    x=x*1023
    y=y*1023
    print(x,y)
    time.sleep(0.5)
1

There are 1 best solutions below

0
maxp On

Some kind of serialization error is probably occurring, the pyfirmata library doesn't seem to have any kind of serialization error handling , and the library is likely populating your analog values with None.

It will be up to you to handle transmission and serialization errors:

...
x=board.analog[0].read()
y=board.analog[1].read()
if( x==None or y==None):
    continue
x=x*1023
y=y*1023
...

Another tip to avoid serialization issues:

  • Pyfirmata expects to be startup at the same time as the Arduino (there is setup data sent). If you restart your Python code, be sure to reset the Arduino too.