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.
#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?
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.
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.
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.
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