I'm not sure about how to properly handle timing readings from serial inside a Python script. I need to read a value every 5 seconds.
From Arduino I could do,
Serial.println(value);
delay(5000); // should I wait here?
Inside Python script I could do,
import serial
import time
arduino = serial.Serial('/dev/ttyACM1', 9600, timeout=5) # should I set timeout?
while True and arduino.isOpen():
try:
value = arduino.readline().strip()
print value
time.sleep(5) # should I wait here?
except KeyboardInterrupt:
print "\nClosing..."
arduino.close()
EDIT: Apart from that, the first reading is always wrong, like if there was something in the reading buffer.
I've found out that to solve that I should add arduino.flushInput()
just after opening the port.