import processing.serial.*;

Serial myPort;
String dataStr;
PrintWriter output;

int count = 1;

int x;
int values[] = new int[300];

Date tmpTime = new Date("2019/06/24 10:10:03");

String showvalues;

void setup(){
  
  size(800,600);
  frameRate(30);
  
 
  
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[3],9600);
  //myPort.clear();
 output = createWriter("sensordata.csv");
 pixelDensity(displayDensity());
 
 
}

void draw(){
 
  
}

void serialEvent(Serial p){

    if (p.available() > 0) {
    try {
      String input = p.readStringUntil('\n');
      if (input != null) {
        
         output.print(input);
         print(input);
         
       }
    } catch (RuntimeException e) {
    }
  }
  
}

void keyPressed() { 
  if(key == 'a'){
    output.flush(); //残りを出力する
    output.close(); // ファイルを閉じる
    exit(); // 終了
  }
  
  if(key == 'r'){
    myPort.write("r");
  }
}
