Arduino to Arduino serial communication buffer fills too fast

(deleted)

antisocialdistancermultitasking.ino (17.8 KB)

socdistancer2nddev.ino (3.26 KB)

One way might be to implement a handshake.

In the receiver code, send a byte back to the sender once the receiver has done it's job based on the received character.

The receiver could look like

if (Serial.available() > 0){
  
  incomingByte = Serial.read();
  
  switch (incomingByte){ 
    case 1:
      voice.say(spO);
      voice.say(spK);
      // echo received character to tell sender that it can send the next character
      Serial.write(incomingByte);
      break;

The sender has to wait till it receives this byte before sending the next one.

Do those functions "voice.say()" take a long time ? Is there something that will start the sound and the function returns ?
You could set the baudrate to 115200, that is something.

The example that you took from the forum is really bad :frowning: There should not be any delay() in that and the for-loop with only 10 is also wrong.

With good code, there is not need to flush the serial buffer.
If you really need to do that, the code by Riva in that thread is correct:

void serialFlush()
{
  while(Serial.available() > 0) 
  {
    char t = Serial.read();
  }
}

or shortened to:

void serialFlush()
{
  while(Serial.available() > 0) 
    Serial.read();
}

When the text layout of the code is a big mess, then you will never be able to write good code.
Put every indent, every newline, every comma, every space at the right place.
In the Arduino IDE you can press Ctrl + T to autoformat the text.
I think you prefer an other style, that is possible:
In the setting at the bottom, see where the file "preferences.txt" is.
Click on it to open that folder.
Close the Arduino IDE.
Look for the file "formatter.conf". If it does not exist, make one.
Add or change the line with the format to: style=allman
Then close it, start the Arduino IDE and press Ctrl+T, then you still have some work to make it look better.