3
\$\begingroup\$

When executing the following code on my Arduino Micro, only the first line is printed:

void crash(void) {
    abort();
}

void setup(void) {
    Serial.begin(9600);

    while(!Serial.available()) {
        Serial.println("Please input something to begin");
        delay(500);
    }

    Serial.println("First"); // Gets printed
    delay(500); // Make sure Arduino is done writing to the serial connection

    Serial.println("Second"); // Doesn't get printed
    crash(); // Makes the Arduino device crash
}

void loop(void) {

}

I would like to something like this to make the second line get printed, before the device crashes:

void setup(void) {
    Serial.begin(9600);

    while(!Serial.available()) {
        Serial.println("Please input something to begin");
        delay(500);
    }

    Serial.println("First"); // Gets printed
    delay(500); // Make sure Arduino is done writing to the serial connection

    Serial.println("Second"); // Doesn't get printed

    wait_for_serial_write(); // Not really a function

    crash(); // Makes the Arduino device crash
}

void loop(void) {

}

Which function can I use instead of wait_for_serial_write?

The reason I want to do this is because my Arduino crashes "randomly" so I have put some debug Serial.println statements, that I would like to see before the device crashes.

I also cannot use delay in my real code as the crash occurs while in a timer interrupt, where delay doesn't work.

\$\endgroup\$
  • \$\begingroup\$ I'm curious, what does crash do exactly? \$\endgroup\$ – Kevin Mark Apr 18 '13 at 19:20
  • \$\begingroup\$ @KevinMark Calls abort function which "Aborts the current process, producing an abnormal program termination." I don't know how it is implemented, but no code is run either in the timer or in the loop function. The Arduino doesn't reset either. \$\endgroup\$ – Tyilo Apr 18 '13 at 19:23
7
\$\begingroup\$

Serial.flush() is exactly what you're looking for.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.