-
This is probably worth pointing out in a Q&A. Originally discussed in #24. Make sure your tasks always return //////////////////////////////////////////////////////
// EXAMPLE FOR TIMER TASKS:
// the function declares it returns 'bool'
// the function actually returns a bool (returns true)
//
bool do_my_thing(void *) {
// your code here...
return true; // do it again too
} The compiler may not notice if there is a problem. I have run into Arduino configurations (ATmega328P) which did not detect "this function does not return a value but it should have". The following sketch compiles and downloads for ATmega328P, even though there are problems with it: /*
Demonstrate compiler warnings that are easy to miss.
Three different tasks, two with bugs.
Based on arduino-timer/blink.
philj404 14feb2021
*/
#include <arduino-timer.h>
auto timer = timer_create_default(); // create a timer with default settings
//////////////////////////////////////////////////////
// CORRECTLY CONFIGURED FOR TIMER TASKS:
// the function declares it returns 'bool'
// the function actually returns a bool (returns true)
//
bool toggle_led(void *) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
return true; // repeat? true
}
//////////////////////////////////////////////////////
// function does not declare a return value
//
void bug_toggle_returns_void(void *) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
return true; // the void declaration ignores this return value
}
//////////////////////////////////////////////////////
// function does not return a value
//
bool bug_toggle_forgot_to_return_bool(void *) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
//"return true;" missing; did you see a warning?
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
// call the toggle_led function about every second
timer.every(1000, toggle_led);
timer.every(1001, bug_toggle_returns_void);
timer.every( 999, bug_toggle_forgot_to_return_bool);
}
void loop() {
timer.tick(); // tick the timer
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Phil, this raises the question: if you've canceled an Jennifer |
Beta Was this translation helpful? Give feedback.
-
This tip is a suggested answer extracted from #24. |
Beta Was this translation helpful? Give feedback.
This tip is a suggested answer extracted from #24.