Created
July 11, 2012 11:55
-
-
Save jlesech/3089916 to your computer and use it in GitHub Desktop.
How to use assertions with Arduino.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define __ASSERT_USE_STDERR | |
#include <assert.h> | |
// Pin 13 has an LED connected on most Arduino boards. | |
// give it a name: | |
int led = 13; | |
// the setup routine runs once when you press reset: | |
void setup() { | |
// initialize the digital pin as an output. | |
pinMode(led, OUTPUT); | |
// initialize serial communication at 9600 bits per second. | |
Serial.begin(9600); | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
for (uint8_t i = 0; i < 3; i++) { | |
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) | |
delay(1000); // wait for a second | |
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW | |
delay(1000); // wait for a second | |
} | |
// make assertion failed. | |
assert(false); | |
} | |
// handle diagnostic informations given by assertion and abort program execution: | |
void __assert(const char *__func, const char *__file, int __lineno, const char *__sexp) { | |
// transmit diagnostic informations through serial link. | |
Serial.println(__func); | |
Serial.println(__file); | |
Serial.println(__lineno, DEC); | |
Serial.println(__sexp); | |
Serial.flush(); | |
// abort program execution. | |
abort(); | |
} |
I know this post is many years old, but since it still comes up widely via google search, I figured I'd point out a gotcha that tripped me up at first: This only works if Serial.begin
has been called - if you attempt an assert
prior to that, it doesn't work. This makes it somewhat less useful as a general purpose assert function.
Unfortunately, it doesn't work on Arduino DUE : __assert() function isn't called and I get the standard error message instead :
assertion "false" failed: file "/assert.ino", line 31, function: void loop() Exiting with status 1.
Do you have any idea why it doesn't work on Arduino DUE ? (However I successfully tested it on Arduino MEGA by the way)
Try to use this code. Presume it will help.
#include <assert.h>
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
// initialize serial communication at 9600 bits per second.
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
for (uint8_t i = 0; i < 3; i++) {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
// make assertion failed.
assert(false);
}
// handle diagnostic informations given by assertion and abort program execution:
void __assert_func(const char *__file, int __lineno, const char *__func, const char *__sexp) {
// transmit diagnostic informations through serial link.
Serial.println(__func);
Serial.println(__file);
Serial.println(__lineno, DEC);
Serial.println(__sexp);
Serial.flush();
// abort program execution.
abort();
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately, it doesn't work on Arduino DUE :
__assert() function isn't called and I get the standard error message instead :
Do you have any idea why it doesn't work on Arduino DUE ?
(However I successfully tested it on Arduino MEGA by the way)