pyFirmata icon indicating copy to clipboard operation
pyFirmata copied to clipboard

pyfirmata in a fast loop

Open acca84 opened this issue 9 years ago • 1 comments

Hello,

I'd like to use pyfirmata to read values from analog port of arduino and display it on a screen using a raspberry pi and pygame. My python code looks like the following :

imports
variables definitions
pygame configuration
board=Arduino('/dev/ttyUSB0',baudrate=9600)
it=util.Iterator(board)
it.start()
board.analog[1].enable_reporting()
board.analog[2].enable_reporting()
sleep(0.5)

while True:
    val1=board.analog[1].read()
    val2=board.analog[2].read()

    computing values from val1 and val2
    displaying values using pygame

it works well on a raspberry pi B+ But on a raspberry pi 2 wich is faster, i can get first analog values then values are not updated, i can see the Arduino Tx light stop blinking after approx 1second.

If i add a sleep(0.1) at the end of the while loop, it works but my animation is not smooth.

Do i stress pyfirmata too much ?

I have tried to read analog values every 10 loops this way (i is iniated at value 0) :

if (i==0):
    val1=board.analog[1].read()
    val2=board.analog[2].read()
i=i+1
if (i>10):
    i=0

but it doesn't works (tx led stays off after 1s)

How can i have updated values without slowing my loop please ? I only want the latest up to date analog value of analog1 and analog2

I hope it's clear, sorry for my poor english.

Regards

acca84 avatar Oct 19 '16 13:10 acca84

Running board.analog[x].read() doesn't have any influence on the communication with the Arduino, it just returns the set value (https://github.com/tino/pyFirmata/blob/master/pyfirmata/pyfirmata.py#L514).

What might have effect is the sleep time of the iterator (https://github.com/tino/pyFirmata/blob/master/pyfirmata/util.py#L50), but I also don't think that is the case. I can run an Arduino with pyFirmata on my MBP, which certainly is faster than an RPi 2 :).

So you probably have to look into other possibilities, maybe the iterator dies or there is something wrong with the USB?

tino avatar Oct 29 '16 13:10 tino