Processing/5.Arduinoとの連携
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
* Arduinoとの連携 [#nc3c128e]
[[Arduino]]と連携することで、外部機器の操作でProcessingの...
以下の例は、ArduinoのA0ピンに入力された値をPCに送信し、PC...
--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();
}
}
終了行:
* Arduinoとの連携 [#nc3c128e]
[[Arduino]]と連携することで、外部機器の操作でProcessingの...
以下の例は、ArduinoのA0ピンに入力された値をPCに送信し、PC...
--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();
}
}
ページ名: