VirtualColorMixer

はじめに

3つのアナログセンサの値を送信します。

プログラム

定義等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
  This example reads three analog sensors (potentiometers are easiest) and sends
  their values serially. The Processing and Max/MSP programs at the bottom take
  those three values and use them to change the background color of the screen.

  The circuit:
  - potentiometers attached to analog inputs 0, 1, and 2

  created 2 Dec 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/VirtualColorMixer
*/

const int redPin = A0;		// sensor to control red color
const int greenPin = A1;	// sensor to control green color
const int bluePin = A2;		// sensor to control blue color
  

int型の変数redPinとgreenPin、bluePinを定義し、それぞれ、A0とA1、A2で初期化します。

setup()

23
24
25
26
void setup() {
  Serial.begin(9600);
}
 

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

loop()

27
28
29
30
31
32
33
void loop() {
  Serial.print(analogRead(redPin));
  Serial.print(",");
  Serial.print(analogRead(greenPin));
  Serial.print(",");
  Serial.println(analogRead(bluePin));
}

analogRead()で読み取った値をSerial.print()Serial.println()でシリアルポートに送信します。

analogRead()で読み取った値を変数に代入することなく、他の関数(この場合はSerial.print())の引数にすることができます。

その他

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

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

September 11, 2021

inserted by FC2 system