|
| 1 | +<img src="https://content.arduino.cc/website/Arduino_logo_teal.svg" height="100" align="right"/> |
| 2 | + |
| 3 | +Threading Basics |
| 4 | +================ |
| 5 | +## Introduction |
| 6 | +In the historic single-threaded execution of Arduino programs the complete program logic is contained within the `*.ino` file. It contains both a `setup()` function, which is executed only once at program start, and a `loop()` function, which is executed indefinitely. In order to support multi-threaded (or parallel) sketch execution a new file type called the `*.inot` file is introduced. While a Arduino program can contain one `*.ino` file, they can contain multiple `*.inot` files. Each `*.inot` file contains it's own `setup()` and `loop()` and represent separate thread. |
| 7 | + |
| 8 | +The advantage of this approach is that a complicated and a long `loop()` function (consisting of nested [finite state machines](https://en.wikipedia.org/wiki/Finite-state_machine)) found in typical Arduino sketches can be broken up in several, parallel executed `loop()` functions with a much smaller scope. This increases program readability and maintainability, directly reducing to bugs (software errors). |
| 9 | + |
| 10 | +#### Example (Single-Threaded): |
| 11 | +This sketch demonstrates how one would implement a program which requires the execution of three different actions on three different periodic intervals. |
| 12 | +**Blink_Three_LEDs.ino**: |
| 13 | +```C++ |
| 14 | +void setup() |
| 15 | +{ |
| 16 | + pinMode(LED_RED, OUTPUT); |
| 17 | + pinMode(LED_GREEN, OUTPUT); |
| 18 | + pinMode(LED_BLUE, OUTPUT); |
| 19 | +} |
| 20 | + |
| 21 | +int const DELAY_RED_msec = 900; |
| 22 | +int const DELAY_GREEN_msec = 500; |
| 23 | +int const DELAY_BLUE_msec = 750; |
| 24 | + |
| 25 | +void loop() |
| 26 | +{ |
| 27 | + static unsigned long prev_red = millis(); |
| 28 | + static unsigned long prev_green = millis(); |
| 29 | + static unsigned long prev_blue = millis(); |
| 30 | + |
| 31 | + unsigned long const now = millis(); |
| 32 | + |
| 33 | + if ((now - prev_red) > DELAY_RED_msec) { |
| 34 | + prev_red = now; |
| 35 | + digitalWrite(LED_RED, !digitalRead(LED_RED)); |
| 36 | + } |
| 37 | + |
| 38 | + if ((now - prev_green) > DELAY_GREEN_msec) { |
| 39 | + prev_green = now; |
| 40 | + digitalWrite(LED_GREEN, !digitalRead(LED_GREEN)); |
| 41 | + } |
| 42 | + |
| 43 | + if ((now - prev_blue) > DELAY_BLUE_msec) { |
| 44 | + prev_blue = now; |
| 45 | + digitalWrite(LED_BLUE, !digitalRead(LED_BLUE)); |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | +#### Example (Multi-Threaded): |
| 50 | +The same functionality can be provided via multi-threaded execution in a much simpler way. |
| 51 | +**Blink_Three_LEDs.ino** |
| 52 | +```C++ |
| 53 | +void setup() { |
| 54 | + LedRed.start(); |
| 55 | + LedGreen.start(); |
| 56 | + LedBlue.start(); |
| 57 | +} |
| 58 | + |
| 59 | +void loop() { |
| 60 | + |
| 61 | +} |
| 62 | +``` |
| 63 | +**LedRed.inot** |
| 64 | +```C++ |
| 65 | +void setup() { |
| 66 | + pinMode(LED_RED, OUTPUT); |
| 67 | +} |
| 68 | + |
| 69 | +int const DELAY_RED_msec = 900; |
| 70 | + |
| 71 | +void loop() { |
| 72 | + digitalWrite(LED_RED, !digitalRead(LED_RED)); |
| 73 | + delay(DELAY_RED_msec); |
| 74 | +} |
| 75 | +``` |
| 76 | +**LedGreen.inot** |
| 77 | +```C++ |
| 78 | +void setup() { |
| 79 | + pinMode(LED_GREEN, OUTPUT); |
| 80 | +} |
| 81 | + |
| 82 | +int const DELAY_GREEN_msec = 500; |
| 83 | + |
| 84 | +void loop() { |
| 85 | + digitalWrite(LED_GREEN, !digitalRead(LED_GREEN)); |
| 86 | + delay(DELAY_GREEN_msec); |
| 87 | +} |
| 88 | +``` |
| 89 | +**LedBlue.inot** |
| 90 | +```C++ |
| 91 | +void setup() { |
| 92 | + pinMode(LED_BLUE, OUTPUT); |
| 93 | +} |
| 94 | + |
| 95 | +int const DELAY_BLUE_msec = 750; |
| 96 | + |
| 97 | +void loop() { |
| 98 | + digitalWrite(LED_BLUE, !digitalRead(LED_BLUE)); |
| 99 | + delay(DELAY_BLUE_msec); |
| 100 | +} |
| 101 | +``` |
0 commit comments