loop()을 동시에 여러개 돌리고 싶어서 멀티쓰레드 라이브러리를 찾아봤는데 아두이노 우노를 지원하는 멀티쓰레드는 제대로 동작하는게 없네요.

다운로드 ::
https://github.com/keepworking/ezTask

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "ezTask.h"

int piezo = 3;
int del = 300;

int led = 9;
int brightness = 0;
int fadeAmount = 5;

#define LENGTH(x) sizeof(x) / sizeof(x[0]);

//-------------------------------//
int a = 0;
void Task1_Runner();
ezTask Task1(0, Task1_Runner);

void (*Task1_func[])(void) {
  []() {
    analogWrite(led, brightness);
    brightness = brightness + fadeAmount;
   
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }
  },
  []() {a++;},
  []() {Task1.Cycle = 10;},
  []() {Task1.Cycle = 0;}
};

void Task1_Runner() {
  static int idx = 0;
  Task1_func[idx]();
  idx++;
  idx %= LENGTH(Task1_func);
}

//-------------------------------//
int b = 0;
void Task2_Runner();
ezTask Task2(0, Task2_Runner);

void (*Task2_func[])(void) {
  []() {
    analogWrite(piezo, 64);
    delay(del);
    analogWrite(piezo, 128);
    delay(del);
    analogWrite(piezo, 256);
    delay(del);
  },
  []() {b++;},
  []() {Task2.Cycle = 30;},
  []() {Task2.Cycle = 0;}
};

void Task2_Runner() {
  static int idx = 0;
  Task2_func[idx]();
  idx++;
  idx %= LENGTH(Task2_func);
}

void setup()
{
  Serial.begin(9600);
 
  pinMode(piezo, OUTPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  Task1.run();
  Task2.run();
}