If you want to learn about FreeRTOS and programming in a multi-tasking environment, do yourself a favor and get an ESP32. They have FreeRTOS built in. So you can concentrate on learning to use the OS rather than fighting installation issues for a reduced-feature version that's cobbled together to run on a low-resource processor.
I think the problem is that you are putting code in loop(). With FreeRTOS it appears loop() should be empty, and all code should run in a task. Look at the examples.
ETA: I tried the original sketch, and it produced corrupted output.
Here is the sketch converted to a "FreeRTOS style" :
#include <Arduino_FreeRTOS.h>
int count = 0;
void myLoop ( void *pvParameters );
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
Serial.write("\nStarted\n");
count = 0;
xTaskCreate(
myLoop
, "loop" // A name just for humans
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL );
}
void myLoop (void *pvParameters) // This is a task.
{
(void) pvParameters;
while (true) // a task shoud not return
{
Serial.write("Loop '");
Serial.print((int)(++count));
Serial.write("'\n");
Serial.write("**********************************\n");
Serial.write("**********************************\n");
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
}
}
void loop() {
// nothing
}
I tested on an Uno, it produces the expected output.
I'll hijack that question because I'm interested in the answer
Without FreeRTOS:
Sketch uses 1972 bytes (6%) of program storage space. Maximum is 32256 bytes.
Global variables use 242 bytes (11%) of dynamic memory, leaving 1806 bytes for local variables. Maximum is 2048 bytes.
With FreeRTOS (my version) :
Sketch uses 8142 bytes (25%) of program storage space. Maximum is 32256 bytes.
Global variables use 405 bytes (19%) of dynamic memory, leaving 1643 bytes for local variables. Maximum is 2048 bytes.
It's a significant overhead. I've found that adding more tasks, mutexes etc quickly chews up memory. I guess as long as you are aware of the overhead, and are not going to be building a complex sketch, then FreeRTOS provides a nice abstraction. Knowledge gained could be applied to more platforms platforms such as ESP32.
Sketch uses 9728 bytes (30%) of program storage space. Maximum is 32256 bytes.
Global variables use 399 bytes (19%) of dynamic memory, leaving 1649 bytes for local variables. Maximum is 2048 bytes.
I didn't really look into what goes on under the hood, apart from seeing that FreeRTOS uses loop() as the idle task, and generally you should not put code there "unless you know what you are doing".
And you don't have to, I think it's clear that a library can maneuver (you know what I mean) it's way into the code by #include and yet no calls. Perhaps libraries are checked first for the presence of a function?
I disagree with the first part, but I think I see what you are getting at. FreeRTOS overrides Arduino startup code to inject it's own scheduler. That will then invoke a lot of functions for task management etc. So even if you don't call any RTOS functions, there is a lot of code that will not be stripped by the linker as "unused".
The memory use reports from the compiler aren't the full story. Each FreeRTOS task is assigned its own stack space. That RAM is allocated dynamically at run time by the xTaskCreate() function. The compiler has no idea about it.
You'r right, it's fixing my issue. Thanks a lot!
Still strange, as it was working in previous version, even find samples of FreeROTS around the internet using code in loop. But whatever, I have a solution, thanks!!