12
12
#define ARDUINO_RUNNING_CORE 1
13
13
#endif
14
14
15
- #define ANALOG_INPUT_PIN 27
15
+ #define ANALOG_INPUT_PIN A0
16
16
17
17
#ifndef LED_BUILTIN
18
18
#define LED_BUILTIN 13 // Specify the on which is your LED
21
21
// Define two tasks for Blink & AnalogRead.
22
22
void TaskBlink ( void *pvParameters );
23
23
void TaskAnalogRead ( void *pvParameters );
24
- TaskHandle_t task_handle ; // You can (don't have to) use this to be able to manipulate a task from somewhere else.
24
+ TaskHandle_t analog_read_task_handle ; // You can (don't have to) use this to be able to manipulate a task from somewhere else.
25
25
26
26
// The setup function runs once when you press reset or power on the board.
27
27
void setup () {
28
28
// Initialize serial communication at 115200 bits per second:
29
29
Serial.begin (115200 );
30
-
31
30
// Set up two tasks to run independently.
32
31
uint32_t blink_delay = 1000 ; // Delay between changing state on LED pin
33
32
xTaskCreate (
34
33
TaskBlink
35
34
, " Task Blink" // A name just for humans
36
- , 1024 // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
35
+ , 2048 // The stack size can be checked by calling `uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);`
37
36
, (void *) &blink_delay // Task parameter which can modify the task behavior. This must be passed as pointer to void.
38
37
, 2 // Priority
39
38
, NULL // Task handle is not used here - simply pass NULL
@@ -43,21 +42,22 @@ void setup() {
43
42
xTaskCreatePinnedToCore (
44
43
TaskAnalogRead
45
44
, " Analog Read"
46
- , 1024 // Stack size
45
+ , 2048 // Stack size
47
46
, NULL // When no parameter is used, simply pass NULL
48
47
, 1 // Priority
49
- , &task_handle // With task handle we will be able to manipulate with this task.
48
+ , &analog_read_task_handle // With task handle we will be able to manipulate with this task.
50
49
, ARDUINO_RUNNING_CORE // Core on which the task will run
51
50
);
52
51
52
+ Serial.printf (" Basic Multi Threading Arduino Example\n " );
53
53
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
54
54
}
55
55
56
56
void loop (){
57
- if (task_handle != NULL ){ // Make sure that the task actually exists
57
+ if (analog_read_task_handle != NULL ){ // Make sure that the task actually exists
58
58
delay (10000 );
59
- vTaskDelete (task_handle ); // Delete task
60
- task_handle = NULL ; // prevent calling vTaskDelete on non-existing task
59
+ vTaskDelete (analog_read_task_handle ); // Delete task
60
+ analog_read_task_handle = NULL ; // prevent calling vTaskDelete on non-existing task
61
61
}
62
62
}
63
63
@@ -91,6 +91,12 @@ void TaskBlink(void *pvParameters){ // This is a task.
91
91
92
92
void TaskAnalogRead (void *pvParameters){ // This is a task.
93
93
(void ) pvParameters;
94
+ // Check if the given analog pin is usable - if not - delete this task
95
+ if (!adcAttachPin (ANALOG_INPUT_PIN)){
96
+ Serial.printf (" TaskAnalogRead cannot work because the given pin %d cannot be used for ADC - the task will delete itself.\n " , ANALOG_INPUT_PIN);
97
+ analog_read_task_handle = NULL ; // Prevent calling vTaskDelete on non-existing task
98
+ vTaskDelete (NULL ); // Delete this task
99
+ }
94
100
95
101
/*
96
102
AnalogReadSerial
0 commit comments