#author("2020-09-11T11:54:41+09:00","default:qua","qua")
#author("2020-09-11T11:54:56+09:00","default:qua","qua")
* Arduinoとの連携 [#nc3c128e]

[[Arduino]]と連携することで、外部機器の操作でProcessingのプログラムを操作できる。Arudionoとの通信には[[Serial通信>Arduino/通信/シリアル通信]]を用いる。
以下の例は、ArduinoのA0ピンに入力された値をPCに送信し、PCで動作しているProcessingでその数値を表示する。

--Arduinoのプログラム
 // A0ピンにアナログ入力された値(0~1023)を
 // 2ビットシフトして8ビットにして、シリアルで送信する。
 // 通信速度は9600bps
 int sensorPin = A0;
 int sensorValue0 = -1;
 int sensorValue;
 
 void setup() {
   Serial.begin( 9600 );
 }
 
 void loop() {
   sensorValue = analogRead(sensorPin);
   if( sensorValue != sensorValue0 ){
     sensorValue0 = sensorValue;
     Serial.write( sensorValue >> 2 );
   }
   delay( 10 );
 }

--Processingのプログラム
 import processing.serial.*;
 Serial port;
 int data;
 
 void setup() 
 {
   port = new Serial(this, Serial.list()[0], 9600);
   size(300, 300);
   background(0, 0, 0);
 }

 
 void draw() 
 {
   // 描画エリア設定
   fill(255);
   noStroke();
   rect(0, 0, width, height);
   fill(0);
   textSize(height*0.10);
   textAlign(LEFT);
   text(data, width*0.5, height*0.5);
 }
 
 void serialEvent(Serial port)
 {
   // シリアルポートからデータを受け取ったら
   if (port.available() >=1) 
   {
     data = port.read();
   }
 }

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS