Pages: [1]   Go Down
Author Topic: Serial Printing to Processing  (Read 1967 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)

Code:
#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:

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?
« Last Edit: November 12, 2010, 11:15:09 pm by afhaque89 » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any thoughts on why the two Serial outputs are different?
No but it would certainly be helpful if you post code that actually compiles.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
« Last Edit: November 13, 2010, 01:42:46 am by bcook » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote

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?

« Last Edit: November 13, 2010, 02:37:03 am by afhaque89 » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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.
Logged

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

Quote
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?

Quote
This is similar to my Arduino code:
Similar to? Why didn't you post YOUR code?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
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.
« Last Edit: November 14, 2010, 03:45:23 pm by afhaque89 » Logged

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

Quote
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.

Quote
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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay! So solution reached.

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

Arduino:
Code:
#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:
Code:
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.
Logged

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

Code:
if(arduinoPressure == 91) //Checks for '[' character {
  processingPressure=arduinoPort.readStringUntil(93);
would be much more maintainable as
Code:
if(arduinoPressure == ']') {
  processingPressure=arduinoPort.readStringUntil('[');
Logged

Pages: [1]   Go Up
Jump to: