[SOLVED] Processing with Arduino [How to Save Serial output every 'x' hour?]

Hello I’m working on an Arduino project and using Processing to catch serial data from Arduino (data such as timestamp and input/output data). I’m trying to automate the saving process so that I can save the data every hour (or every ‘x’ hour)

Here is my code so far.

import processing.serial.*;    // import the Processing serial library
PrintWriter output;  

Serial myPort; 

String [] months = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", 
"Aug", "Sept", "Oct", "Nov", "Dec"} ;   

long time = 0;    // initialize variable for the current time since program started
String value;// initialize variable for serial data from Arduino
String am_pm;

// time variables (year, month, day etc.) 
int y = 0;
int mon = 0;
int d = 0;
int h = 0;
int m = 0;
int s = 0;
int prev_min = 0;

String title = "subject1.txt";

// * * * * * * * * * * * * * * * S E T U P * * * * * * * * * * * * * * *

void setup() {
  size(200,200);
  
  String portName = Serial.list()[3];  // change every time different port is used
  
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil('\n');  // would I need this statement?
  
  y = year();
  mon = month();
  d = day();
  h = hour();
  m = minute();
  s = second();

// AM/PM functionality

  if (h > 11) {
    h = h - 12; 
    am_pm = "PM";
  }
  else {
    am_pm = "AM";
  }
}

// * * * * * * * * * * * * * * * L O O P  * * * * * * * * * * * * * * *

void draw() {
         record();
 }

void record() {
  
if (prev_min != minute()) {
     
     output = createWriter(y + "/" + months[mon-1] + " " + d + "th " + h + "."
   + m + am_pm + "_" + title);

     prev_min = minute();      
     println(prev_min);  // for debugging purposes
   }
   
 if ( myPort.available() > 0) {  // If data is available,
  value = myPort.readStringUntil('\n'); // read until new line carriage 
  
  if (value != null) {
   // prints to console
   println(value);
   output.print(value);
  }
 }
}
   
void keyPressed(){
  
  output.flush();
  output.close();
  println("End Line");
  exit();
  }
 
  

** Note: I’ve stated every ‘x’ hour on the title because that’s what I eventually want to do but am using every minute for debugging purposes

Current problem is that:

  1. The output doesn’t get saved every minute (only the initial minute gets saved)
  2. When I press a key to exit out, the values are overwritten: meaning that the values from the most recent minute are only recorded as final output (ie: if I started recording from minute 10 to minute 15, only values from minute 15 are shown

I feel like I’m putting my conditional in an incorrect location…But would someone be able to help me out?
Also would there be other objects that I can use other than ‘Printwriter’ that would make this task easier?

1 Like

Another way is to use a String array and save the data at the end calling saveStrings. If you are saving each hour, what you could do is to load the data with loadStrings, add your new value by appending to the array obtained from loadStrings, then call saveStrings. You have to do this as there is no way to append to a file in Processing. You have to load the file and then save it again with the new or updated values.

For data folder creation, you can check how Processing manages the folder stamp creation when it creates new files using the Calendar object:

processing/app/src/processing/app/Base.java at 77e513ba07f440299b5c314b9aa190e648775647 · processing/processing · GitHub

Kf

1 Like

I see thanks for the reply Kf!
I appreciate it!

I actually was able to figure out this particular problem - I’ll post the code in a separate reply

Solution Code

// (saves data and writes to 'output' instance every minute

import processing.serial.*;    // import the Processing serial library
PrintWriter output;  

Serial myPort; 

// String [] months = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"} ;   

long time=0;    // initialize variable for the current time since program started
String value;// initialize variable for serial data from Arduino

int y = 0;
int mon = 0;
int d = 0;
int h = 0;
int m = 0;
int s = 0;
String am_pm;

String title = "subject_1.txt";

int prev_min = minute();

// * * * * * * * * * * * * * * * * S E T U P * * * * * * * * * * * * * *

void setup() {
  size(200,200);
  
  String portName = Serial.list()[3];  // change every time different port is used
  // look it from Arduino Side (simple indexing from 0) 
  
  myPort = new Serial(this, portName, 9600);
  myPort.bufferUntil('\n');  // would I need this statement?
  
  // "/" makes the directory: in this example, creates a directory named "2019" 
  // output format: mmddyy_hhmm(am/pm)_mouse_#.txt  (Ex: 020419_1234pm_mouse_1.txt)
  // first instance of output: 
  
  output = createWriter(year() + "/" + String.format("%02d", month()) + String.format("%02d", day()) + String.format("%04d", year()) + "_" + 
  String.format("%02d", hour()) + String.format("%02d", minute()) + (hour() > 11? "pm ":"am ") + title);
   
}

// * * * * * * * * * * * * * * * * L O O P * * * * * * * * * * * * * *

void draw()
{
 record();
}

// * * * * * * * * * * * * * * * * F U N C T I O N * * * * * * * * * * * * * *

void record() {

  if (myPort.available() > 0) {  // If data is available,
    value = myPort.readStringUntil('\n'); // read until new line carriage 
  

    if (value != null) {
    
    // prints to console
      println(value);

      // creates output instance every minute: 
      if (prev_min != minute()) {
     
        output.close(); // close the first instance --loops around--> close the next instances 
        
        output = createWriter(year() + "/" + String.format("%02d",month()) + String.format("%02d",day()) + String.format("%04d",year()) + "_" + 
                  String.format("%02d", hour()) + String.format("%02d", minute()) + (hour() > 11? "pm ":"am ") + title);
        
        prev_min = minute();       

      }
      output.print(value); 
      output.flush(); 
    
  } // end of (value != null)
 } // end of (myPort.available() > 0)
} // end of function
  
// just to exit out of the program: 
void keyPressed(){
  
  output.flush();
  output.close();
  println("End Line");
  exit();
  }

Hope this helps anybody who’s having trouble with this particular problem

2 Likes
1 Like