Smoothing

はじめに

アナログピンから読み取った値の平均値を求め、出力します。

アナログの0番ピンが入力です。

プログラム

定義等

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

  Reads repeatedly from an analog input, calculating a running average and
  printing it to the computer. Keeps ten readings in an array and continually
  averages them.

  The circuit:
  - analog sensor (potentiometer will do) attached to analog input 0

  created 22 Apr 2007
  by David A. Mellis  <dam@mellis.org>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

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

// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;
 

const int型の変数 numReadingsを定義し、10を代入します。これは、平均をとるためにアナログピンから値を読み込む回数です。

int型の配列readingsを定義します。配列の大きさは、先ほど定義したnumReadingsです。これにより、readings[0]からreadings[numReadings-1]の領域がとられます。配列の大きさを指定するには、数値を直接指定する(#defineの利用を含みます)か、constで修飾された整数型(char, byte, int, word, longが使えます)の変数で指定する必要があります。

また、int型の変数index, total, average, inputPinを定義し初期化します。

totalは、(最大)過去10回のanalogRead()の読み取り値の合計を保持する変数です。

indexは、読み取った値をreadings[]のどこに格納するかを表します。0からnumReadings-1までの値で、loop()が実行するたびに1増えます。ただし、numReadings-1の次は0に戻ります。

setup()

34
35
36
37
38
39
40
41
42
void setup() {
  // initialize serial communication with computer:
  Serial.begin(9600);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  }
}
 

Serial.begin()を使って、シリアルコンソールを初期化します。

for文を使って、readings[]の全要素に0を代入します。

loop()

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
void loop() {
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(inputPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  Serial.println(average);
  delay(1);        // delay in between reads for stability
}

最初にtotalからreadings[index]を引きます。今までの合計値から、今回代入する部分の値を引くことを意味します。

この部分は、-=演算子を使って、以下のように書く方が多いように思います。

  total -= readings[readIndex];

analogRead()で読み取った値を、readings[index]に代入します。totalにreadings[index]を足します。

indexを1増やして、次のloop()のときに利用するreadings[]の場所を変更します。indexがnumReadings以上かどうかをif文を使って判断し、そうであれば、indexを0に戻します。

平均値を求め、求めた平均値を、Serial.println()を使い、シリアルコンソールに出力します。

平均を求める際、単純にnumReadingsの値で割っているため、最初の9回は誤った数値を出すのではないかと思います。

バージョン

Hardware:Arduino Mega
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system