Pages: [1]   Go Down
Author Topic: Arduino to Processing Communication Issue  (Read 506 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have some code that uses 2 temperature sensors. It works great on the arduino and is able to output to the serial monitor. My problem comes when I attempt to send the data to Processing and create a .csv file with the data. Arduino and Processing are both attempting to use COM3, and of course they can't at the same time. The Processing creates a .csv file, but does not have any data in it. Could someone point out my issue? I have tried everything I can think of at this point. My code is below:

Arduino:

Code:
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2 /*-(Connect to Pin 2 )-*/
#define bus2 4

OneWire ourWire(ONE_WIRE_BUS);
OneWire ourWire2(bus2);

DallasTemperature sensors(&ourWire);
DallasTemperature sensors2(&ourWire2);

void setup()
{
  Serial.begin(9600);
  delay(1000);

  sensors.begin();
}

void loop()
  Serial.println();
  sensors.requestTemperatures();
  Serial.print(sensors.getTempCByIndex(0));
  Serial.print(",  ");
  sensors2.requestTemperatures();
  Serial.print(sensors2.getTempCByIndex(0));

  delay(1000);
}


Processing:

Code:
import processing.serial.*;


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

Serial myPort;
short portIndex = 0;
char HEADER = 'H';

void setup()
{
  size(200, 200);
  String portName = Serial.list()[portIndex];
  println(Serial.list());
  println(" Connecting to -> " + Serial.list()[portIndex]);
  myPort = new Serial(this, portName, 9600);
  Date now = new Date();
  fileName = fnameFormat.format(now);
  output = createWriter(fileName + ".csv");
}

void draw()
{
  int val;
  String time;

  if ( myPort.available() >= 15)
  {
    if( myPort.read() == HEADER)
    {
      String timeString = timeFormat.format(new Date());
      println("Message received at " + timeString);
      output.println(timeString);
      
      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;
      }
      // 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();
  output.close();
  exit();
}

int readArduinoInt()
{
  int val;      
  val = myPort.read();        
  val =  myPort.read() * 256 + val;
  return val;
}


Thank you in advance!!!


« Last Edit: June 04, 2013, 01:10:49 pm by michael_davidson » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12417
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PLease use the # button when posting code, it becomes better readable. You can modify your post, select the code and press the # button above the smileys for proper tags.
Thank you.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45972
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My problem comes when I attempt to send the data to Processing and create a .csv file with the data. Arduino and Processing are both attempting to use COM3, and of course they can't at the same time.
Sure they can, as long as Processing is the only application on the PC that is trying to use that port.

If you mean that Processing and the Serial Monitor can't both use COM3 at the same time, you are correct. So, close the Serial Monitor.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, that works. But I'm still confused as to how to get Processing to receive the temperature data and continually update the .csv file.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45972
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But I'm still confused as to how to get Processing to receive the temperature data and continually update the .csv file.
For one thing, reading from the serial port is not something that should be done in draw(). You should have a serialEvent() function that does the serial I/O stuff.

Why the Arduino starts with printing a carriage return and line feed is a mystery. Then, it prints variable length values, separated by a comma and some spaces. At the end, NO carriage return and line feed are sent.

The Processing application then expects a fixed number of characters to arrive. It then expects that the input will contain a 'H', as the first character. Well, the Arduino never sends an H, so Processing never does anything. That seems pretty consistent with your problem description.

Perhaps you can now see what you need to do.
Logged

Pages: [1]   Go Up
Jump to: