Skip to content

Commit 0bec759

Browse files
committed
Modified BasicMultiThreading example
1 parent f4dbaf2 commit 0bec759

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

libraries/MultiThreading/examples/BasicMultiThreading/BasicMultiThreading.ino

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#define ARDUINO_RUNNING_CORE 1
1313
#endif
1414

15-
#define ANALOG_INPUT_PIN 27
15+
#define ANALOG_INPUT_PIN A0
1616

1717
#ifndef LED_BUILTIN
1818
#define LED_BUILTIN 13 // Specify the on which is your LED
@@ -21,19 +21,18 @@
2121
// Define two tasks for Blink & AnalogRead.
2222
void TaskBlink( void *pvParameters );
2323
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.
2525

2626
// The setup function runs once when you press reset or power on the board.
2727
void setup() {
2828
// Initialize serial communication at 115200 bits per second:
2929
Serial.begin(115200);
30-
3130
// Set up two tasks to run independently.
3231
uint32_t blink_delay = 1000; // Delay between changing state on LED pin
3332
xTaskCreate(
3433
TaskBlink
3534
, "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);`
3736
, (void*) &blink_delay // Task parameter which can modify the task behavior. This must be passed as pointer to void.
3837
, 2 // Priority
3938
, NULL // Task handle is not used here - simply pass NULL
@@ -43,21 +42,22 @@ void setup() {
4342
xTaskCreatePinnedToCore(
4443
TaskAnalogRead
4544
, "Analog Read"
46-
, 1024 // Stack size
45+
, 2048 // Stack size
4746
, NULL // When no parameter is used, simply pass NULL
4847
, 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.
5049
, ARDUINO_RUNNING_CORE // Core on which the task will run
5150
);
5251

52+
Serial.printf("Basic Multi Threading Arduino Example\n");
5353
// Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
5454
}
5555

5656
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
5858
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
6161
}
6262
}
6363

@@ -91,6 +91,12 @@ void TaskBlink(void *pvParameters){ // This is a task.
9191

9292
void TaskAnalogRead(void *pvParameters){ // This is a task.
9393
(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+
}
94100

95101
/*
96102
AnalogReadSerial

0 commit comments

Comments
 (0)