Importing serial from two processing sketches simultaneously

edited July 2016 in Arduino

Hi all, I currently have two Arduinos (Uno and Nano) set up to do and record different functions but running at the same time. To import the serial data to csv files, I also have two separate processing sketches, one for each corresponding Arduino, that needs to run simultaneously. The sketches are pretty much the same since it is just importing serial data and they are working fine between the Arduino to Processing. The only hiccup is that when I open the csv files, one would have successfully imported the data but the other hasn't. Is there something I might be missing? Is it possible to read two Arduino serials into one Processing sketch instead? Thanks.

import processing.serial.*;

PrintWriter output;
Serial portGenerator;  // Arduino Uno, Controlling and recording generator
String valueGenerator;
String fileName;
String timestamp;
String timer;
StopWatchTimer sw;

void setup()
{
  println(Serial.list());
  println("Generator Control");
  print("Setting up Serial ports... ");
  portGenerator = new Serial(this, "COM3", 9600);  // intialising serial to Arduino Uno
  println("Done");

  fileName = year() + nf(month(),2) + nf(day(),2) + "-" + nf(hour(),2) + nf(minute(),2) + nf(second(),2) + "_test.csv";
  sw = new StopWatchTimer();
  output = createWriter(fileName); 
  sw.start();
}

void draw()
{
  while (portGenerator.available() > 0) // only when there are bytes available
  {
    valueGenerator = portGenerator.readStringUntil('\n'); // read line until \n
    timer = nf(sw.elapsedTime(), 6);
    if (valueGenerator != null)
    {
      output.print(timer);
      output.print(", ");
      output.print(valueGenerator);

      print(timer);
      print(", ");
      print(valueGenerator);
    }
  }
}

class StopWatchTimer
{
  int startTime = 0;
  int stopTime = 0;
  boolean running = false;

  void start()
  {
    startTime = millis();
    running = true;
  }

  void stop()
  {
    stopTime = millis();
    running = false;
  }

  int elapsedTime()
  {
    int elapsed;
    int elapsedSecond;
    if (running)
    {
      elapsed = (millis() - startTime);
    } else {
      elapsed = (stopTime - startTime);
    }

    elapsedSecond = (elapsed/1000);
    return elapsedSecond;
  }
}

void keyPressed()
{
  sw.stop();
  output.flush(); // Writes the remaining data to the file
  output.close(); // Finishes the file
  exit();         // Stops the program
}
Tagged:
Sign In or Register to comment.