New file for serial data on button toggle?

edited April 2015 in Arduino

I had an earlier question on this but it wasn't very clear and I hadn't done enough homework.

I'm viewing serial data from Arduino (it will be plotted but isn't here to make the example simpler). I only want to record when something interesting is happening. Record is triggered with a mouse click on a button from Processing. The following code works great. BUT, I can only record one snippet of the data stream.

import processing.serial.*; 

Serial myPort;    // The serial port
int mills; 
String dataOutput;
int val;
int value;
PrintWriter output;

int rectX = 50; //button coords
int rectY = 400;
int rectW = 100;
int rectH = 50;
String command = "";
boolean canStart = true;
String timestamp;

void setup() { 
  size(1500, 480);
  println(Serial.list()); 
  myPort = new Serial(this, Serial.list()[2], 9600); 
  //output = createWriter("test.txt");
  //myPort.bufferUntil(lf); 
  //output = createWriter(timestamp + ".txt");
  timestamp = year() + nf(month(),2) + nf(day(),2) + "-"  + nf(hour(),2) + nf(minute(),2)+ nf(second(),2);
  output = createWriter(timestamp + ".txt");
} 
int getValue() {
  int value = -1;
  while (myPort.available () >= 3) {
    if (myPort.read() == 0xff) {
      value = (myPort.read() << 8) | (myPort.read());
    }
  }
  return value;
}
void draw() {
  background(0);

  if (canStart) {
    command = "REC";
    fill(0, 255, 0);
  } else {
    saveData();
    command = "STOP";
    fill(255, 0, 0);
  }
  noStroke();
  rect(rectX, rectY, rectW, rectH);
  fill(255);
  text(command, rectX + 30, rectY + 30);  
 }

void mousePressed() {
    if(buttonOver()){
   if(canStart){
    } else {
    output.flush(); // Writes the remaining data to the file
    output.close(); // Finishes the file
    PrintWriter output;
    }
     canStart = !canStart;
  }
}

boolean buttonOver()  {
  if (mouseX >= rectX && mouseX <= rectX+rectW && 
      mouseY >= rectY && mouseY <= rectY+rectH) {
    return true;
  } else {
    return false;
  }
}

void saveData(){
  val = getValue();
  mills= millis();
  String[] dataOutput = {val + "," + mills};
  println(dataOutput); 
  output.println(val + "," + mills); // Write the coordinate to the file 
}

I really need a new createWriter to make a new file each time the button is pressed. So I tried the following:

void saveData(){
  timestamp = year() + nf(month(),2) + nf(day(),2) + "-"  + nf(hour(),2) + nf(minute(),2)+ nf(second(),2);
  output = createWriter(timestamp + ".txt");
  val = getValue();
  mills= millis();
  String[] dataOutput = {val + "," + mills};
  println(dataOutput); 
  output.println(val + "," + mills); // Write the coordinate to the file 
}

This continually creates empty files, and doesn't get me where I want to be.

Answers

  • Answer ✓

    Potentially a hacky solution but works! Solved by closing the port, then going back to setup to establish a new connection (thereby restarting Arduino) and initialize new createWriter/file. Surprisingly fast to reestablish, I thought I might need a delay but no.

    void mousePressed() {
      if(timeMode == 1)
        timeBars[0] = mouseX;
      else if(timeMode == 2)
        timeBars[1] = mouseX;
        if(buttonOver()){
        if(canStart){
        } else {
         output.flush(); // Writes the remaining data to the file
         output.close(); // Finishes the file
         port.clear();
         // Close the port
         port.stop();
        //delay(1000);
        setup();
        }
    
         canStart = !canStart;
      }
    }
    
  • I still can't figure out the relation between restarting arduino and writing into a file! Weird!

Sign In or Register to comment.