0

What is the "proper" way to handle an error during setup on an Arduino -- for example some attached component is missing or returning an error code or invalid response? The setup() function returns void so that's no help. This just doesn't seem to be an issue that is addressed in the official docs -- at least not that I have found so far.

Here are some options I have considered:

  • Throw an exception (but where does that go?)
  • Write a message to Serial and then go into an infinite loop or wait for a y/n to continue.
  • Set a setup_successful flag and then do if (!setup_successful) return; in the main loop()
2

The setup() function returns void so that's no help.

You wouldn't be able to get the return value anyway even if it did return something - it's not you calling the setup() function, but main(), which is not in your code or control.

This just doesn't seem to be an issue that is addressed in the official docs

That is because it is not within the remit of the API and its associated documentation.

Throw an exception (but where does that go?)

It goes in the bin. Exceptions are disabled. They make little sense in an embedded system. Yes, it is possible to use them, but not worth it.

Write a message to Serial and then go into an infinite loop or wait for a y/n to continue.

That kind of thing is what most people do - or variations on that kind of theme.

Set a setup_successful flag and then do if (!setup_successful) return; in the main loop()

That will just run loop() again - after all, it's called loop() for a reason.

You seem to be in the mindset of a PC programmer. Embedded programming is a very very different beast. You don't (usually) have an operating system to handle exceptions and errors and things like that. You don't have a keyboard and mouse, and a screen with pretty pictures on it. Every single thing that the board does is what you program. If you want it to raise an error when an error occurs then it is up to you to program a suitable mechanism for that to happen. Reporting that error and asking for feedback in some way is entirely governed by your system's design. If you have a TFT screen you could put an error message on that. If you have a serial connection you can use that to show error messages. If you don't have any of those you can blink an LED or use a buzzer to beep in a specific pattern (like a PC's POST beeps).

  • It's true, I have been an application developer (mostly unix/linux) for over 30 years so my thinking is more along those lines and since Arduino and others have made access to embedded development so accessible, there are probably a lot of people like me that art starting to cross over and blur the lines. – ThatAintWorking Apr 28 '16 at 16:40
2

Same way you would handle any error in an embedded system; either try to recover from it if you can, or use whatever communications mechanism you have to let the user know that something failed.

I normally use a combination of Serial.println in case I happen to have a serial connection at the time, and also a blinking pattern on an LED (which most boards I use have). As an example, look at my code here: https://github.com/jayeye/jardino/blob/master/libard/WiFiSPIFFSConfig/src/WiFiSPIFFSConfig.cpp#L11. error() will do a sequence of slow and then fast blinks, and either return if I think the error is recoverable and I'm merely warning the user, or just stay in an infinite loop and keep blinking in despair :)

Obviously this is not an original idea -- think beep codes!

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.