XBeeから送信された加速度を表示・記録するプログラム(ファイル名を日時から生成)

OB201301.pde
import processing.serial.*;

int XBee = 0;

float VD = 2.9;                     // 加速度センサの電源電圧[v]
int SAMPLE_RATE = 100;              // サンプリング周波数[Hz]
String save_filename = "exp1.csv";  // データの保存ファイル名

Serial myPort;
ArrayList data;

void setup()
{
  size(100, 100);

  int m = month();
  int d = day();
  save_filename = year() + "";
  if (m < 10) {
    save_filename = save_filename + "0" + m;
  }
  else {
    save_filename = save_filename + m;
  }

  if (d < 10) {
    save_filename = save_filename + "0" + d;
  }
  else {
    save_filename = save_filename + d;
  }

  save_filename = save_filename + "_";

  int _s = second();  
  int _m = minute();  
  int _h = hour(); 

  if (_h < 10) {
    save_filename = save_filename + "0" + _h;
  }
  else {
    save_filename = save_filename + _h;
  }

  if (_m < 10) {
    save_filename = save_filename + "0" + _m;
  }
  else {
    save_filename = save_filename + _m;
  }

  if (_s < 10) {
    save_filename = save_filename + "0" + _s;
  }
  else {
    save_filename = save_filename + _s;
  }

  save_filename = save_filename + ".csv";

  data = new ArrayList();  
  myPort = new Serial(this, Serial.list()[XBee], 9600);
  myPort.buffer(30);
}

float calcV(int H, int L)
{
  int analogV = L + (256 * H);
  float v0 = analogV * 1.2 / 1023;
  
  return v0;
}

float calcG(float v0)
{
  float v = v0 * 3 - VD/2.0;
  v = v/(0.3*VD/3.0);
  
  return v;
}

void serialEvent(Serial myPort)
{
  float VD=2.9;
  if (myPort.available() <= 25) return;
        
  if (myPort.read() == 0x7E) {/*[0x7E]は10進数で126(開始)*/
    for (int i = 0; i < 18; i++) {
      int discard = myPort.read();
    }
    
    int analogXH = myPort.read();
    int analogXL = myPort.read();
    float xV = calcV(analogXH, analogXL);
    float xG = calcG(xV);

    int analogYH = myPort.read();
    int analogYL = myPort.read();
    float yV = calcV(analogYH, analogYL);
    float yG = calcG(yV);

    int analogZH = myPort.read();
    int analogZL = myPort.read();
    float zV = calcV(analogZH, analogZL);
    float zG = calcG(zV);

    data.add(new FV(xG));
    data.add(new FV(yG));
    data.add(new FV(zG));

    print("x="); print(xV); print(" [v] "); print(xG); println(" [G]");
    print("y="); print(yV); print(" [v] "); print(yG); println(" [G]");
    print("z="); print(zV); print(" [v] "); print(zG); println(" [G]");
  }
}
     
void draw()
{
}

float getData(int i)
{
  FV fv = (FV)data.get(i);
  return fv.getValue();
}

void saveData()
{
  int i, n;
  float ir = 1.0 / SAMPLE_RATE;
  n = data.size() / 3;
  String[] s = new String[n+1];
  s[0] = "\"t\",\"X\",\"Y\",\"Z\"";
  
  for (i = 0; i < n; i++) {
   s[i+1] = "" + (i*ir) + "," + getData(i*3) + "," + getData(i*3+1) + "," + getData(i*3+2);
  }
  
  saveStrings(save_filename, s);
}
        
void keyPressed()
{
  if (keyCode == ESC) {
    saveData();
    exit();
  }
}

FV.pde
class FV
{
  float v;
  FV(float val) {
    v = val;
  }

  float getValue() {
    return v;
  }
}