1

So I have been trying to write a code where I use the millis() function to print the milliseconds of on-time of the board using Serial. But the condition is I want to stop printing the value once it reaches 10 seconds (10000ms).

I have been trying to use the exit() function but I get no output then.

#include<stdlib.h>

void setup()
{

  Serial.begin(9600);          //  setup serial


}

void loop()

{

unsigned long timer1;

while(timer1%10000!=0)

{

timer1=millis();

Serial.println(timer1);

}

    exit(1);

}
3
  • What do you imagine exit() is supposed to do in an embedded system? Commented Dec 15, 2015 at 19:08
  • 3
    exit() is probably a bad idea in a bare-metal embedded system unless you know for a fact how the environment will respond. Consider either an infinite while(1); loop explicit in your own code, or shutting down the processor. Make sure to wait until the serial output buffer is empty before shutting down or disabling interrupts, or you may see your messages truncated. Commented Dec 15, 2015 at 19:08
  • Where do you suppose exit() would exit to in a microcontroller? Your desk?
    – Majenko
    Commented Dec 16, 2015 at 0:18

1 Answer 1

2

The AVR library documentation for exit() is great help. It explains that the interrupts are turned off and an infinite loop is entered. As the interrupts get turned off the last characters in the Serial buffer are not transmitted.

You can use Serial.flush() to wait for the transmission to complete before calling exit().

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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