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:
The output doesn’t get saved every minute (only the initial minute gets saved)
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?
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:
// (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