사실상 멀티쓰레드는 아닙니다. 아두이노 우노에서 멀티쓰레드는 구현이 불가능한게 아닐런지…(더 찾아봐야 할 듯…)

다운로드 ::
https://github.com/jlamothe/mthread

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
#include "mthread.h"

int piezo = 3;
int del = 1;

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

class BlessingThread : public Thread
{
  public:
      BlessingThread(int id);
  protected:
      bool loop();
  private:
      int id;
};

BlessingThread::BlessingThread(int id)
{
    this->id = id;
}

bool BlessingThread::loop()
{
  if(kill_flag) return false;

  if(id == 1)
  {
    brightness = brightness + fadeAmount;
    analogWrite(led, brightness);
   
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ;
    }

    delay(del * 3);
  }
  else if(id == 2)
  {
    analogWrite(piezo, 64);
    delay(del);
    analogWrite(piezo, 128);
    delay(del);
    analogWrite(piezo, 256);
    delay(del);
  }
 
  return true;
}

void setup()
{
  Serial.begin(9600);
 
  main_thread_list->add_thread(new BlessingThread(1));
  main_thread_list->add_thread(new BlessingThread(2));
}