Serial Printing to Processing

Hi guys,

I'm running into a snag as I'm trying to use serial.read() from processing to take in values from my Arduino prototype.

Basically, I have Arduino code that reads in pressure values, subtracts an offset value (stored in EEPROM), and converts the resulting value into desired units.

I was then hoping to send those values to Pachube through Processing. Trouble is the values shown in the Processing Serial do not match the desired values I see in the Arduino Serial. From the looks of it, the values printed do not account for that initial offset stored in EEPROM.

This is similar to my Arduino code:
(Source: http://spiffie.org/electronics/archives/microcontrollers/Arduino%20and%20Freescale%20MPX%20Pressure%20sensors.html)

#include <EEPROM.h>
#include <LiquidCrystal.h>

int offset = 0;

int number = 0; //number of iterations
int x;  // fresh value from sensor
float cmOfWater; //after multiplication
 
void setup()
{
  Serial.begin(9600);
  offset = EEPROM.read(0);
  offset<<=8;
  offset|=EEPROM.read(1);
  
  analogReference(DEFAULT);
 
  //wait until stabilization
  delay(1000);
}

void loop()
{
 
  //read the sensor
  x = analogRead(0);
 
  //convert to pascals
  cmOfWater = ((x-offset)*.17335);
 
  //output the pressure and the iteration number on serial monitor

  Serial.print(cmOfWater,3);
 
 
  //update the iteration and pause a bit.
  number++; //to the next character
 
  //Refresh rate of 500ms
  delay(500);
}

This is the Processing Code:

import processing.serial.*;

Serial arduinoPort;  // Serial port you are using
float arduinoPressure;

void setup() {
  arduinoPort = new Serial(this, Serial.list()[1], 9600); 
  arduinoPort.clear();
  
}

void draw() {
  arduinoPressure = arduinoPort.read();
  println(arduinoPressure);
  
    }

Any thoughts on why the two Serial outputs are different?

Any thoughts on why the two Serial outputs are different?

No but it would certainly be helpful if you post code that actually compiles.

No but it would certainly be helpful if you post code that actually compiles.

Sorry. I originally just posted to get the concept across. But I fixed the code.

Collect the output from the Sketch using Serial Monitor. Are you able to tell where one value ends and another begins?

Collect the output from the Sketch using Serial Monitor. Are you able to tell where one value ends and another begins?

Yeah. There's a time delay. Plus I've got an LCD attached to my unit displaying the correct Arduino based value. The LCD is also the reason I'm not just switching to Firmata and manipulating the inputs in Processing. I'd have no idea how to get Firmata to work with the LCD at this newb stage.

Yeah. There's a time delay.

I take it from your response that you have no intention of putting any thought into this matter.

You have two problems in your code...

  1. You're sending text characters but trying to read a binary value. Pick which one you want to use. I suggest text.

  2. You provide no reliable method of differentiating between two values. I suggest a square brackets around the value.

I take it from your response that you have no intention of putting any thought into this matter.

You have two problems in your code...

  1. You're sending text characters but trying to read a binary value. Pick which one you want to use. I suggest text.

  2. You provide no reliable method of differentiating between two values. I suggest a square brackets around the value.

Okay I get what you're saying here (and thanks a lot for helping). So if instead of sending in float values like 0.0001.2213.222 ....

I instead send [0.001] [1.2213] as strings?

Then from processing break up the string and convert the number back to float?

I instead send [0.001] [1.2213] as strings?

Exactly. Without the separators you will be pulling your hair out trying to get the transfer reliable.

Then from processing break up the string and convert the number back to float?

Yes. Skip incoming characters until an "opening" character arrives. Collect characters until a "closing" character arrives. Convert the text to a float.

I believe this has been discussed several times in the forum. You may want to spend some time searching before coding.

Then from processing break up the string and convert the number back to float?

Why? So you can then convert the floats to strings to send to pachube?

This is similar to my Arduino code:

Similar to? Why didn't you post YOUR code?

Why? So you can then convert the floats to strings to send to pachube?

I thought I needed to send floats? I'll have to take another look. Thanks for the help.

Similar to? Why didn't you post YOUR code?

I just meant that I abridged my full program to take out all of the irrelevant stuff.

I thought I needed to send floats?

I could be wrong. I was thinking more along the lines of twitter. But, I think all the data sent to pachube is sent using a POST request, which is a string. Hence the need to represent the float as a string to post to pachube.

I just meant that I abridged my full program to take out all of the irrelevant stuff.

Oh. OK, then, as long as the posted code ACTUALLY illustrates the problem. Too many people come in here and post code that dances all around the problem, figuring they know where the problem is because the snippet they post is where they see the problem, whereas the problem code is way over there, but the manifestation is here.

Okay! So solution reached.

Here's the working code. Hope it helps someone else.

Arduino:

#include <EEPROM.h>


int offset = 0;

int number = 0; //number of iterations
int x;  // fresh value from sensor
float cmOfWater; //after multiplication

void setup()
{
  Serial.begin(9600);
  offset = EEPROM.read(0);
  offset<<=8;
  offset|=EEPROM.read(1);
  
  analogReference(DEFAULT);

  //wait until stabilization
  delay(1000);
}

void loop()
{

  //read the sensor
  x = analogRead(0);

  //convert to pascals
  cmOfWater = ((x-offset)*.17335);

[glow]   Serial.print("[");
   Serial.print(cmOfWater,3);
   Serial.print("]");[/glow]

  //update the iteration and pause a bit.
  number++; //to the next character

  //Refresh rate of 500ms
  delay(500);
}

Processing:

import processing.serial.*;

Serial arduinoPort;  // Serial port you are using
float arduinoPressure;
[glow]String processingPressure;[/glow]

void setup() {
  arduinoPort = new Serial(this, Serial.list()[1], 9600); 
  arduinoPort.clear();
}

void draw() {
  //println(Serial.list()); 
  arduinoPressure = arduinoPort.read();
  
[glow]  if(arduinoPressure == 91) //Checks for '[' character {
  processingPressure=arduinoPort.readStringUntil(93); //Checks for ']' character
    if(processingPressure !=null)
    { [/glow]
     [glow] processingPressure=processingPressure.substring(0,processingPressure.length()-1); //removes ']' character
      println(processingPressure);[/glow]
}
}
}

With this I get outputted values from Arduino as:
[0.000][0.173][-2.312][1.231]

And outputted values from Processing as:
0.000
0.173
-2.312
1.231

Which can be uploaded to Pachube.

if(arduinoPressure == 91) //Checks for '[' character {
  processingPressure=arduinoPort.readStringUntil(93);

would be much more maintainable as

if(arduinoPressure == ']') {
  processingPressure=arduinoPort.readStringUntil('[');