import processing.serial.*;
Serial myPort;
String portName = "COM6"; //BT serial port
int baudRate = 115200;
int dataPlotWidth = 640;
int dataPlotHeight = 200;
float[] data = new float[5];
String[] dataMV = new String[5];
float[] prevData = new float[5];
color[] colors = {
color(255, 0, 0),
color(0, 0, 255),
color(0, 0, 0, 0),
color(0, 0, 0, 0)
};
int datacount=0;
String OutFileName;
String buf2;
PrintWriter writer;
int startMillis;
void setup()
{
//size(dataPlotWidth, dataPlotHeight);
size(600,500);
myPort = new Serial(this, portName, baudRate);
OutFileName=getTimestamp()+".csv";
writer = createWriter("./data/"+OutFileName); // Create a new file in the sketch directory
buf2="";
}
void draw()
{
if((datacount % 600) == 0) {
background(255); // clear screen
fill(0, 0, 0); // set the fill color to black
textSize(12);
text("40 deg", 10, 20); //
}
while (myPort.available() > 0)
{
String inString = myPort.readStringUntil('\n');
if (inString != null)
{
inString = trim(inString);
String[] parts = split(inString, ',');
if(parts.length >3)
{
for (int i = 0; i < 5; i++)
{
prevData[i] = data[i];
data[i] = map(float(parts[i]), 20, 40, 0, 500); // Assuming your data is in the range 0-200
dataMV[i] = parts[i];
}
String stamp=getTimestamp2();
String etime= getElapsedTime();
String buf=stamp+","+etime+","+dataMV[0] + "," + dataMV[1] + "," + dataMV[2] + "," + dataMV[3] + "," + dataMV[4];
println(buf);
buf2=buf2+buf+"\n";
if(datacount%10==0){
writer.print(buf2);
writer.flush();
buf2="";
}
int xp = datacount%600;
strokeWeight(1);
for (int i = 0; i < 1; i++)
{
stroke(colors[i]);
line(xp-1, 500 - prevData[i], xp, 500 - data[i]);
}
drawCurrentTime();
drawElapsedTime();
datacount++;
}
}
}
}
void stop() {
writer.flush(); // Writes the remaining data to the file
writer.close(); // Finishes the file
super.stop();
}
Subroutine1(現在時刻, 経過時間)
String getTimestamp() {
int y = year();
int m = month();
int d = day();
int h = hour();
int min = minute();
int s = second();
// Use nf() to add leading zeroes to each time component if needed
String timestamp = nf(y, 4) + nf(m, 2) + nf(d, 2) + "_" + nf(h, 2) + nf(min, 2) + nf(s, 2);
return timestamp;
}
String getTimestamp2() {
int h = hour();
int min = minute();
int s = second();
// Use nf() to add leading zeroes to each time component if needed
String timestamp = nf(h, 2) +":"+ nf(min, 2) +":"+ nf(s, 2);
return timestamp;
}
void drawCurrentTime()
{
String stamp2=getTimestamp2();
fill(255); noStroke();
rect(450, 5,130,50);
fill(0); stroke(0);textSize(24);
text(stamp2, 450, 30); //
}
void drawElapsedTime()
{
String elapsedTime = getElapsedTime();
fill(255); noStroke();
rect(450, 55,130,50);
fill(0); stroke(0);textSize(24);
text(elapsedTime, 450, 60);
}
String getElapsedTime() {
int elapsedMillis = millis() - startMillis;
int s = elapsedMillis / 1000;
int m = s / 60;
int h = m / 60;
s = s % 60;
m = m % 60;
return nf(h, 2) + ":" + nf(m, 2) + ":" + nf(s, 2);
}