p03_LoveOMeter

はじめに

温度センサから値を読み取り読み取った値をシリアルコンソールに送信するとともに、LEDを点灯します。

アナログの0番ピンに温度センサが、デジタルの2番ピンと3番ピン、4番ピンに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
/*
  Arduino Starter Kit example
  Project 3 - Love-O-Meter

  This sketch is written to accompany Project 3 in the Arduino Starter Kit

  Parts required:
  - one TMP36 temperature sensor
  - three red LEDs
  - three 220 ohm resistors

  created 13 Sep 2012
  by Scott Fitzgerald

  https://store.arduino.cc/genuino-starter-kit

  This example code is part of the public domain.
*/

// named constant for the pin the sensor is connected to
const int sensorPin = A0;
// room temperature in Celsius
const float baselineTemp = 20.0;
 

int型の変数sensorPinを定義しA0を代入します。A0はアナログの0番ピンの意味です。float型の変数baselineTempを定義し20.0で初期化します。

setup()

25
26
27
28
29
30
31
32
33
34
35
void setup() {
  // open a serial connection to display values
  Serial.begin(9600);
  // set the LED pins as outputs
  // the for() loop saves some extra coding
  for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
    pinMode(pinNumber, OUTPUT);
    digitalWrite(pinNumber, LOW);
  }
}
 

Serial.begin()でシリアル通信の初期化を行います。通信速度は9600bpsです。

for文を使いpinNumberを2から4まで変化させます。for文の中では、pinMode()を使い、デジタルピンの2番と3番、4番を出力モードにし、digitalWrite()を使ってLOWを出力します。これによりLEDを消灯します。

loop()

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
void loop() {
  // read the value on AnalogIn pin 0 and store it in a variable
  int sensorVal = analogRead(sensorPin);

  // send the 10-bit sensor value out the serial port
  Serial.print("sensor Value: ");
  Serial.print(sensorVal);

  // convert the ADC reading to voltage
  float voltage = (sensorVal / 1024.0) * 5.0;

  // Send the voltage level out the Serial port
  Serial.print(", Volts: ");
  Serial.print(voltage);

  // convert the voltage to temperature in degrees C
  // the sensor changes 10 mV per degree
  // the datasheet says there's a 500 mV offset
  // ((voltage - 500 mV) times 100)
  Serial.print(", degrees C: ");
  float temperature = (voltage - .5) * 100;
  Serial.println(temperature);

  // if the current temperature is lower than the baseline turn off all LEDs
  if (temperature < baselineTemp + 2) {
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  } // if the temperature rises 2-4 degrees, turn an LED on
  else if (temperature >= baselineTemp + 2 && temperature < baselineTemp + 4) {
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
  } // if the temperature rises 4-6 degrees, turn a second LED on
  else if (temperature >= baselineTemp + 4 && temperature < baselineTemp + 6) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
  } // if the temperature rises more than 6 degrees, turn all LEDs on
  else if (temperature >= baselineTemp + 6) {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
  }
  delay(1);
}

analogRead()を使い、アナログの0番ピン(A0)の電圧を読み取りsensorValに代入します。

Serial.print()で、読み取った値をシリアルコンソールに表示します。

A0から読み取った値を電圧に変換します。

Serial.print()で、電圧をシリアルコンソールに表示します。

電圧を温度に変換し、結果をシリアルコンソールに表示します。

温度がbaselineTemp(20度)未満であれば、digitalWrite()で2番ピンと3番ピン、4番ピンにLOWを出力し、LEDをすべて消灯します。

温度がbaselineTemp+2以上、baselineTemp+4未満であれば、2番ピンのLEDを点灯、3番ピンと4番ピンのLEDを消灯します。

温度がbaselineTemp+4以上、baselineTemp+6未満であれば、2番ピンと3番ピンのLEDを点灯、4番ピンのLEDを消灯します。

温度がbaselineTemp+6以上であれば、全てのLEDを点灯します。

最後にdelay()で1ミリ秒待ちます。

baselineTemp以上baselineTemp+2未満のときは何もしません。意図的にそうしているのかはわかりません。

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system