Reset/turn of buffer on Arduino with Python

Hi, I am working on a project where I have a rotary encoder connected to an Arduino Mega board which in turn is connected to my computer via Python. I am using the rotary encoder to measure the to move a virtual object in in the python code up and down.

The idea here is that the object is moved to a certain position on the screen and then something happens (tone is played). So far I got this to work, however there are some issues. I read the input of the rotary encoder with serial.readline() which has to be stopped once the object is in the correct position and the tone is played and the object position is reset. If I keep turning the rotary encoder however, this causes a problem because once the new 'trial' begins, all the information form the turning during the tone playing is read out of the buffer of the Arduino and moves the object on the screen. This messes things up as the buffer controls the object for a little while and not the actual input of the rotary encoder.

So far I have achieved this by closing the serial port after each trial and opening it again, which works, but introduces a delay of 1+ second as it is the time it takes to re-open the serial port. A delay which is much too long between trials.

I therefore wanted to know if there is an option of maybe turning of the buffer capacity of Arduino or something that can reset the buffer of the Arduino at the start of each trial with a python command in order to get rid of the delay and the buffer issue. Or any other idea how I could handle this issue.

Thanks in advance !

I read the input of the rotary encoder with serial.readline() which has to be stopped once the object is in the correct position

How is the Arduino supposed to know that some external application no longer wants it sending serial data? That is nonsense. It is up to the external application to read, but not use, the serial data. Or, to send something to the Arduino to tell it to stop driving whatever is making the rotary encoder change.

How is the Arduino supposed to know that some external application no longer wants it sending serial data?

Assuming, that with 'external application' you refer to the python code: That's why I close and start the serial again. To let the Arduino know I don't need the information you need anymore. The issue here is that this method works but has the drawback of having a delay.

The rotary encoder is driven by a mouse (animal), therefore it is very difficult to tell it to stop moving the rotary encoder. That is the reason I thought there was a way to tell the Arduino via python to stop reading the rotary encoder at the end of the trial and then start doing it again the the beginning of a new trial.

As far as I understand you however, this is not possible and I have to find a way to NOT USE the data that is transmitted from the Arduino. did I get this right ?

That is the reason I thought there was a way to tell the Arduino via python to stop reading the rotary encoder at the end of the trial and then start doing it again the the beginning of a new trial.

What would make more sense is to tell the Arduino to send/not send data, rather than to read/not read the encoder.

As far as I understand you however, this is not possible and I have to find a way to NOT USE the data that is transmitted from the Arduino. did I get this right ?

While that is what I said, you can make the Pi send data to the Arduino telling it to send/not send data, or telling it to read/not read the encoder.

'turn off the buffer capacity" doesn't make sense. Resetting the outgoing buffer doesn't make sense, either, if you don't stop writing to the buffer.

Getting the PC to cause the Arduino to reset by closing and opening the serial port is a very crude kludge.

There are many alternative ways to manage the flow of data.

You could get the PC to send a message (perhaps just 'M' for more) every time it is ready for more data for a movement.

Or you could get the Arduino to include a serial number in each message and when the PC has enough it could remember the last serial number it used. Then when it is ready for more data for another move it could send a message to the Arduino to tell the Arduino it is ready. Then the Arduino could set the serial numbering back to zero and the PC would know to discard all existing messages with serial numbers higher than zero.

And I'm sure there are 100 other options.

...R