Created
October 18, 2015 08:35
-
-
Save RyoSaitoOp3/b75aec85e5c348789297 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.serial.*; | |
Serial arduino; // Arduino と通信するための Serial オブジェクト | |
// データを格納するための配列を確保します。 | |
int size = 1200; | |
float[] d1 = new float[size]; | |
float[] d2 = new float[size]; | |
float[] d3 = new float[size]; | |
// グラフ描画用の設定値 | |
int top = 25; // グラフエリア上端の位置。 | |
int bottom = 675; // グラフエリア下端の位置。 | |
int left = 5; // グラフエリア左端の位置。 | |
int right = 1205; // グラフエリア右端の位置。 | |
float highest = 130.0; // グラフに表示するデータの最大値。 | |
float lowest = 0.0; // グラフに表示するデータの最小値。 | |
void setup(){ | |
size( 1250, 680 ); // 描画のためのウィンドウサイズを設定します。 | |
String arduinoPort = Serial.list()[2]; // 3 番目が Arduino のポートです。 | |
arduino = new Serial( this, arduinoPort, 9600 ); // 9600bps でポートを開きます。 | |
arduino.write( "r" ); // ハンドシェイクするため最初のリクエストを送ります。 | |
arduino.bufferUntil( '\n' ); // Arduino が返信したら serialEvent()を呼び出します。 | |
} | |
void draw(){ | |
int i, j; | |
float[] drawData = new float[2]; | |
// グラフの枠とグリッド線を描画します。 | |
background( 0 ); // 背景の色を黒にします。 | |
noFill(); // 塗りつぶしを行わない設定にします。 | |
stroke( 0, 255, 0 ); // 線の色を緑色にします。引数は (r,g,b) です。 | |
strokeWeight( 1 ); // 線の太さを 1pixel にします。 | |
rect( left, top, right-left, bottom-top ); // グラフエリアの長方形を描画します。 | |
for( i=60; i<right-left; i+=60 ){ | |
line( i+left, top, i+left, bottom ); // 縦方向のグリッド線を描画します。 | |
} | |
for( i=50; i<bottom-top; i+=50 ){ | |
line( left, i+top, right, i+top ); // 横方向のグリッド線を描画します。 | |
} | |
// d1のデータを折れ線グラフで描画します。 | |
stroke( 255, 255, 255 ); // 線の色を白にします。 | |
for( i=0; i<size-1; i++ ){ | |
for( j=0; j<2; j++ ){ | |
drawData[j] = d1[i+j]; | |
if( drawData[j] > highest ){ | |
drawData[j] = highest; // データ値が上限を超えていたらクリッピングします。 | |
}else if( drawData[j] < lowest ){ | |
drawData[j] = lowest; // データが下限を超えていたらクリッピングします。 | |
} | |
} | |
line( right-i, bottom-(drawData[0]-lowest)/(highest-lowest)*(bottom-top), | |
right-(i+1), bottom-(drawData[1]-lowest)/(highest-lowest)*(bottom-top) ); | |
} | |
// d2のデータを折れ線グラフで描画します。 | |
stroke( 255, 0, 0 ); // 線の色を赤にします。 | |
for( i=0; i<size-1; i++ ){ | |
for( j=0; j<2; j++ ){ | |
drawData[j] = d2[i+j]; | |
if( drawData[j] > highest ){ | |
drawData[j] = highest; // データ値が上限を超えていたらクリッピングします。 | |
}else if( drawData[j] < lowest ){ | |
drawData[j] = lowest; // データが下限を超えていたらクリッピングします。 | |
} | |
} | |
line( right-i, bottom-(drawData[0]-lowest)/(highest-lowest)*(bottom-top), | |
right-(i+1), bottom-(drawData[1]-lowest)/(highest-lowest)*(bottom-top) ); | |
} | |
// d3のデータを折れ線グラフで描画します。 | |
stroke( 0, 0, 255 ); // 線の色を赤にします。 | |
for( i=0; i<size-1; i++ ){ | |
for( j=0; j<2; j++ ){ | |
drawData[j] = d3[i+j]; | |
if( drawData[j] > highest ){ | |
drawData[j] = highest; // データ値が上限を超えていたらクリッピングします。 | |
}else if( drawData[j] < lowest ){ | |
drawData[j] = lowest; // データが下限を超えていたらクリッピングします。 | |
} | |
} | |
line( right-i, bottom-(drawData[0]-lowest)/(highest-lowest)*(bottom-top), | |
right-(i+1), bottom-(drawData[1]-lowest)/(highest-lowest)*(bottom-top) ); | |
} | |
//文字列を描画します | |
text( nf(highest,2,1), right+5, top+8 ); // highest の値を文字列に変換して描画します。 | |
text( nf(lowest,2,1), right+5, bottom ); // lowest の値を文字列に変換して描画します。 | |
text( "底面センサーの温度: "+nf(d1[0],2,1)+"°C 液中センサーの温度: "+nf(d2[0],2,1)+"°C", left, top-5 ); //「現在の温度:○○.○°C」を描画します。 | |
} | |
void serialEvent( Serial arduino ){ | |
String s = arduino.readStringUntil( '\n' ); // Arduino からの文字列を読み出します。 | |
arduino.write( "r" ); // 次のリクエストを Arduino に送信します。 | |
// 配列の内容を 1 つずつ隣に移動し、d[0] を空けます。 | |
println(s); | |
for( int i=size-1; i>0; i-- ){ | |
d1[i] = d1[i-1]; | |
d2[i] = d2[i-1]; | |
d3[i] = d3[i-1]; | |
} | |
// Arduino から受け取ったデータを d[0] に格納します。 | |
float temp[] = float(split(s, ',')); | |
d1[0] = temp[0]; | |
d2[0] = temp[1]; | |
d3[0] = temp[2]/10; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment