Problem with saving data from Arduino

Hello,

I need to save incoming data from Arduino sent in this format at a serial speed of 57600:
The data are all int type.

        Serial.print(drive, DEC);
        Serial.print(" ");
        Serial.print(distanceAry[index], DEC);
        Serial.print(" ");
        Serial.print(pressureAry[index], DEC);
        Serial.print(" ");
        Serial.print(prSwitchAry[index], DEC);
        Serial.print(" ");
        Serial.println(tempAry[index], DEC);

Here is my processing program:

    import processing.serial.*;
    PrintWriter output;
    Serial myPort;
    String myData = null;
    String[] Data = new String[5000];
    int numData = 0;
    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, "COM5", 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 ( myPort.available() <= 0) {
        if ( Data[m] != null) {
          output.print(Data[m]);
          m++;
        }
      }
    } 
    void keyPressed() {
      output.flush(); // Writes the remaining data to the file
      output.close(); // Finishes the file
      exit(); // Stops the program
    }

The Arduino sends data when a push button is activated and it sends the data described above for 3.1 seconds. The data in the arrays was captured every 5 ms. (200Hz). The total number of data sequence is 620 rows and in each row there are 5 data.
Here is the problem I have:
First I run the processing program, then I press the push button. The TX light is on for a short time (~3.1 seconds).
Now my code says, if I press any key on keyboard, flush/close the file, and terminate the program, right?... This does not work, and I have to stop the program by clicking the Stop Button in the processing window.
Here is the weird thing: After I close the program, I only have 2.8 seconds of data in the output file. where did the other 300 ms go?
I really appreciate it if you can help me understand this, or you have some idea on how to fix this.
Thanks

first please post your whole Arduino code (or a stripped version that shows the problem.
second 57600 baud is the least favorite baud rate IIRC.

My assumption is that you do not synchronize the start and stop of the data stream over the serial . If you look at an XML file you see al those < and > to synchronize the parsing of the stream.
So processing should wait until she gets the start byte before capturing data.

It's a Processing question not an Arduino one, so you're asking in the wrong place. However, that logic looks horrible. In the second loop inside draw() you will spin waiting for Data[ m ] to become non-null, but there is nothing writing to it so that will never happen. Draw() will sit in the first loop until it manages to empty the serial buffer, drop into the second loop and stay there until you kill the program. Your program is not able to respond to key presses because it is stuck in Draw().

Suggest you change the structure to something like this (untested):

// handle input port and output file, giving priority to the input port
if( myPort.available () > 0 )
{
        myData = myPort.readStringUntil('\n');
        if ( myData != null )
        {
          Data[n] = myData;
          n++;
        }
}
else
{
      if(m < n)
      {
              if ( Data[m] != null)
              {
                      output.print(Data[m]);
                      m++;
              }
       }
}

I'd prefer to see Data structured as a circular buffer and array bounds checking, but as long as your Processing application never needs to deal with more lines than the size of your Data array in any given execution then something along the lines given above should work.

Hi peter,

I understand your point and logically it sound right, but I after the changes I dont have any data in the text file. At least, before I had 2.8 sec out of 3.1 sec of data.
Any more ideas you have on how to fix this.

Thanks

How is the keypressed() function called when you type something on your keyboard!.

Mark

keypressed() is a built in function in Processing. You dont need to call it.

FardinB:
Hi peter,

I understand your point and logically it sound right, but I after the changes I dont have any data in the text file. At least, before I had 2.8 sec out of 3.1 sec of data.
Any more ideas you have on how to fix this.

Thanks

Post your (corrected) code.