2008年6月12日木曜日

センサ値をグラフ表示

utano

tmg様ごめんなさい、
一個の前の記事のコードではまだグラフは描けないのです…

p.102-103のRefine Itのコードを書き足さないといけないのです。
これならいけるはず:

import processing.serial.*;

int linefeed = 10;//linefeed in ASCII
Serial myPort;//the serial port
int sensorValue = 0;//the value from the sensor

int graphPosition = 0;//the horizontal position of the latest line to be drawn on the graph

void setup(){
size(400,300);
//list all the available serial ports
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil(linefeed);
}

void draw(){
//twiddle your thumbs
}

void serialEvent(Serial myPort){
//read the serial buffer
String myString = myPort.readStringUntil(linefeed);
//if you got any bytes other than the linefeed
if(myString != null){
//trim off the carriage return and convert the string to an integer
sensorValue = int(trim(myString));
//print it
println(sensorValue);
drawGraph();
}
}

void drawGraph(){
//adjust this formula so that lineHeight
//is always less than the height of the window
int lineHeight = sensorValue /2;

//draw the line
stroke(0,0,255);
line(graphPosition, height, graphPosition, height-lineHeight);

//at the edge of the screen, go back to the beginning
if(graphPosition >= width){
graphPosition = 0;
background(0);
}
else{
graphPosition++;
}
}

0 件のコメント: