SerialCallResponse

はじめに

最初にデータを受信するまで、‘A’をシリアルポートに送信し続けます。一度データを受信した後は、データを受信するたびにセンサの値を送信します。

プログラム

定義等

 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
/*
  Serial Call and Response
  Language: Wiring/Arduino

  This program sends an ASCII A (byte of value 65) on startup and repeats that
  until it gets some data in. Then it waits for a byte in the serial port, and
  sends three sensor values whenever it gets a byte in.

  The circuit:
  - potentiometers attached to analog inputs 0 and 1
  - pushbutton attached to digital I/O 2

  created 26 Sep 2005
  by Tom Igoe
  modified 24 Apr 2012
  by Tom Igoe and Scott Fitzgerald
  Thanks to Greg Shakar and Scott Fitzgerald for the improvements

  This example code is in the public domain.

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

int firstSensor = 0;    // first analog sensor
int secondSensor = 0;   // second analog sensor
int thirdSensor = 0;    // digital sensor
int inByte = 0;         // incoming serial byte
  

int型の変数firstSensorとsecondSensor、thirdSensor、inByteを定義し、初期化します。

setup()

29
30
31
32
33
34
35
36
37
38
39
void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  pinMode(2, INPUT);   // digital sensor is on digital pin 2
  establishContact();  // send a byte to establish contact until receiver responds
}
 

Serial.begin()を使って、シリアルポートを初期化します。Arduino Loenardでは、シリアルコンソールを開いたときにリセットがかからないようです。このため、シリアルポートを開いたときには、既に送信してしまったデータは表示されません。これを防ぐために、シリアルポートが接続されるまで待つことで対応しています。pinMode()で2番ピンを入力モードにします。

最後に、establishContact()を呼び出します。この関数はこのプログラムの中で定義している関数です。後で説明します。

loop()

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void loop() {
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    // read first analog input, divide by 4 to make the range 0-255:
    firstSensor = analogRead(A0) / 4;
    // delay 10ms to let the ADC recover:
    delay(10);
    // read second analog input, divide by 4 to make the range 0-255:
    secondSensor = analogRead(1) / 4;
    // read switch, map it to 0 or 255L
    thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
    // send sensor values:
    Serial.write(firstSensor);
    Serial.write(secondSensor);
    Serial.write(thirdSensor);
  }
}
 

Serial.available()を使って、シリアルポートでデータを受信しているかを調べます。データを受信していれば、Serial.read()でデータを読み込み、inByteに代入します。

analogRead()で、アナログの0番と1番の値を読み込み、4で割った値をそれぞれ、firstSensorとsecondSensorに代入します。digitalRead()でデジタルピンの2番の値を読み込み、map()で0は0に、1は255に変換し、thirdSensorに代入します。

Serial.write()でfirstSensorとsecondSensor、thirdSensorをシリアルポートに送信します。

establishContact()

60
61
62
63
64
65
void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A');   // send a capital A
    delay(300);
  }
}

シリアルポートでデータを受信していない間(Serial.available()が0以下の値を返している間)、Serial.print()を使って、‘A’を送信し続けます。

この関数は、setup()から呼ばれています。

その他

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

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system