tino / pyFirmata

Python interface for the Firmata (http://firmata.org/) protocol. It is compliant with Firmata 2.1. Any help with updating to 2.2 is welcome. The Capability Query is implemented, but the Pin State Query feature not yet.
MIT License
575 stars 193 forks source link

Why is the first values read from the arduino always None #92

Open martynwheeler opened 4 years ago

martynwheeler commented 4 years ago

I am trying to read analog data from the arduino. Whenever I read the data the first value is always None. Subsequent readings are okay. I have implemented a check in my code but i was wondering why this was the case.

Here is my code:

#!/usr/bin/python3
# Import Libraries
import time
import pyfirmata

# Creates a new board
board = pyfirmata.Arduino('/dev/ttyAMA0')

# Start the iterator
it = pyfirmata.util.Iterator(board)
it.start()

# Setup the analog pin
analog_input = board.get_pin('a:0:i')

# get the data
try:
    analog_value = analog_input.read()
    while analog_value == None:
        analog_value = analog_input.read()
        time.sleep(0.1)
finally:
    board.exit()
    it = None

print(analog_value)

I have tried waiting 1 second before the first reading but this makes no difference

Thanks

Martyn

JeS24 commented 3 years ago

@martynwheeler I think, this answers the "how" of your question: https://github.com/tino/pyFirmata/issues/57#issuecomment-381686268. In particular, "read() can return None in the beginning when there aren't any reads yet.".