5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Teensyduinoでスレッドを使う

Last updated at Posted at 2021-08-02

#Teensyduinoでスレッドを使う
TeensyThreads.hを使います
##インストール
https://github.com/ftrias/TeensyThreads
##注意
マルチスレッドにする場合、
普通のdelay()を使うと指定した時間の待機になりません。
threads.delay()を使いましょう。

#include <TeensyThreads.h>

volatile long old[4];

void one() {
  while (1) {
    Serial.print("ONE ");
    Serial.print(threads.id());
    Serial.print(" ");
    Serial.println(millis());
    threads.delay(1000);
  }
}

void two() {
  while (1) {
    Serial.print("TWO ");
    Serial.print(threads.id());
    Serial.print(" ");
    Serial.println(millis());
    threads.delay(1000);
  }
}

void three() {
  while (1) {
    Serial.print("THREE ");
    Serial.print(threads.id());
    Serial.print(" ");
    Serial.println(millis());
    threads.delay(1000);
  }
}

void setup() {
  Serial.begin(9600);
  threads.addThread(one);
  threads.addThread(two);
  threads.addThread(three);
}

void loop() {
  Serial.print("MAIN ");
  Serial.print(threads.id());
  Serial.print(" ");
  Serial.println(millis());
  threads.delay(1000);
//  delay(1000);   //上の行をコメントアウトしてこの行のコメントアウトを外すと1秒間隔がずれていく
}
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?