I have two ARDUINOs. One reading a linear transducer (0 to 5v) to measure distance.
The other is reading a force transducer (0 to 5v) to measure the force.
The transducers are connected and actuated together so that I can determine the force generated at a particular distance. I then generate a Force vs Distance graph.
I am reading the distance port in a loop. When it has traveled a known amount, say 1 inch, which corresponds to an increase in the output of 20, I then want the value of the force at the other port. If I then read it, I will be reading "old" data that has been stacked up in the buffer. I want the value that corresponds to the position read from the distance transducer.
Well, the answer to that is simple. Don't send anything out the Serial port unless it's the data you want. You read the distance sensor and the force sensor, store the values, and check to see if the distance has increased to 1 inch. If it has, send the values at that time. There's no sense in sending in data that you aren't interested in.
Because of this I put the Force read in the same loop as the Distance so they were both constantly reading and updating at the same time. Then when the value of the distance changed by at least 20 I jump out to the plotting routine. This seems to help. I still get erroneous small values as I believe I am reading partial words.
If you have the values you are interested in, just send them with something like:
Serial.print ("\n"); // or "\r" or "\r\n"
I still do not understand how to read the buffer until I get to a LF or CR.
To read the port I use a command: t$=input$(#comm,10)
input$() should input an entire line, though I don't know if it requires a CR, LF, or both. See the snippet above for how to send them.