Calibration

はじめに

センサからの入力を校正します。

プログラムの開始5秒間で読み取ったアナログピンの最小値と最大値を記録しておき、それ以降は、読み取った値をその範囲に収まるように制限します。

アナログピンの0番にアナログセンサ、デジタルピンの9番にLEDがつながっている想定です。

プログラム

定義等

 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
/*
  Calibration

  Demonstrates one technique for calibrating sensor input. The sensor readings
  during the first five seconds of the sketch execution define the minimum and
  maximum of expected values attached to the sensor pin.

  The sensor minimum and maximum initial values may seem backwards. Initially,
  you set the minimum high and listen for anything lower, saving it as the new
  minimum. Likewise, you set the maximum low and listen for anything higher as
  the new maximum.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0
  - LED attached from digital pin 9 to ground through 220 ohm resistor

  created 29 Oct 2008
  by David A Mellis
  modified 30 Aug 2011
  by Tom Igoe
  modified 07 Apr 2017
  by Zachary J. Fields

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Calibration
*/

// These constants won't change:
const int sensorPin = A0;    // pin that the sensor is attached to
const int ledPin = 9;        // pin that the LED is attached to

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value

 

const int型の変数 sensorPinとledPinを定義し、それぞれ、A0と9を代入します。プログラム内で変更することはできません。A0は、アナログピンを示すための変数です。9はデジタルの9番ピンです(アナログ出力はデジタルピンに対して行います)。Arduino Unoの場合、アナログピンは6本あるので、A0からA5まで利用可能です。数字の0から5を使っても大丈夫です。

int型の変数 sensorValueとsendorMin, sensorMaxを定義し、それぞれ、0と1023、0で初期化します。最終的にはsensorMinに最小値、sensorMaxに最大値が入ることになりますが、初期値はsensorMinに最大値、sensorMaxに最小値を入れていることに注意してください。

setup()

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
void setup() {
  // turn on LED to signal the start of the calibration period:
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  // calibrate during the first five seconds
  while (millis() < 5000) {
    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }

    // record the minimum sensor value
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }

  // signal the end of the calibration period
  digitalWrite(13, LOW);
}
 

pinMode()を使って、デジタルピンの13番を出力モードに設定します。ここでは、13という数値(定数)を直接指定しています。このような定数をリテラルと呼びます。

millis()が返す値が5000より小さい間、while文を実行し続けます。つまり、5秒間実行することになります。何回実行されるかはわかりません。

この中では、analogRead()で読み取った値(sensorValue)がsensorMaxよりも大きい場合は、その値(sensorValue)をsensorMaxに代入します。また、読み取った値(sensorValue)がsensorMinよりも小さい場合は、その値(sensorValue)をsensorMinに代入します。

loop()

63
64
65
66
67
68
69
70
71
72
73
74
75
void loop() {
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, sensorMin, sensorMax);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);
}

analogRead()を使い、sensorPinから値を読み込みます。読み取った値を、map()を使って、0から255にマッピングします。このとき、sensorMinが0、sensorMaxが255になるようにしています。

map()の結果、sensorValueは0より小さくなったり、255より大きくなることがあります。こうならないように、constrain()を利用して、sensorValueの値が0から255になるように調整します。これは、単に0より小さければ0、255より大きければ255という調整です。

最後に、変換後のsensorValueの値をanalogWrite()を使って、ledPinに出力します。結果として、入力値に応じた明るさでLEDが点灯します。

その後、analogWrite()で、実際に9番ピンに電圧を出力します。

Serial.print()Serial.println()を使い、読み取った値と計算したデューティ比を出力します。

最後に、次のloop()を実行する前にdelay()を使って10ミリ秒待ちます。オリジナルのプログラムのコメントによると、ADコンバータが安定するのを待つためのようです。

バージョン

Hardware:Arduino Mega
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system