How to clean up Serial port buffer

PaulS:

serialEvent() is called whenever Serial.available() > 0.

No. The serialEvent() function is called at the end of loop(), if Serial.available() returns greater than 0.

Of course :stuck_out_tongue: Thanks for pointing that out :slight_smile:

#include <Arduino.h>

int main(void)
{
    init();

#if defined(USBCON)
    USBDevice.attach();
#endif

    setup();

    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }

    return 0;
}