Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Export CSV data to Excel in Separate Columns

$
0
0
Hello,

I'm attempting to export serial data to Excel into different columns. Right now, my code allows for all the data to be exported as a .csv file into Excel with all data in the A Column; however, I'm wanting to import timestamps in Column A with Analog values in Column B. Any help would be greatly appreciated!

By the way, my current code is a modified version from the Arduino Cookbook (I'm very new to Processing and programming in general):

  1. import processing.serial.*;
    import java.text.*;
    import java.util.*;

    PrintWriter output;
    DateFormat fnameFormat = new SimpleDateFormat("yyMMdd_HHmm");
    DateFormat  timeFormat = new SimpleDateFormat("hh:mm:ss:SSS");

    String fileName;

    Serial myPort;        // Create object from Serial class
    short portIndex = 2;  // select the com port, 0 is the first port
    char HEADER = 'H';

    void setup()
    {
      size(200, 200);
      // Open whatever serial port is connected to Arduino.
      String portName = Serial.list()[portIndex];
      println(Serial.list());
      println(" Connecting to -> " + Serial.list()[portIndex]);
      myPort = new Serial(this, portName, 115200);
      Date now = new Date();
      fileName = fnameFormat.format(now);
      output = createWriter(fileName + ".csv"); // save the file in the sketch folder
    }

    void draw()
    {
      int val;
      String time;

      if ( myPort.available() >= 15)  // wait for the entire message to arrive
      {
        if( myPort.read() == HEADER) // is this the header
        {
          String timeString = timeFormat.format(new Date());
          println("Message received at " + timeString);
          val = readArduinoInt();
          output.println(timeString);
          // header found
          // get the integer containing the bit values
           val = readArduinoInt();
          // print the analog value
          for(int i=0; i < 1; i ++){
            val = readArduinoInt();
            println(val);
            output.println( val);
          }
        }
      }
    }

    void keyPressed() {
      output.flush(); // Writes the remaining data to the file
      output.close(); // Finishes the file
      exit(); // Stops the program
    }

    // return the integer value from bytes received on the serial port
    // (in low,high order)
    int readArduinoInt()
    {
      int val;      // Data received from the serial port

      val = myPort.read();          // read the least significant byte
      val =  myPort.read() * 256 + val; // add the most significant byte
      return val;
    }


Viewing all articles
Browse latest Browse all 1768

Trending Articles