How to check if a function completed successfully or not

The function "Initialize_Serial_Monitor" uses the while (!Serial) technique to determine if the serial monitor completed successfully or not.
I'm wondering how I can tell if all my commands in the function "Initialize_LEDs" have been completed successfully or not.

void Initialize_Serial_Monitor()
  Serial.begin(115200);  //Set the baud rate
  Initialize_Serial_Monitor == true;  //Set the flag to true
  while (!Serial)  //If the serial monitor fails to initialize
    Initialize_Serial_Monitor == false;  //Set the flag to false

void Initialize_LEDs()
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  analogWrite(RED, 0);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0);

AFAIK to verify a procedure was successful requires the device in question to provide a response. These are usually external devices.

This code is if Serial initialization fails, the function will not end.
If it happened so Arduino hangs. :expressionless:

And == is wrong operator in this case.

Either way, the your desired function is not performed.

1 Like

The statements in Initialize_LEDs() will never fail so no need to check.

while (!Serial) checks if a terminal program has established a connection to the Arduino. It's only not useful on Arduinos that don't have native USB support (like the 328P and 2560 based boards).


Why would the statements inside the Initialize_LEDs() function never fail?

Also @sterretje I remember you helping me out a while ago on here. Keep it up, it's definitely appreciated!

Good point, Thanks!
Sorry, I missed it. Works on some boards.

Always True for boards that don't have USB-CDC.

1 Like

But looks like the other way around at above codes?

OOPS, will correct.

Because these are setting the internal working of the processor. If these did fail then this would be because of a faulty or damaged processor and there is no way that code could spot that.

It is only external devices like OLED displays, SD cards or some sorts of sensors that can respond when initialises that you can check if it is working.


Thank you, sir.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.