Arduinoとの連携

Arduinoと連携することで、外部機器の操作でProcessingのプログラムを操作できる。Arudionoとの通信はシリアル通信で行う。

// 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 );
}
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