1

I'm new to Arduino but I'm an experienced programmer. I'm wondering if there are any common functions for reporting errors in Arduino programs? For example, in JavaScript we have the alert() function, or on Unix you can write a message to standard error and exit.

Ideally, if the serial port is available then an error message would be printed there, or if there is an LED then maybe we should blink it. This would need to be configurable based on what the programmer wants to do with available hardware.

Looking the example sketches, error reporting seems pretty horrible. I'm wondering if any libraries handle this better?

  • How you report an error depends entirely on what communication channels you have available and what kind of error reporting is appropriate for your design. There can be no "standard" since every design is different. Welcome to the embedded world. When you have no OS and no screen and keyboard you have to make do with what resources you have. – Majenko Sep 17 at 23:54
  • 1
    I myself have resorted to blinking an LED with morse code to indicate different error conditions. – Majenko Sep 17 at 23:55
  • I don't see any reason why there couldn't be a standard macro call that libraries use to set an error condition. (The default implementation could be a no-op.) I guess it doesn't exist, though? – Brian Slesinsky Sep 19 at 1:13
1

As pointed out by Majenko in a comment, you can hardly define a standard facility for error reporting when you don't know what kind of I/O hardware will be available on any particular project. Printing to the serial port is the most common method though, as it tends to always be available during the development phase, while the Arduino is tethered to the development computer. I personally tend to just do something like

Serial.println(F("Error: the warp field coil is busted."));

and be consistent with the error prefix, so that a program receiving this can tell it's an error by matching the line against /^Error: /.

While it is OK for user code, reporting errors through Serial is certainly not a good practice for library code. Alas, it is done way too often in Arduino libraries. I've seen at least one library (don't remember which one) that allows the user to provide a reference to a Print object (defaulting to Serial) to be used for this purpose. This is way better, as it allows the user to handle the error messages any way she chooses. It is also very easy to turn off the error reporting altogether by implementing a do-nothing Print, which would be a /dev/null of sorts.

Note that the avr-libc allows you to print to stderr, as long as you have first implemented stderr->put... While this method is the closest you can get to the standards you are used to, it may not be portable to non-AVR Arduinos. And it is worth noting that we tend to avoid the printf() family of functions on Arduinos, as they tend to be quite large compared to the print() and println() methods.

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.