]> git.piffa.net Git - sketchbook_andrea/blob - multitasking/blink_10_freeRTOS/blink_10_freeRTOS.ino
RTOS
[sketchbook_andrea] / multitasking / blink_10_freeRTOS / blink_10_freeRTOS.ino
1 #include <Arduino_FreeRTOS.h>
2 /* E' necessario installare la libreria: Arduino_FreeRTOS
3 - https://github.com/feilipu/Arduino_FreeRTOS_Library
4 - https://www.hackster.io/feilipu/using-freertos-multi-tasking-in-arduino-ebc3cc
5 */
6 // define two tasks for Blink & AnalogRead
7 void TaskFirstBlink( void *pvParameters );
8 void TaskSecondBlink( void *pvParameters );
9
10 // the setup function runs once when you press reset or power the board
11 void setup() {
12   
13   // initialize serial communication at 9600 bits per second:
14   Serial.begin(9600);
15   
16   while (!Serial) {
17     ; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
18   }
19
20   // Now set up two tasks to run independently.
21   xTaskCreate(
22     TaskFirstBlink
23     ,  (const portCHAR *)"First Blink"   // A name just for humans
24     ,  128  // This stack size can be checked & adjusted by reading the Stack Highwater
25     ,  NULL
26     ,  2  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
27     ,  NULL );
28
29   xTaskCreate(
30     TaskSecondBlink
31     ,  (const portCHAR *) "Sec Blink"
32     ,  128  // Stack size
33     ,  NULL
34     ,  1  // Priority
35     ,  NULL );
36
37   // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
38 }
39
40 void loop()
41 {
42   // Empty. Things are done in Tasks.
43 }
44
45 /*--------------------------------------------------*/
46 /*---------------------- Tasks ---------------------*/
47 /*--------------------------------------------------*/
48
49 void TaskFirstBlink(void *pvParameters)  // This is a task.
50 {
51   (void) pvParameters;
52
53 /*
54   Blink
55   Turns on an LED on for one second, then off for one second, repeatedly.
56
57   Most Arduinos have an on-board LED you can control. On the UNO, LEONARDO, MEGA, and ZERO 
58   it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN takes care 
59   of use the correct LED pin whatever is the board used.
60   
61   The MICRO does not have a LED_BUILTIN available. For the MICRO board please substitute
62   the LED_BUILTIN definition with either LED_BUILTIN_RX or LED_BUILTIN_TX.
63   e.g. pinMode(LED_BUILTIN_RX, OUTPUT); etc.
64   
65   If you want to know what pin the on-board LED is connected to on your Arduino model, check
66   the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products
67   
68   This example code is in the public domain.
69
70   modified 8 May 2014
71   by Scott Fitzgerald
72   
73   modified 2 Sep 2016
74   by Arturo Guadalupi
75 */
76
77   // initialize digital LED_BUILTIN on pin 13 as an output.
78   pinMode(LED_BUILTIN, OUTPUT);
79 const int pausa = 500;
80
81   for (;;) // A Task shall never return or exit.
82   {
83     digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
84     vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second
85     digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
86     vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second
87   }
88 }
89
90 void TaskSecondBlink(void *pvParameters)  // This is a task.
91 {
92   (void) pvParameters;
93   
94 pinMode(3, OUTPUT);
95 const int pausa = 1000;
96   for (;;) // A Task shall never return or exit.
97   {
98     digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
99     vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second
100     digitalWrite(3, LOW);    // turn the LED off by making the voltage LOW
101     vTaskDelay( pausa / portTICK_PERIOD_MS ); // wait for one second
102   }
103 }