|
| 1 | +#include <Arduino_FreeRTOS.h> |
| 2 | + |
| 3 | +// define two tasks for Blink & AnalogRead |
| 4 | +void TaskBlink( void *pvParameters ); |
| 5 | +void TaskAnalogRead( void *pvParameters ); |
| 6 | + |
| 7 | +// the setup function runs once when you press reset or power the board |
| 8 | +void setup() { |
| 9 | + |
| 10 | + // Now set up two tasks to run independently. |
| 11 | + xTaskCreate( |
| 12 | + TaskBlink |
| 13 | + , (const portCHAR *)"Blink" // A name just for humans |
| 14 | + , 128 // This stack size can be checked & adjusted by reading the Stack Highwater |
| 15 | + , NULL |
| 16 | + , 2 // Priority, with 1 being the highest, and 4 being the lowest. |
| 17 | + , NULL ); |
| 18 | + |
| 19 | + xTaskCreate( |
| 20 | + TaskAnalogRead |
| 21 | + , (const portCHAR *) "AnalogRead" |
| 22 | + , 128 // Stack size |
| 23 | + , NULL |
| 24 | + , 1 // Priority |
| 25 | + , NULL ); |
| 26 | + |
| 27 | + // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started. |
| 28 | +} |
| 29 | + |
| 30 | +void loop() |
| 31 | +{ |
| 32 | + // Empty. Things are done in Tasks. |
| 33 | +} |
| 34 | + |
| 35 | +/*--------------------------------------------------*/ |
| 36 | +/*---------------------- Tasks ---------------------*/ |
| 37 | +/*--------------------------------------------------*/ |
| 38 | + |
| 39 | +void TaskBlink(void *pvParameters) // This is a task. |
| 40 | +{ |
| 41 | + (void) pvParameters; |
| 42 | + |
| 43 | + // initialize digital pin 13 as an output. |
| 44 | + pinMode(13, OUTPUT); |
| 45 | + |
| 46 | + for (;;) // A Task shall never return or exit. |
| 47 | + { |
| 48 | + digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) |
| 49 | + vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second |
| 50 | + digitalWrite(13, LOW); // turn the LED off by making the voltage LOW |
| 51 | + vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +void TaskAnalogRead(void *pvParameters) // This is a task. |
| 56 | +{ |
| 57 | + (void) pvParameters; |
| 58 | + |
| 59 | + // initialize serial communication at 9600 bits per second: |
| 60 | + Serial.begin(9600); |
| 61 | + |
| 62 | + for (;;) |
| 63 | + { |
| 64 | + // read the input on analog pin 0: |
| 65 | + int sensorValue = analogRead(A0); |
| 66 | + // print out the value you read: |
| 67 | + Serial.println(sensorValue); |
| 68 | + vTaskDelay(1); // one tick delay (30ms) in between reads for stability |
| 69 | + } |
| 70 | +} |
0 commit comments