You will need
  • - Arduino;
  • - 1 led;
  • 1 plesiopidae.
Instruction
1
Generally speaking, an Arduino does not support a real parallelization of tasks or multipotent.
But with each repeat of the cycle "loop()" to specify the microcontroller to check whether there has come time to perform some additional background task. In this case the user would seem that several tasks are performed simultaneously.
For example, let's flash led with a specified frequency and in parallel to this issue of growing and subsiding like the siren sounds from the piezo.
And led, and piezo buzzer, we have connected to the Arduino. Collect the schema, as shown in the figure. If you connect the led to digital pin other than "13", don't forget a current limiting resistor of about 220 Ohms.
Wiring tweeters and LEDs to Arduino
2
Write here is a sketch and upload it to your Arduino.
After loading the Board can see that the sketch is not quite as we need, until the work siren, the led will not blink and we'd like to see the led blinking while the sound of sirens. What's the problem here?
The fact that the usual way this problem can not be solved. Tasks are executed by the microcontroller sequentially. The operator "delay()" delays the program execution for a specified period of time, and until this time expires, the next program command will not be executed. Because of this, we can't set a different duration for each task in the cycle "loop()" program.
So you need to simulate multitasking.
Managing beeper and led consistently
3
A variant in which Arduino will carry out the tasks pseudo-concurrently, the proposed developers of Arduino in the article https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay.
The essence of the method is that at each iteration of the loop "loop ()", we check whether it is time to blink the led (to run a background task) or not. And if it is, then invert the led state. It is a peculiar variant of bypass of the operator "delay()".
A significant disadvantage of this method is that piece of code before control unit led should be faster than the time interval of blinking of the led "ledInterval". Otherwise, the flashing will occur less frequently than they should, and the effect of parallel execution of tasks we get. In particular, in our sketch, the duration changes the sound of the siren is 200+200+200+200 = 800 MS, and the blink interval of the led light we asked 200 msec. But the led will blink with a period of 800 milliseconds, which is 4 times different from what we asked. In General, if your code uses the "delay ()" in this case, it is difficult to simulate pseudo-parallelism, so it is advisable to avoid it.
In this case it would be necessary for control unit siren sound also check the time or not, and not to use "delay()". But this would have increased the amount of code and worsened the readability of the program.
The blinking led without delay()
4
To solve the problem, we use the wonderful library ArduinoThread that allows you to easily create pseudo-parallel processes. It works in a similar way, but allows you to write code for checking the time - you need to perform the task in this cycle or not. This helps reduce the amount of code and improves the readability of the sketch. Let's see the library in action.
The first thing I will download from the official site https://github.com/ivanseidel/ArduinoThread/archive/master.zip the archive library and rotaryforum it into the directory "libraries" of Arduino development IDE. Then rename the folder "ArduinoThread-master" in "ArduinoThread".
Installing the library ArduinoThread
5
The circuit connections will remain the same. To change only the program code. Now it will be the same as the sidebar.
In the program we create two threads that each performs its operation: single flashing led, the second controls the sound of a siren. In each iteration of the loop for each thread check whether the time of its execution or not. If it is - it is run using the method "run()". The main thing - not to use "delay()".
In the code given a more detailed explanation.
Upload code to the Arduino memory, run. Now everything works exactly as it should be!
Managing beeper and led in parallel with the flow