I don’t think I will be able to pin point your issue. I will suggest by starting with the following revision of your code:
void serialEvent (Serial port)
{
data_str = port.readStringUntil('\n');
String[] tokens = split(data_str,',');
lane1_on =tokens[0];
delay_light=float(tokens[1]);
//SEE note 3
stuff = loadStrings("data.txt");
data_arr = int(split(stuff[0], ','));
port.write(data_arr[0]);
port.write(data_arr[1]);
}
Note:
- Serial Event fires when there is some data. No need to check if there is data available in the port. Check the reference: Serial Event
- Use split() as it makes your code more readable
- It is not clear what you are doing here by loading the file in this function. Can you share the content of this file, or a snipet to show its structure? I think you are better to load the file only once in setup and then you manage the data here in the serial event. There is no information about the logic of your program, so it will help if you briefly exaplain what these few lines suppose to do.
Finally, it is not recommend it to have a delay() inside the draw() function. You can slow down your sketch using frameRate() or control the execution of each frame using noLoop()/redraw() functions. You can call 'redraw()inside theserialEvent()` if that suits your use case.
Kf