Saving live information to excel file every half hour

Apologies for the super late reply. I was using the program manually resetting for the past few months because i could not figure out how to have it reset automatically. I made the adjustments that were suggested above however it does not reset them all to 0 at any time, it continues from the the number from the previous day. I have attached the full code below. Any assistance would be greatly appreciated, thank you in advance.

import processing.serial.*;
import java.io.FileWriter;
FileWriter output;
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);
}

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

boolean fileExists(String filename) {
  File f = dataFile(filename);
  String filePath = f.getPath();
  boolean exist = f.isFile();
  println(filePath, exist);
  return exist;
}

String machineNames = "PRESS 2, PRESS 3, PRESS 4, PRESS 5, PRESS 6, CC SAW 1, CC SAW 2, CC SAW 3, ROBOT 1, ROBOT 2, ROBOT 3, RIVETER 3, RIVETER 4, RIVETER 6, WELDER 4, WELDER 5, SYSTEM 5-8, BROACHER, HEADER\n";


int[] xbdata = new int [19];//set up array for API data from xbee
int count = 0;  //integer used to find places of data in transmission
int p2c1 = 0;
int p2c2 = 0;
int p3c1 = 0;
int p3c2 = 0;
int p4c1 = 0;
int p4c2 = 0;
int p5c1 = 0;
int p5c2 = 0;
int p6c1 = 0;  // integer used for press 6 count 
int p6c2 = 0; //set up second integer for p6 count
int time = millis();  //integer used for timing
int time2 = 0;        //integer used for timing
int cntbyte = 0;
int ccs1c1 = 0;
int ccs1c2 = 0;
int ccs2c1 = 0;
int ccs2c2 = 0;
int ccs3c1 = 0;
int ccs3c2 = 0;
int ccs4c1 = 0;
int ccs4c2 = 0;
int rob1c1 = 0;
int rob1c2 = 0;
int rob2c1 = 0;
int rob2c2 = 0;
int rob3c1 = 0;
int rob3c2 = 0;
int riv3c1 = 0;
int riv3c2 = 0;
int riv4c1 = 0;
int riv4c2 = 0;
int riv6c1 = 0;
int riv6c2 = 0;
int weld4c1 = 0;
int weld4c2 = 0;
int weld5c1 = 0;
int weld5c2 = 0;
int sys58c1 = 0;
int sys58c2 = 0;
int broachc1 = 0;
int broachc2 = 0;
int headerc1 = 0;
int headerc2 = 0;
int timePeriod = 30;  // Set the time period
int timeCount0 = -1;
int timeCount1;

Serial myPort;  // The serial port

void setup() {
  size(500,900);  //sets up size of total area
 // List all the available serial ports help find usb port to computer
  println(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, "COM3", 9600);

frameRate(1);

println(sketchPath());


}
void draw() 
{
  background(90,90);
  
 // count = 0;
 
 if ( mouseY > 10 && mouseY < 30 &&   //test for curson on leftmost character of press 6 count b 
       mouseX > 99 && mouseX < 110      //and mouse button pressed
       &&  mousePressed)                 
  {
    p2c1 = 0;
  }
  if ( mouseY > 40 && mouseY < 60 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 99 && mouseX < 110        // and mouse button pressed resets count
       &&  mousePressed)
  {
    p2c2 = 0;
  }  
  
   if ( mouseY > 150 && mouseY < 170 &&   //test for curson on leftmost character of press 6 count b 
       mouseX > 99 && mouseX < 110      //and mouse button pressed
       &&  mousePressed)                 
  {
    p4c1 = 0;
  }
  if ( mouseY > 180 && mouseY < 200 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 99 && mouseX < 110        // and mouse button pressed resets count
       &&  mousePressed)
  {
    p4c2 = 0;
  }  
 if ( mouseY > 224 && mouseY < 240 &&   //test for curson on leftmost character of press 6 count b 
       mouseX > 99 && mouseX < 110      //and mouse button pressed
       &&  mousePressed)                 
  {
    p5c1 = 0;
  }
  if ( mouseY > 255 && mouseY < 270 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 99 && mouseX < 110        // and mouse button pressed resets count
       &&  mousePressed)
  {
    p5c2 = 0;
  }  
 
   if ( mouseY > 324 && mouseY < 340 &&   //test for curson on leftmost character of press 6 count b 
       mouseX > 99 && mouseX < 110      //and mouse button pressed
       &&  mousePressed)                 
  {
    p6c2 = 0;
  }
  if ( mouseY > 294 && mouseY < 310 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 99 && mouseX < 110        // and mouse button pressed resets count
       &&  mousePressed)
  {
    p6c1 = 0;
  }  
  if ( mouseY > 423 && mouseY < 440 &&   //test for curson on leftmost character of press 6 count b 
       mouseX > 140 && mouseX < 150      //and mouse button pressed
       &&  mousePressed)                 
  {
    ccs1c2 = 0;
  }
  if ( mouseY > 394 && mouseY < 410 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 140 && mouseX < 150        // and mouse button pressed resets count
       &&  mousePressed)
  {
    ccs1c1 = 0;
  }  
  if ( mouseY > 494 && mouseY < 510 &&   //test for curson on leftmost character of press 6 count b 
       mouseX > 140 && mouseX < 150      //and mouse button pressed
       &&  mousePressed)                 
  {
    ccs2c2 = 0;
  }
  if ( mouseY > 463 && mouseY < 480 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 140 && mouseX < 150        // and mouse button pressed resets count
       &&  mousePressed)
  {
    ccs2c1 = 0;
  }  
  if ( mouseY > 563 && mouseY < 580 &&   //test for curson on leftmost character of press 6 count b 
       mouseX > 140 && mouseX < 150      //and mouse button pressed
       &&  mousePressed)                 
  {
    ccs3c2 = 0;
  }
  if ( mouseY > 533 && mouseY < 550 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 140 && mouseX < 150        // and mouse button pressed resets count
       &&  mousePressed)
  {
    ccs3c1 = 0;
  }

  
  if ( mouseY > 12 && mouseY < 30 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 392 && mouseX < 405        // and mouse button pressed resets count
       &&  mousePressed)
  {
    rob1c1 = 0;
  }
  
  if ( mouseY > 42 && mouseY < 60 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 392 && mouseX < 405        // and mouse button pressed resets count
       &&  mousePressed)
  {
    rob1c2 = 0;
  }
  
  if ( mouseY > 82 && mouseY < 100 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 392 && mouseX < 405        // and mouse button pressed resets count
       &&  mousePressed)
  {
    rob2c1 = 0;
  }
  
  if ( mouseY > 112 && mouseY < 130 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 392 && mouseX < 405        // and mouse button pressed resets count
       &&  mousePressed)
  {
    rob2c2 = 0;
  }
  
  if ( mouseY > 152 && mouseY < 170 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 392 && mouseX < 405        // and mouse button pressed resets count
       &&  mousePressed)
  {
    rob3c1 = 0;
  }
  
  if ( mouseY > 182 && mouseY < 200 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 392 && mouseX < 405        // and mouse button pressed resets count
       &&  mousePressed)
  {
    rob3c2 = 0;
  }
   
  if ( mouseY > 221 && mouseY < 240 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 409 && mouseX < 422        // and mouse button pressed resets count
       &&  mousePressed)
  {
    riv3c1 = 0;
  }
  
  if ( mouseY > 251 && mouseY < 271 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 409 && mouseX < 422        // and mouse button pressed resets count
       &&  mousePressed)
  {
    riv3c2 = 0;
  }
  
  if ( mouseY > 291 && mouseY < 310 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 409 && mouseX < 422        // and mouse button pressed resets count
       &&  mousePressed)
  {
    riv4c1 = 0;
  }
  
  if ( mouseY > 321 && mouseY < 340 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 409 && mouseX < 422        // and mouse button pressed resets count
       &&  mousePressed)
  {
    riv4c2 = 0;
  }
  
  if ( mouseY > 361 && mouseY < 380 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 409 && mouseX < 422        // and mouse button pressed resets count
       &&  mousePressed)
  {
    riv6c1 = 0;
  }
  
  if ( mouseY > 391 && mouseY < 410 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 409 && mouseX < 422        // and mouse button pressed resets count
       &&  mousePressed)
  {
    riv6c2 = 0;
  }

     
  if ( mouseY > 430 && mouseY < 450 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 399 && mouseX < 412        // and mouse button pressed resets count
       &&  mousePressed)
  {
    weld4c1 = 0;
  }
  
  if ( mouseY > 460 && mouseY < 480 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 399 && mouseX < 412        // and mouse button pressed resets count
       &&  mousePressed)
  {
    weld4c2 = 0;
  }
  
  if ( mouseY > 500 && mouseY < 520 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 399 && mouseX < 412        // and mouse button pressed resets count
       &&  mousePressed)
  {
    weld5c1 = 0;
  }
  
  if ( mouseY > 530 && mouseY < 550 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 399 && mouseX < 412        // and mouse button pressed resets count
       &&  mousePressed)
  {
    weld5c2 = 0;
  }
  
  if ( mouseY > 570 && mouseY < 590 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 409 && mouseX < 422        // and mouse button pressed resets count
       &&  mousePressed)
  {
    sys58c1 = 0;
  }
  
  if ( mouseY > 600 && mouseY < 620 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 409 && mouseX < 422        // and mouse button pressed resets count
       &&  mousePressed)
  {
    sys58c2 = 0;
  }
  
  if ( mouseY > 640 && mouseY < 660 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 390 && mouseX < 402        // and mouse button pressed resets count
       &&  mousePressed)
  {
    broachc1 = 0;
  }
  
  if ( mouseY > 670 && mouseY < 690 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 390 && mouseX < 402        // and mouse button pressed resets count
       &&  mousePressed)
  {
    broachc2 = 0;
  }
  
  if ( mouseY > 710 && mouseY < 730 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 379 && mouseX < 391        // and mouse button pressed resets count
       &&  mousePressed)
  {
    headerc1 = 0;
  }
  
  if ( mouseY > 740 && mouseY < 760 &&     // test for cursor on leftmost character of press6 count a
       mouseX > 379 && mouseX < 391        // and mouse button pressed resets count
       &&  mousePressed)
  {
    headerc2 = 0;
  }

  //Test for click on reset all square
 if ( mouseY > 732 && mouseY < 750 &&     // test for cursor on reset all square
       mouseX > 165 && mouseX < 185        // and mouse button pressed resets all counts
       &&  mousePressed)
  {

 // The action every timePeriod:
    
int hours;
hours = LocalDateTime.now().getHour();
print(String.format("tC %2d, Hr %2d, DateTime %s", timeCount1, hours, dateStamp()));
if (hours == 23 && timeCount1 == 1) {
 ResetAll();
}
  } 


  
    if (myPort.available() > 0) //data available on port if > 0
  {
    int inByte = myPort.read();  //load bytes one by one into array
   // if (xbdata[0] = 126) {
   //   count = 0;} 
   count ++;
   
   xbdata[count]=inByte;
  
  
    println(inByte);
   if (xbdata[1] != 126) {count = 0;
 }
   //   count = 0;}
    if (count > 17)  {
      count = 0;}

    if (xbdata[16]==67) 
    {
    ccs1c1 ++;
    ccs1c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    if (xbdata[16]==66) 
    {
    ccs2c1 ++;
    ccs2c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    if (xbdata[16]==65) 
    {
    p6c1 ++;
    p6c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
    if (xbdata[16]==70) 
    {
     p5c1 ++;
    p5c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
    if (xbdata[16]==71) 
    {
     p2c1 ++;
    p2c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
     if (xbdata[16]==72) 
    {
     p4c1 ++;
    p4c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
    if (xbdata[16]==68) 
    {
     ccs3c1 ++;
    ccs3c2 ++;
    //count = 0;
    xbdata[16] = 0;
    }
    
     if (xbdata[16]==50) 
    {
    rob1c1 ++;
    rob1c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
     if (xbdata[16]==51) 
    {
    rob2c1 ++;
    rob2c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
     if (xbdata[16]==52) 
    {
    rob3c1 ++;
    rob3c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
    if (xbdata[16]==42) 
    {
    riv6c1 ++;
    riv6c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
     
     if (xbdata[16]==41) 
    {
    riv4c1 ++;
    riv4c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
    if (xbdata[16]==40) 
    {
    riv3c1 ++;
    riv3c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
    if (xbdata[16]==31) 
    {
    weld4c1 ++;
    weld4c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
    if (xbdata[16]==30) 
    {
    weld5c1 ++;
    weld5c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
    if (xbdata[16]==81) 
    {
    sys58c1 ++;
    sys58c2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
     if (xbdata[16]==80) 
    {
    broachc1 ++;
    broachc2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
     if (xbdata[16]==82) 
    {
    headerc1 ++;
    headerc2 ++;
    count = 0;
    xbdata[16] = 0;
    }
    
 // time2 = millis();
 // if (time2 > (time + 1000)){
// background(200,75,80);

// For final:
  //timeCount1 = LocalDateTime.now().getMinute();
  // For faster testing:
  timeCount1 = LocalDateTime.now().getSecond();
  timeCount1 /= timePeriod;
  if (timeCount1 != timeCount0) {
    try  {
        String textLine;
        String dateString;
        String filename;
  filename = String.format("Machine_Counts_%s.csv", dateForFilename());
  
  boolean csvFileExists;  
  csvFileExists = fileExists(sketchPath() + "/" + filename);
  
dateString = dateStamp();
textLine = String.format("%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
dateString, p2c1, p3c1, p4c1, p5c1, p6c1, ccs1c1, ccs2c1, ccs3c1, rob1c1, rob2c2, rob3c1, riv3c1, riv4c1, riv6c1, weld4c1, weld5c1, sys58c1, broachc1, headerc1) ;
output = new FileWriter(sketchPath() + "/" + filename, true);
    // true = append (create if not there), 
    // false = create new (delete if already there)
   
    if (!csvFileExists) {
    output.write("DateTime," + machineNames);
  }
    
output.write(textLine);
        output.flush();
        output.close();
  }
  catch(IOException e) {
    println("It broke!!!");
    e.printStackTrace();
  }
    println(dateStamp());
    timeCount0 = timeCount1;
  }  


}
      textSize (20);
      
      text ("Press 2a",10,30);
      text ("Press 2b" ,10, 60);
      fill(245,71,20);
      text (p2c1,100,30);
      text (p2c2,100,60);
      fill (230);
      
      text ("Press 3a",10,100);
      text ("Press 3b",10,130);
      fill(245,71,20);
      text (p3c1,100,100);
      text (p3c2,100,130);
      fill (230);
      
      text ("Press 4a",10,170);
      text ("Press 4b",10,200);
      fill(245,71,20);
      text (p4c1,100,170);
      text (p4c2,100,200);
      fill (230);
      
      text ("Press 5a",10,240);
      text ("Press 5b",10,270);
      fill(245,71,20);
      text (p5c1,100,240);
      text (p5c2,100,270);
      fill (230);
      
      text ("CC Saw #1a" ,10, 410);
      text ("CC Saw #1b" ,10, 440);  
      fill(245,71,20);
      text (ccs1c1,140,410);
      text (ccs1c2,140,440);
      fill (230);
     
      text ("CC Saw #2a" ,10, 480);
      text ("CC Saw #2b" ,10, 510);  
      fill(245,71,20);
      text (ccs2c1,140,480);
      text (ccs2c2,140,510);
      fill (230);
      
      text ("CC Saw #3a" ,10, 550);
      text ("CC Saw #3b" ,10, 580);  
      fill(245,71,20);
      text (ccs3c1,140,550);
      text (ccs3c2,140,580);
      fill (230);
      
      text ("Press 6a" ,10, 310);
      text ("Press 6b" ,10, 340);  
      fill(245,71,20);
      text (p6c1,100,310);
      text (p6c2,100,340);
      fill (230);
      
      text ("Robot1a",300,30);
      text ("Robot1b" ,300, 60);
      fill(245,71,20);
      text (rob1c1,395,30);
      text (rob1c2,395,60);
      fill (230);
      
      text ("Robot2a",300,100);
      text ("Robot2b" ,300,130);
      fill(245,71,20);
      text (rob2c1,395,100);
      text (rob2c2,395,130);
      fill (230);
      
      text ("Robot3a",300,170);
      text ("Robot3b" ,300,200);
      fill(245,71,20);
      text (rob3c1,395,170);
      text (rob3c2,395,200);
      fill (230);
      
      text ("Riveter3a",300,240);
      text ("Riveter3b",300,270);
      fill(245,71,20);
      text (riv3c1,410,240);
      text (riv3c2,410,270);
      fill (230);
      
      text ("Riveter4a",300,310);
      text ("Riveter4b",300,340);
      fill(245,71,20);
      text (riv4c1,410,310);
      text (riv4c2,410,340);
      fill (230);
      
      text ("Riveter6a",300,380);
      text ("Riveter6b",300,410);
      fill(245,71,20);
      text (riv6c1,410,380);
      text (riv6c2,410,410);
      fill (230);
      
      text ("Welder4a",300,450);
      text ("Welder4b",300,480);
      fill(245,71,20);
      text (weld4c1,400,450);
      text (weld4c2,400,480);
      fill (230);
      
      text ("Welder5a",300,520);
      text ("Welder5b",300,550);
      fill(245,71,20);
      text (weld5c1,400,520);
      text (weld5c2,400,550);
      fill (230);
      
      text ("System58",300,590);
      text ("System58",300,620);
      fill(245,71,20);
      text (sys58c1,410,590);
      text (sys58c2,410,620);
      fill (230);
      
      text ("Broacher",300,660);
      text ("Broacher",300,690);
      fill(245,71,20);
      text (broachc1,390,660);
      text (broachc2,390,690);
      fill (230);
      
      text ("Header",300,730);
      text ("Header",300,760);
      fill(245,71,20);
      text (headerc1,380,730);
      text (headerc2,380,760);
      fill (230);
      
       text ("Reset ALL",25,750);
      rect (165,732,20,20); 
      
  

      
      
     // text ("Reset ALL",500,30);
     // rect (640,12,20,20); 
       
    
   //  text (mouseX,25,700);   //display horizontal pixel position of cursor
   //   text (mouseY, 25,720);  //display vertical pixel position of cursor
  }
  
  void ResetAll() {
    p5c1=0;
    p5c2=0;
    p2c1=0;
    p2c2=0;
    p6c1 = 0;
    p6c2 = 0;
    ccs1c1 = 0;
    ccs1c2 = 0;
    ccs2c1 = 0;
    ccs2c2 = 0;
    ccs3c1 = 0;
    ccs3c2 = 0;
    p4c1 = 0;
    p4c2 = 0;
    rob1c1 = 0;
    rob1c2 = 0;
    rob2c2 = 0;
    rob2c1 = 0;
    rob3c1 = 0;
    rob3c2 = 0;
    riv3c1 = 0;
    riv3c2 = 0;
    riv4c1 = 0;
    riv4c2 = 0;
    riv6c1 = 0;
    riv6c2 = 0;
    weld4c1 = 0;
    weld4c2 = 0;
    weld5c1 = 0;
    weld5c2 = 0;
    sys58c1 = 0;
    sys58c2 = 0;
    broachc1 = 0;
    broachc2 = 0;
    headerc1 = 0;
    headerc2 = 0;
    
  }
1 Like