How to clear the serial data? (Arduino + TVout)

Hello!
Could you guys help me? I'm working on a project where I use three arduinos. I'm making a videogame with a wireless controller, which sends a certain number to an Arduino Uno via the RF module, according to the pressed button. The Arduino Uno that receives this number sends it again to an Arduino Mega, by Serial Communication (TX and RX pins). The problem starts here: I'm using the TVout library in the Arduino Mega to make a videogame, and it reads the data received to execute certain commands. But as I press the buttons the data is stored in the buffer (I think), and my program read ALL this data, not only the last one.

I'll give an example: If I press the Up button 10 times in a row, the program will execute the functions associated with the Up button then time (Like making the character walking up), but this is undesirable. I want to read only the last command, i.e the last char in the serial buffer. Do you have any advice to solve this problem?

I'm new in this forum and Enghish is not my native language, so sorry if I made any mistakes! And thank you very much for your time and help!!!

I'll give an example: If I press the Up button 10 times in a row, the program will execute the functions associated with the Up button then time (Like making the character walking up), but this is undesirable. I want to read only the last command, i.e the last char in the serial buffer. Do you have any advice to solve this problem?

I have a much simpler idea. Only send the data that you ACTUALLY want to deal with.

I want to read only the last command, i.e the last char in the serial buffer. Do you have any advice to solve this problem?

So how is the arduino to know what is the last button push/command?

Gaba:
the last char in the serial buffer. Do you have any advice to solve this problem?

If you literally want to do that you could try something like this

numChars = Serial.available();
for (byte n = 0; n < numChars-1; n++) {
  Serial.read();  // discards characters
}
lastChar = Serial.read();

But I doubt if it would be a robust way to deal with the problem. Much better to use a delimiter to mark the end of a message, read all the characters up to the delimiter and then analyze the received data to do what you want with it.

That is how the examples in serial input basics work.

…R