# Saving live information to excel file every half hour

**URL:** https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061
**Category:** Electronics (Arduino, etc.)
**Created:** [October 25, 2023, 7:47pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061 "2023-10-25T19:47:01Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 1, 2023, 8:16pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/21 "2023-11-01T20:16:20Z")

</div>

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/d/edeb556d8f03e2c2b6e7e48635da0dda2e21d47b.png)

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 1, 2023, 8:25pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/22 "2023-11-01T20:25:04Z")

</div>

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.

```auto
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?

---

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 1, 2023, 8:48pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/23 "2023-11-01T20:48:07Z")

</div>

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()”?

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 1, 2023, 8:52pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/24 "2023-11-01T20:52:11Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 1, 2023, 8:59pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/25 "2023-11-01T20:59:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 1, 2023, 9:40pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/26 "2023-11-01T21:40:52Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 1, 2023, 9:46pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/27 "2023-11-01T21:46:57Z")

</div>

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.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/8/9814cde641af93eb7e7582db29f4ed22f5c52fc2.png)

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 1, 2023, 10:01pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/28 "2023-11-01T22:01:06Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 1, 2023, 10:24pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/29 "2023-11-01T22:24:41Z")

</div>

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

```auto
void setup() {
  frameRate(1);
}

```

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

```auto
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;
  }  
}

```

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 1, 2023, 10:33pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/30 "2023-11-01T22:33:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 1, 2023, 10:50pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/31 "2023-11-01T22:50:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 1, 2023, 11:26pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/32 "2023-11-01T23:26:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 2, 2023, 4:25pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/33 "2023-11-02T16:25:39Z")

</div>

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/f/9fe95e1b0a060289890ecf8dece9c6ef338a2050.png)

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 2, 2023, 4:34pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/34 "2023-11-02T16:34:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 3, 2023, 3:49pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/35 "2023-11-03T15:49:58Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 3, 2023, 4:23pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/36 "2023-11-03T16:23:26Z")

</div>

Indenting is a way of arranging the code to make it easier to understand the function. Please see the help for '“[if](https://processing.org/reference/if.html)” 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.

---

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 3, 2023, 5:33pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/37 "2023-11-03T17:33:50Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 3, 2023, 9:18pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/38 "2023-11-03T21:18:51Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jason](https://discourse.processing.org/u/Jason)
#### Post date: [November 3, 2023, 10:24pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/39 "2023-11-03T22:24:23Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [November 4, 2023, 5:22pm UTC](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061/40 "2023-11-04T17:22:42Z")

</div>

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.

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

```

and this just before you open the file:

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

```

[Previous page](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061.md?page=1)

[Next page](https://discourse.processing.org/t/saving-live-information-to-excel-file-every-half-hour/43061.md?page=3)
