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

TimeStamping Saved Data

$
0
0
Hello all,

I'm extremely new to Processing (and programming in general). My goal in learning to program is to save acquired signal from an arduino due to a file so that several readings can be made, which will eventually help my research group be able to identify pollutants in the air. Pretty cool, right? Well, I've been using the Arduino Cookbook with the example provided there to test out if this is going to work. (This code is provided below.) The problem that I have been facing is the timestamping, and really just the whole data saving. The error that I receive is "Cannot find a class or type named 'DateFormat'". Once again, I'm very new to Processing; please be gentle . Also, if anyone know is timestamping can be done in microseconds or milliseconds, that would be great to know too! Thanks so much for any help given!
  1. /*
     * ReceiveMultipleFieldsBinaryToFile_P
     *
     * portIndex must be set to the port connected to the Arduino
     * based on ReceiveMultipleFieldsBinary, this version saves data to file
     * Press any key to stop logging and save file
     */

    import processing.serial.*;


    PrintWriter output;
    DateFormat fnameFormat = new SimpleDateFormat("yyMMdd_HHmm");
    DateFormat  timeFormat = new SimpleDateFormat("hh:mm:ss");
    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 + ".txt"); // 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);
          output.println(timeString);
          // header found
          // get the integer containing the bit values
          val = readArduinoInt();
          // print the value of each bit
          for(int pin=2, bit=1; pin <= 13; pin++){
            print("digital pin " + pin + " = " );
            output.print("digital pin " + pin + " = " );
            int isSet = (val & bit);
            if( isSet == 0){
               println("0");
               output.println("0");
            }
            else  {
              println("1");
              output.println("0");
            }
            bit = bit * 2; // shift the bit
          }
          // print the six analog values
          for(int i=0; i < 6; i ++){
            val = readArduinoInt();
            println("analog port " + i + "= " + val);
            output.println("analog port " + i + "= " + val);
          }
          println("----");
          output.println("----");
        }
      }
    }

    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