Saving live information to excel file every half hour

I pasted it here and it seemed to run. Now the “example” text document has a bunch of 0s and an “abc”

It will have real values when the serial stuff arrives?

For the date, the example I gave a few posts back was not ideal as it’s normal Java not Processing. I’ve modified it. Put this in a separate sketch and run.

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;  

String dateStamp() {
 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
 LocalDateTime now = LocalDateTime.now();
 return dtf.format(now);
}

void setup() {
  String dateString;
  dateString = dateStamp(); 
  println(dateString);
  exit();
}

Then think about copying pieces into your main sketch, piece by piece as I said.
1 - the imports.
2 - the dateStamp function.
3 - dateString = dateStamp();

And it works?

1 Like

I have added the imports up at the top and it seems to run still.

Which section would i be adding the rest of the code? The “String dateStamp()” line as well as the “void setup()”?

Now you need the whole function dateStamp. Put it somewhere near the top of your sketch, after the imports.

String dateStamp() {
 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
 LocalDateTime now = LocalDateTime.now();
 return dtf.format(now);
}

It won’t do anything because nothing is calling it, but we need to add it and see that nothing else is messed up.

1 Like

Ok, i have added this at the top just below the imports and it still seems to run.

Now back to my list of additions in post 22, you need item 3. In place of, or dateString = “abc”;
Is it still printing lots of times/sec? we haven’t done a timebase yet.

1 Like

Ok, i now just replaced the “dateString = “abc”;” with your item in line 3 and it ran.

Here is a screenshot of the example file.

1 Like

I didn’t alter the date-time format from where I copied the example I don’t know if Excel likes that format. You can alter the format string. Try different options. I know Excel accepts dd-MMM-yyyy hh:mm:ss.

Now we need to print every 30 mins, not n times/sec. Try this in a separate program.

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;  


String dateStamp() {
  // return the date in specified format.
  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
  LocalDateTime now = LocalDateTime.now();
  return dtf.format(now);
}

int timePeriod = 30;  // Set the time period
int timeCount0 = -1;
int timeCount1;


void setup() {
  frameRate(1);
}

void draw() {
  // For final:
  //timeCount1 = LocalDateTime.now().getMinute();
  // For faster testing:
  timeCount1 = LocalDateTime.now().getSecond();
  timeCount1 /= timePeriod;
  if (timeCount1 != timeCount0) {
    // The action every timePeriod:
    println(dateStamp());
    timeCount0 = timeCount1;
  }  
}

Run it and it should print the time every 30 seconds. For 30 minutes comment out the line with getSecond, and un-comment the line with getMinute.

How to merge it into the main program? Not much to do as most is there already.

  • imports - already.
  • those 3 ints, copy to the top of your sketch, just after all your values are declared.
  • everything inside the draw function should be copied to where you are doing the action for output.write.
  • move your lines that prepare the data for the file, and the actual output.write, to where I say "The action every timePeriod.

Would i be copying the code below into the “void setup()” section of the code?

void setup() {
  frameRate(1);
}

and the below code into the “void draw()” section?

void draw() {
  // For final:
  //timeCount1 = LocalDateTime.now().getMinute();
  // For faster testing:
  timeCount1 = LocalDateTime.now().getSecond();
  timeCount1 /= timePeriod;
  if (timeCount1 != timeCount0) {
    // The action every timePeriod:
    println(dateStamp());
    timeCount0 = timeCount1;
  }  
}

You already have the setup and draw functions in your sketch. Every Processing sketch (almost) has just one of each of those. From my Setup you could copy that frameRate line to yours. No need to run the draw at the default 60 times/sec. From my Draw you need all the contents (i.e. not the Draw line, and not the last }, but everything else.

1 Like

I added the functions to the sections as you suggested. It still runs, however, it is adding an new line every second instead of every 30 seconds.

See what I said in post 28 about moving the ‘write’ etc to the ‘action every time period’.

1 Like

Just for clarification, would I be moving the lines 703 and 704 to the section where you say “The action every timePeriod”?

On looking at it, I can see it needs to be more than that, lines 699 to 710. Once you’ve done that you should try to fix the indenting. After { left margin goes in 2 characters (IMO, others will not agree). } goes out 2 chars. You can split statements on more than 1 line, but not between “text string”. Suggest you break what is line 703 in the image after the first comma. Java does not care about leading space and line endings, we format it to make it clear.

1 Like

Apologies for the late reply, being a new user my daily replies are limited and I was locked out for 24 hours.

Awesome, I have moved the lines as suggested and now it prints to the text file every 30 seconds. I am a bit confused regarding the indenting though, if you could please give me an example.

Indenting is a way of arranging the code to make it easier to understand the function. Please see the help for '“if” The action inside the ‘if’ condition (one line) is indented. Also File, Examples, Basics, Control, Embedediteration. See how the contents of each ‘for’ loop is indented.

1 Like

Ok thank you for the information.

Would there be a way for the program to create a new file everyday instead of continuously adding onto the same file? and would there be a way to add each 30 minute increment onto a new line? Right now when i try to import the text file into excel everything is in columns, however, i would like it to add each time on a seperate row.

Both possible. See in my example in post 4. On the output.write…\n . We’ve lost that \n along the way. Put it back in and you should get each entry on a separate line.

Just to prove something, while the current sketch is running, try to rename the output file. I think you can’t.

For the ‘new daily file’ we need to do two things. Make it open and close the file each time. At the moment the sketch opens the file when it starts, closes only when you stop the program. See that example again, it opens,write,closes. In your sketch move the line “… output = new File…” to just before the write (as the example), and put the ‘close’ line in (as the example). Run the sketch, should be the same, but you can rename the output file while it’s running.

Next, instead of using a fixed filename, we need to make a variable string for the filename, and build the date into it e.g. Output_20231103.csv.

More detail soon if you need it, start trying those.

1 Like

I have added the “\n” and it is now creating a new line as i wanted.

I have moved the “… output = new File…” line to just before the write as the example and added a “close” line just below the “flush” line as the example and it is still running good. One question i have is will this close the file at 11:59pm and create a new file at 12am?

Now i just need to figure out how to have the dates as the filenames.

Well done. It closes the file after writing each line, after every half hour. It opens the file when it needs to write a line. I stopped using am/pm 40 years ago, too confusing and more characters to write. So I’m guessing you are in USA. (I’m in UK). New file should be created at 00:00:00.010. (USA ‘Military Time’). Now we need three things. A string variable to hold the filename, a calculation of that filename, and use the name to open the file.

Put this function at near the top of the program just after the dateStamp function.

String dateForFilename() {
 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
 LocalDateTime now = LocalDateTime.now();
 return dtf.format(now);
}

and this just before you open the file:

  String filename;
  filename = String.format("Machine_Counts_%s.csv", dateForFilename());