Warning: Trying to access array offset on value of type bool in /home/cycos/cammy.co.jp/public_html/technical/wp-content/themes/cocoon-master/lib/utils.php on line 2604

Warning: Trying to access array offset on value of type bool in /home/cycos/cammy.co.jp/public_html/technical/wp-content/themes/cocoon-master/lib/utils.php on line 2604

Warning: Trying to access array offset on value of type bool in /home/cycos/cammy.co.jp/public_html/technical/wp-content/themes/cocoon-master/lib/utils.php on line 2632

delayMicroseconds

説明

指定したマイクロ秒プログラムの動作を止めます。
1秒は1,000,000マイクロ秒となりますが、関数で正確に停止できるのは最大値は16383の指定までです。これ以上はdelayを使います。

書き方 delayMicroseconds(マイクロ秒)
戻り値 なし
引数 (unsigned int)マイクロ秒

プログラム例

int outPin = 8;               // digital pin 8

void setup() {
  pinMode(outPin, OUTPUT);    // sets the digital pin as output
}

void loop() {
  digitalWrite(outPin, HIGH); // sets the pin on
  delayMicroseconds(50);      // pauses for 50 microseconds
  digitalWrite(outPin, LOW);  // sets the pin off
  delayMicroseconds(50);      // pauses for 50 microseconds
}

クムクムロボットロボットでの使用

delayMicrosecondsは、弊社のクムクムロボットの超音波距離センサー(HC-SR04)の制御で使います。

#define Trig 13
#define Echo A3
int Duration;
float Distance;

void setup() {
    Serial.begin(9600);
    while (!Serial) { ; } //Leonardoの場合にはこの部分が必要
    pinMode(Trig,OUTPUT);
    pinMode(Echo,INPUT);
}

void loop() {
    digitalWrite(Trig,LOW); //まずLowにする
    delayMicroseconds(1); //1μ秒まつ
    digitalWrite(Trig,HIGH); //トリガー信号をあげる
    delayMicroseconds(11); //10μ秒だが念のため11μ秒HIGHを出す
    digitalWrite(Trig,LOW); //信号を落とす
    Duration = pulseIn(Echo,HIGH);//パルスがHIGHの間の時間を測る
    if (Duration>0) {
    //パルスの秒から距離を計算する
        Distance = Duration/2;
        Distance = Distance*340*100/1000000;
        Serial.println(Distance);//シリアルに書き出してみる
    }
    delay(1000);
}
タイトルとURLをコピーしました