PhysicalPixel

はじめに

シリアルポートにPhysicalPixelデータを送信します。

ArduinoとPCとをUSBケーブルで接続します。

プログラム

定義等

 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
/*
  Physical Pixel

  An example of using the Arduino board to receive data from the computer. In
  this case, the Arduino boards turns on an LED when it receives the character
  'H', and turns off the LED when it receives the character 'L'.

  The data can be sent from the Arduino Serial Monitor, or another program like
  Processing (see code below), Flash (via a serial-net proxy), PD, or Max/MSP.

  The circuit:
  - LED connected from digital pin 13 to ground through 220 ohm resistor

  created 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe and Scott Fitzgerald

  This example code is in the public domain.

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

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into
  

const int型の変数ledPinを定義し、初期化します。また、int型の変数incomingByteを定義します。

incomingByteは明示的に初期化していません。しかしincomingByteは静的記憶域期間を持つ算術型の変数なので、0に初期化されます。

setup()

27
28
29
30
31
32
33
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}
 

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

pinMode()で、ledPinを出力モードにします。

loop()

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}

Serial.available()で、シリアル入力があるかを調べます。入力がある場合、Serial.read()を使い、シリアル入力を読み込み、incomingByteに代入します。

if文を使って、incomingByteが’H’かどうかを調べ、そうであればdigitalWrite()を使ってledPinをHIGHにします。

if文を使って、incomingByteが’L’かどうかを調べ、そうであればdigitalWrite()を使ってledPinをLOWにします。

‘H’でも’L’でもなければ、何もしません。

HやLをシングルクォートを使って括っていることに気を付けてください。文字定数を表す場合はシングルクォートを使い、文字列を表す場合はダブルクォートを使います。

その他

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

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system