Graph

はじめに

アナログの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
/*
  Graph

  A simple example of communication from the Arduino board to the computer: The
  value of analog input 0 is sent out the serial port. We call this "serial"
  communication because the connection appears to both the Arduino and the
  computer as a serial port, even though it may actually use a USB cable. Bytes
  are sent one after another (serially) from the Arduino to the computer.

  You can use the Arduino Serial Monitor to view the sent data, or it can be
  read by Processing, PD, Max/MSP, or any other program capable of reading data
  from a serial port. The Processing code below graphs the data received so you
  can see the value of the analog input changing over time.

  The circuit:
  - any analog input sensor attached to analog in pin 0

  created 2006
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe and Scott Fitzgerald

  This example code is in the public domain.

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

このプログラムでは特に何もしていません。

setup()

28
29
30
31
32
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}
 

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

loop()

33
34
35
36
37
38
39
void loop() {
  // send the value of analog input 0:
  Serial.println(analogRead(A0));
  // wait a bit for the analog-to-digital converter to stabilize after the last
  // reading:
  delay(2);
}

analogRead()で読み取った値をSerial.println()を使ってシリアルポートに送信します。この例のように、ある関数の戻り値を他の関数の引数に指定することも可能です。

最後に、delay()を使ってADコンバータが安定するのを待ちます。

その他

この例題には、通信相手となるProcessingのプログラムも入っていますが、こちらはよくわからないので掲載していません。

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system