Reading Data from Arduino

edited April 2014 in Arduino

I am having a weird problem with Processing Programming Language.

  1. I can read and store the data as a text file from a Arduino board with the program shown below. It works fine.

  2. I wanted to read data from multiple boards and record in one text file. I saved the file with a different name and made some chnages. It stopped working. I compared line by line with the unchanged file and deleted the chages, still it did not work.

  3. I was surprised to see that even an insignificant change makes the program unworkable. Even if I comment-out the line, the program does not work any more. It is explained below.

To_Txt_No_Dgtl.pde

/*

+++  WARNING: ANY FILE WITH TODAY'S DATE WILL BE OVERRITTEN +++

       * Receive Multiple Fields Binary To File_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;
String fileNm;
String prtNm;
Serial myPrt, myPrt1;                                // Create object from Serial class
short prtIndx = 2;                          // select the com port, 0 is the first port
char HEADER = 'H';
int TtlPrt = 0;
int strtng, dy = 0;
int val;
String y, m, d;
String hr;
String mn;
String s;
String t;


void setup()
{
  size(200, 200);
  dy = day();

  TtlPrt = Serial.list().length;                    // Counts how many com ports are present

  if (TtlPrt <=2)                                   // i.e. Arduino is connected or not
  {
    println("Only " + TtlPrt + " COM ports are present.");
    println("May be, Arduino is not connected.");
  }
  else
  {
                                                              // Open whatever serial port is connected to Arduino.
    String prtNm = Serial.list()[prtIndx];
    println(Serial.list());
    println(" Connecting to -> " + Serial.list()[prtIndx]);
    myPrt1 = new Serial(this, prtNm, 9600);
  }
}

void draw()
{
  if (TtlPrt <=2) 
    exit();
  else
  {                                         // After running, for the first time a file is created
    if (strtng == 0 || dy != day())
    {
      y = str(year());
      m = str(month());
      d = str(day());
      if (month() < 10)
        m = "0" + m;
      if (day() < 10)
        d = "0" + d;

      if (dy != day())        // When continuosly running, if the date changes the existing file is closed and a new file is created
      {
        output.flush();                       // Writes the remaining data to the file
        output.close();                       // Closes the file
      }
      fileNm = (y + m + d);
      output = createWriter(fileNm + ".txt");
      output.println("  , " + "  , " + "   , " + "Room,   " + " Roof,  " + "S.Wall, " + "S.Wall, " + "  Roof, " + "E.Wall  ");
      output.println("Hr, " + "Mn, " + "Sec, " + "Centre, " + " under, " + "inside, " + "otside, " + "  top,  " + "outside " + "\n");
      strtng = 1;
      dy = day();
    }

    hr = str(hour());
    mn = str(minute());
    s = str(second());
    if (hour() < 10)
      hr = "0" + hr;
    if (minute() < 10)
      mn = "0" + mn;
    if (second() < 10)
    s = "0" + s;

    if ( myPrt1.available() >= 15)                      // wait for the entire message to arrive
    {
      if( myPrt1.read() == HEADER)       // is this the header
      {
            // header found
        println(hr + "  " + mn + "  " + s + "     ");
        output.print(hr + ", " + mn + ",  " + s + ", ");

// ------------ print the six analog port values ---------

        for(int i=0; i < 6; i ++)
        {
          val = 0;
          val = readArduinoInt1();
          println("analog port " + i + "= " + val);
        if (val <= 9)
          output.print("     " + val + ", ");
        if (val > 9 && val < 100)
          output.print("    " + val + ", ");
        if (val > 99 && val < 1000)
          output.print("   " + val + ", ");
        if (val > 999 && val < 10000)
          output.print("  " + val + ", ");
        if (val > 9999 && val < 100000)
          output.print(" " + val + ", ");
        }
        println("----");
        output.println("\n");
      }
    }
    k = k+1;
    if (k > 10)
    {
      k=0;
      output.flush();                     // Writes the remaining data to the file
    }
  }
}
void keyPressed()
{
  output.flush();                       // Writes the remaining data to the file
  output.close();                       // Finishes the file
  exit();                               // Stops the program
}

// ---------- Function for value reading from COM2 -------------

                                        // return the integer value from bytes received on the serial port
                                        // (in low,high order)
int readArduinoInt1()
{
  int val;                              // Data received from the serial port
  val = myPrt1.read();                  // read the least significant byte
  val =  myPrt1.read() * 256 + val;           // add the most significant byte
  return val;
}
                                        // (in low,high order)

===========================================

  1. The "if-then" loop from line 103 to 132 works fine.

  2. But, if I add an "else" loop the working loop stops working as shown below:

++++++++++++++++++++++++++++++++++++++++

if ( myPrt1.available() >= 15)
{
  Working codes
}
else
{
  println("Port is not available");
}

++++++++++++++++++++++++++++++++++++++++

Note: The curly brackets are not necessary, as it is only one line after "else".

The idea of this program was adopted from Arduino Cookbook by Michael Margolis.

Sign In or Register to comment.