Skip to content

Commit 4dd54bb

Browse files
committed
Adding documentation for threading basics.
1 parent 33fb1ce commit 4dd54bb

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

docs/01-threading-basics.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
```

docs/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<img src="https://content.arduino.cc/website/Arduino_logo_teal.svg" height="100" align="right"/>
2+
3+
`Arduino_Threads/docs`
4+
======================
5+
The Arduino threading APIs brings multi-threading to the world of Arduino.
6+
7+
The following Arduino architectures and boards are supported:
8+
* `mbed_portenta`: [Portenta H7](https://store.arduino.cc/products/portenta-h7)
9+
* `mbed_nano`: [Nano 33 BLE](https://store.arduino.cc/arduino-nano-33-ble), [Nano RP2040 Connect](https://store.arduino.cc/nano-rp2040-connect)
10+
* `mbed_edge`: [Edge Control](https://store.arduino.cc/products/arduino-edge-control)
11+
12+
Threading on Arduino can be achieved by leveraging the [Arduino_Threads](https://github.com/bcmi-labs/Arduino_Threads) library in combination with [arduino-cli](https://github.com/facchinm/arduino-cli/commits/arduino_threads_rebased).
13+
14+
### Table of Contents
15+
* [Threading Basics](01-threading-basics.md)

0 commit comments

Comments
 (0)