How To Perform Parallel Tasks (Threads) In An Arduino Program

Table of contents:

How To Perform Parallel Tasks (Threads) In An Arduino Program
How To Perform Parallel Tasks (Threads) In An Arduino Program

Video: How To Perform Parallel Tasks (Threads) In An Arduino Program

Video: How To Perform Parallel Tasks (Threads) In An Arduino Program
Video: How to do multiple tasks in Arduino | Beginners | millis() function 2024, December
Anonim

In microprocessor technology, tasks running in parallel are called Threads. This is very convenient, because it is often necessary to perform several operations at the same time. Is it possible to make the Arduino microcontroller perform several tasks at once, like a real processor? Let's see.

Parallel streams in an Arduino program
Parallel streams in an Arduino program

It is necessary

  • - Arduino;
  • - 1 LED;
  • - 1 piezo buzzer.

Instructions

Step 1

Generally speaking, Arduino does not support true parallelization, or multithreading.

But you can tell the microcontroller to check if the time has come to execute some additional, background task at each repetition of the "loop ()" cycle. In this case, it will seem to the user that several tasks are being performed simultaneously.

For example, let's blink an LED at a given frequency and, in parallel, emit sounds that rise and fall like a siren from a piezoelectric emitter.

We have connected both the LED and the piezo emitter to the Arduino more than once. Let's assemble the circuit as shown in the figure. If you are connecting an LED to a digital pin other than "13", remember to have a current limiting resistor of about 220 ohms.

Wiring diagram for buzzer and LED to Arduino
Wiring diagram for buzzer and LED to Arduino

Step 2

Let's write a sketch like this and upload it to Arduino.

After loading the board, you can see that the sketch is not executed exactly as we need it to: until the siren is fully operational, the LED will not blink, and we would like the LED to blink DURING the siren sounding. What is the problem here?

The fact is that this problem cannot be solved in the usual way. The tasks are performed by the microcontroller strictly sequentially. The "delay ()" operator delays the execution of the program for the specified period of time, and until this time expires, the following program commands will not be executed. Because of this, we cannot set a different execution time for each task in the "loop ()" of the program.

Therefore, you need to somehow simulate multitasking.

Beeper and LED control in series
Beeper and LED control in series

Step 3

The option in which the Arduino will perform tasks in pseudo-parallel is suggested by the Arduino developers in the article

The essence of the method is that with each repetition of the "loop ()" loop, we check whether it is time to blink the LED (to perform a background task) or not. And if it does, then we invert the state of the LED. This is a kind of bypassing the "delay ()" operator.

A significant disadvantage of this method is that the code section in front of the LED control unit must be executed faster than the blinking time interval of the "ledInterval" LED. Otherwise, the blinking will occur less frequently than necessary, and we will not get the effect of parallel execution of tasks. In particular, in our sketch, the duration of the siren sound change is 200 + 200 + 200 + 200 = 800 msec, and we set the LED blinking interval to 200 msec. But the LED will flash with a period of 800 msec, which is 4 times different from what we set. In general, if the "delay ()" operator is used in the code, then it is difficult to simulate pseudo-parallelism, so it is advisable to avoid it.

In this case, it would be necessary for the siren sound control unit to also check whether the time has come or not, and not to use "delay ()". But this would increase the amount of code and worsen the readability of the program.

LED blinking without delay () operator
LED blinking without delay () operator

Step 4

To solve this problem, we will use the wonderful ArduinoThread library, which allows you to easily create pseudo-parallel processes. It works in a similar way, but it allows you not to write code to check the time - whether you need to perform the task in this loop or not. This reduces the amount of code and improves the readability of the sketch. Let's check out the library in action.

First of all, download the library archive from the official site https://github.com/ivanseidel/ArduinoThread/archive/master.zip and unzip it into the "libraries" directory of the Arduino IDE. Then rename the "ArduinoThread-master" folder to "ArduinoThread".

Installing the ArduinoThread library
Installing the ArduinoThread library

Step 5

The connection diagram will remain the same. Only the program code will change. Now it will be the same as in the sidebar.

In the program, we create two streams, each performs its own operation: one blinks with an LED, the second controls the sound of the siren. In each iteration of the loop, for each thread, we check whether the time has come for its execution or not. If it arrives, it is launched for execution using the "run ()" method. The main thing is not to use the "delay ()" operator.

More detailed explanations are given in the code.

Let's load the code into the Arduino memory, run it. Now everything works exactly as it should!

Recommended: