delay()

概要

delay()は、指定した値(ミリ秒単位)だけ待つ関数です。micros()で得られるArduinoを起動してからのマイクロ秒を利用して遅延を実現します。基本的にはビジーウエイトです。

delay()のリファレンスはこちらをご覧ください。

ソースコード

delay()は、hardware/arduino/avr/cores/arduino/wiring.c に定義されています。以下に全ソースコードを示します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
void delay(unsigned long ms)
{
    uint32_t start = micros();
 
    while (ms > 0) {
        yield();
        while ( ms > 0 && (micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }
}

入力はunsinged long型の変数です。戻り値はありません。

最初に、開始した時刻をmicros()を利用して取得し、startに代入します。

1
2
3
void delay(unsigned long ms)
{
    uint32_t start = micros();

次に、入力として与えられたmsが0よりも大きい間、外側のwhileループを実行し続けます。

1
2
3
4
5
6
7
8
    while (ms > 0) {
        yield();
        while ( ms > 0 && (micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }
}

最初にyield()を呼び出しています。yield()は他のタスクに制御を移すために自らCPUを放棄し、他のタスクに処理を移行するための関数です。Arduino Unoの場合yield()は何もしません。

内側のwhileループでは、msが0よりも大きいときは、micros()を使って現在時刻を取得し、startから1000マイクロ秒以上たっていれば、msをデクリメントし、startに1000加算します。startから1000マイクロ秒たっていなければ、何もせずにループを回ります。

内側のwhileループがあるのは、yield()の処理に1ms以上かかった時に追いつくためと思います。開始時の時刻から終了時の時刻を計算しておかないのは、micros()がオーバーフローしたときの影響を少なくするためと思います(startも同じようにオーバーフローするので)。

1000マイクロ秒ごとにmsを1減算していくので、msが0になった時点でループは終了し、delay()を呼び出した関数に制御が戻ります。

バージョン

Arduino AVR Boards 1.8.6

ライセンス

 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
/*
  Copyright (c) 2016 garretlab.
  Added source code explanation by garretlab.
*/

/*
  wiring.c - Partial implementation of the Wiring API for the ATmega8.
  Part of Arduino - http://www.arduino.cc/

  Copyright (c) 2005-2006 David A. Mellis

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

最終更新日

July 31, 2025

inserted by FC2 system