Saving live information to excel file every half hour

The following code was written by someone a few years ago for our company. What it does is display the counts of parts that are coming off of some of our machines. What i need to be able to do is have those numbers save into an excel file with the corresponding machine names. I need this to save to the file every 30mins on a new row each time. I would also like there to be an option to reset the counter at 2 different times each day, once at 6am and once at 3:30pm. I do have a fair bit of tech background and knowledge but not any coding experience. Any help would be greatly appreciated. See screenshot for what the program looks like.

import processing.serial.*;
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;


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);

  
}
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)
  {
    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;

  } 


  
    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);
}
      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
  }
1 Like

Hello @Jason, Welcome to the forum. Interesting to see your application. “an Excel file” - Excel has it’s own format of files .xls and .xlsx, I don’t know of any interface between Processing and those two. Excel will also open a csv (Comma Separated Variable) file. That’s a plain text file, editable by Notepad and many other editors. Processing can create a csv file, and append a line on each half hour.

Were you thinking that Excel can have the file open, and Processing appends the new lines? That can’t be done directly, because Excel will open the file exclusively and Processing won’t be able to write to it. (Maybe someone knows a way round that.) You make a copy of the file at anytime, and open the copy with Excel.

Do you want one file per machine, slowly growing, forever? I quite often build the date into the filename e.g.
Machine_202310.log so that a new file is started each month.

I have an example somewhere, back soon.

(You might like to edit your first post, select the program example, and click the </> - it will format it nicely, easier for anyone to read and copy.)

2 Likes

Hi @RichardDL, than you for your reply. CSV file would be what I’m after. I don’t need to have the csv file open all the time but what I would require is having it update every half hour. Preferably I would like to have have all machines in 1 file with a date and timestamp every half half hour however if that is not an option then I could also work with multiple folders and files for each machine slowly growing forever and create my own summary spreadsheet to use those files as a dataset.

Thank you for letting me know about the </> feature. I have now edited the post to make it easier to read.

I hope I answered all your questions.

@Jason, thanks for formatting the code.

Yes you can have datetime,value1,value2,… I forgot to say that a frequent format of csv files is to have the first line as a header, e.g. Time,Name1,Name2,…

Here’s an append file example, I copied from https://forum.processing.org/two/discussion/561/easiest-way-to-append-to-a-file-in-processing.html (sketchPath needs () since then).

Note the use of “/” in the path. Windows normally uses “\” but accepts “/”. (If you use “\” you will run into Java thinking it’s special and you have to use “\\”. Keep away. :slight_smile: )

import java.io.FileWriter;

println(sketchPath());

try {
  FileWriter output = new FileWriter(sketchPath() + "/example.txt", true);
    // true = append (create if not there), 
    // false = create new (delete if already there)
  output.write("a; b; c; this; that\n");
  output.flush();
  output.close();
}

catch(IOException e) {
  println("It broke!!!");
  e.printStackTrace();
}

exit();
1 Like

dataPath(): dataPath("example.txt")

2 Likes

@RichardDL, Thanks for the info.

Sorry but I’m just a bit confused. So can add this code into my existing code and it should then start creating the files which I require for my machines? Apologies I’m completely new with coding.

No prob, I would make a safe copy of the current code, just in case. Are you working on another computer? to leave the live program running, until your new one is ready? Or work in a copy with different name, it will work normally but have zero in all values. Yes, you need to merge the example into your working program.

Look at the code I posted:

  • copy the ‘import’ line to yours and run it - it still runs as before.
  • copy the all the rest to the after your “myPort = new…”
  • in what you’ve just copied, need to
    • move the “write” and “flush” lines to the end of the draw function, after “rect (165,732,20,20);”
    • delete the “close” line
    • delete the “exit” line.

If you run it now, it should create the file, and write lots of example text into it, much too often, but let’s get that working. Run it for e.g. 10 seconds, stop, look at example.txt.

Developing like this could be very slow, you should also see e.g. YouTube “get started with processing”, also see File, Examples…, also Help, Reference.

I copied my existing code to a text file to keep it safe. I am working on the same computer but we just are not running the program at the moment because its too inefficient to key in the numbers 1 by 1 every so often.

I copied the import line and ran it and it still ran.

When i copied the rest after the “myPort = new…”, I deleted the “close” and “exit” lines and the “write” and “flush” lines and pasted the “write” and “flush” lines to the end of the draw function.

When i go to run i get the following message “Cannot find anything named “output”.”

I have repasted my edited code below.

import processing.serial.*;
import java.io.FileWriter;

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;


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);

println(sketchPath());

try {
  FileWriter output = new FileWriter(sketchPath() + "/example.txt", true);
    // true = append (create if not there), 
    // false = create new (delete if already there)

}

catch(IOException e) {
  println("It broke!!!");
  e.printStackTrace();
}

  
}
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)
  {
    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;

  } 


  
    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);
}
      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); 
      
  output.write("a; b; c; this; that\n");
  output.flush();
      
      
     // 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
  }

Im also not sure if the works “output” and “create” should be colored or not like they are in the code pasted here. (see image below)

image

Alright, 2 probs. Output needs to be declared at the top of the program. It wants an exception handler around the write,flush.

Put this line at the top e.g. after the 2 import:
FileWriter output;

In setup, change the ‘output’ line to (we’ve remove the def of the var):
output = new FileWriter(sketchPath() + "/example.txt", true);

In draw, where you are writing to file, replace write and flush lines with:

  try {
        output.write("a; b; c; this; that\n");
        output.flush();
  }
  catch(IOException e) {
    println("It broke!!!");
    e.printStackTrace();
  }

Awesome, it ran with no errors. I have attached an image below of what the ‘example’ file produced.

image

1 Like

@RichardDL, If you could please advise me now on how I can get this file to display the numbers from the machines with the times that would also be greatly appreciated.

Hi @Jason, can’t give full attention now, 2 hours later maybe. See dates. Put the result in dateString. For the values, something like:

String textLine;
textLine = String.format("%s,%d,%d,%d",dateString, ccs1c1, etc) ;
output.write(textLine);

See String.format in help or google. Play with these things in small separate test program.

1 Like
1 Like

No worries @RichardDL, there is no rush. Whenever you have time is totally good, i am still learning so it does take me some time as well to understand some of the language.

@Jason, How are things? With all those examples you have all the pieces, and it’s working?

Hi @RichardDL, I was actually a bit confused as to what part of the example code I was going to paste into my code and where to paste it.

This is a very common situation. You have a program that does something, and you want to add a feature. You research the new function (Examples, Help, Google, Forum), and make a small test program that demonstrates the new function. Now you need to merge parts of the test program into the working one.

First make a safe copy of the working program, just in-case you make a complete mess and want to go back. I often do the merge in several steps, running it after each addition. e.g. imports, variable declarations, new functions, finally the calling the new functions. Running it after each addition makes locating the errors easier.

For you, let’s make the values right, that’s very simple, hardly needs the test program. See Java String.format See ‘Method Example 2’ . Copy my 3 line example and replace the 1 output.write line in the current program.

In the format line, you need as many %d, as you want values on your line.
Where I’ve written ccs1c1, etc. you need all your variable names separated by commas.

Easy?

1 Like

Thanks for your patience @RichardDL, I have replaced the output line with the 3 line example. i am getting the following error “The variable “dateString” does not exist”.

Ah, we haven’t done that part yet. Put this just above where you need it.

String dateString;
dateString = "abc";

By “where you need it” are you referring to right above the 3 lines I previously pasted? or at the top of the whole code?