Serial.availableForWrite versus Serial.flush
The line

while (Serial.availableForWrite() &lt;= 0) {}


waits until the serial output buffer is not full. Whereas

Serial.flush();


waits until the serial output buffer is empty.