Simple loop question
in
Programming Questions
•
6 months ago
Hi everyone,
I'm new at processing, and as I was looking through a really cool sample script (see below), and I had a quick question. Basically, this script writes 20 lines of serial output into a file, and then it stops.
My question: how I can I do so that the script basically loops continuously after it closes the file (i.e.: it writes a file with 20 lines, and then starts again by creating a new file with new 20 lines, etc etc), as opposed of just exiting the program?
I'm sure it is simple, but I can't figure out exactly the syntax of how to do it... Any suggestions would be suuuper helpful!
Thanks!!
Luc
I'm new at processing, and as I was looking through a really cool sample script (see below), and I had a quick question. Basically, this script writes 20 lines of serial output into a file, and then it stops.
My question: how I can I do so that the script basically loops continuously after it closes the file (i.e.: it writes a file with 20 lines, and then starts again by creating a new file with new 20 lines, etc etc), as opposed of just exiting the program?
I'm sure it is simple, but I can't figure out exactly the syntax of how to do it... Any suggestions would be suuuper helpful!
Thanks!!
Luc
- import processing.serial.*;
PrintWriter output;
Serial myPort;
String myData = null;
String[] Data = new String[5000];
int n = 0;
int m = 0;
void setup() {
println(Serial.list());
String portName = Serial.list()[0];
// change [0] for the Arduino serial port
myPort = new Serial(this, "COM3", 57600);
myPort.bufferUntil('\n'); // SerialEvent for new line
myPort.clear();
String Name = "Data_"+str(month())+"-"+str(day())+"-"+str(year())
+"_"+str(hour())+"."+str(minute())+".txt";
// Create file to save the captured data
output = createWriter(Name);
}
void draw() {
while ( myPort.available () > 0 ) {
myData = myPort.readStringUntil('\n');
if ( myData != null ) {
Data[n] = myData;
n++;
}
}
while (m < n) {
if ( Data[m] != null) {
output.print(Data[m]);
m++;
output.flush();
}
}
if ( m == n ) {
if ( m >= 20 ) { // number of data lines in 3.1 seconds.
// output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
}
}
1