http://coopermaa2nd.blogspot.tw/2011/03/processing-arduino.html
arduino code
/* Arduino 範例:
* 讀取接在 pin 0 上的可變電阻,並將讀值列印到 Serial Port
*/
int potPin = 0; // 可變電阻接在 pin 0 上
void setup() {
// 開啟 Serial port, 通訊速率為 9600 bps
Serial.begin(9600);
}
void loop() {
// 讀取可變電阻
int sensorValue = analogRead(potPin);
// 將讀值列印到 Serial Port, 以位元組(Byte)的格式寫出
// 讀值除以 4, 把 0-1023 的數值按比例縮放為 0-255 之間的數值
Serial.write(sensorValue/4);
delay(100);
}
processing code
/* Processing 範例:
* 讀取從 Serial Port 傳進來的 Sensor 讀值
* 利用 Sensor 讀值移動矩形 (Rectangle)
*/
import processing.serial.*;
Serial serial;
int sensorValue;
void setup() {
// 設定畫布大小為 305 x 200
size(305, 200);
// 開啟 Serial port,通訊速率為 9600 bps
// 注意! 如果你 Arduino 不是接在 COM4,請做適當調整
serial = new Serial(this, "COM4", 9600);
}
void draw() {
if ( serial.available() > 0) {
// 讀取從 Serial Port 傳進來的 Sensor 讀值
sensorValue = serial.read();
println(sensorValue);
// 在 (x, y) 座標為 (sensorValue, 80) 的位置畫一個 50x50 的矩形
background(255); // 白色背景
fill(255,0,0); // 填滿顏色為紅色
rect(sensorValue, 80, 50, 50);
}
}
沒有留言:
張貼留言