-
-
Save jlesech/3089916 to your computer and use it in GitHub Desktop.
#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(); | |
} |
Many thanks for sharing! It works!
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)
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();
}
Thanks for documenting this. I was able to put it to use today.
Plus 1 star!