Help reading in serial strings into pi from arduino?
If the sensor input changes (e.g. from FTFF to FTTF) I want to change which image the pi displays. This works. But if the sensor input doesn't change in the time it takes to loop through my function, i just want it to keep the existing image displayed. They way it is working right now, it will close out the image and reopen it. I can't figure out where the issue is exactly. But I've tracked the issue down to the fact that it seems like each loop it seems to think that it didn't get a serial input (initially), so it enters the "close image" else clause before getting the serial input. I think this is related to having/not having a buffer for serial set up correctly, but I don't understand the serial read well enough. Any help would be much appreciated. Code below, let me know if I left any important details out. Thanks!
Code: Select all
def touchPic (sensor1, sensor2, sensor3, sensor4, sensorCase, picPath): import os import time import serial #check sensor values if sensor1 and sensor2 and sensor3 and sensor4: if sensorCase != 1: #only enter the image open function if status is different than previous status os.system("sudo pkill fbi") os.system("sudo fbi -a -T 2" + picPath + "testpic" + "all" + ".png") sensorCase = 1 elif sensor1 and sensor2 and sensor3 and not sensor4: if sensorCase != 2: os.system("sudo pkill fbi") os.system("sudo fbi -a -T 2" + picPath + "testpic" + "123" + ".png") sensorCase = 2 elif sensor1 and sensor2 and sensor4 and not sensor3: if sensorCase != 3: os.system("sudo pkill fbi") os.system("sudo fbi -a -T 2" + picPath + "testpic" + "124" + ".png") sensorCase = 3 #... continue for all cases... time.sleep(.3) picPath = " /home/pi/Pictures/" ser = serial.Serial('/dev/ttyUSB0', 9600) if (ser.in_waiting > 0): #read the serial data from arduino. Set cases based on T/F inputs line = str(ser.readline().strip()) sensor1status = line sensor2status = line sensor3status = line sensor4status = line if sensor1status == "T": sensor1 = True elif sensor1status == "F": sensor1 = False if sensor2status == "T": sensor2 = True elif sensor2status == "F": sensor2 = False if sensor3status == "T": sensor3 = True elif sensor3status == "F": sensor3 = False if sensor4status == "T": sensor4 = True elif sensor4status == "F": sensor4 = False if sensor1 or sensor2 or sensor3 or sensor4: (sensor1, sensor2, sensor3, sensor4) = escapeFunctions.touchPic(sensor1, sensor2, sensor3, sensor4, sensorCase,picPath) else: #if no serial data to read, then close out of the image, set all values to false, and exit function #code seems to run this every loop even though serial data should be coming in constantly... os.system("sudo pkill fbi") time.sleep(5) sensorCase = 0 sensor1 = False sensor2 = False sensor3 = False sensor4 = False return (sensor1, sensor2, sensor3, sensor4) #time.sleep(1) #os.system("sudo pkill fbi")