We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Pressure sensor (Read 2352 times)
Pressure sensor
Jun 3rd, 2008, 3:05pm
 
Hi.
I have a problem with serial communiaction between Arduino and Processing. I want to read data from a pressure sensor.

The Serial Monitor in the Arduino software prints values between 0 and 100, depending on the pressure on the sensor.

I try to read this data with Processing, but it only prints the values 48, 13, 10 all the time. Is there anything wrong with my code?

Processing
Code:
  
import processing.serial.*;
Serial thePort;
int var;

void setup() {
println(Serial.list());
thePort = new Serial(this, Serial.list()[5], 9600); //Serial.list()[5] is the right port, i double-checked on this
}

void draw() {
while(thePort.available() > 0) {
var = thePort.read();
println(var);
}
}


Arduino code
Code:

int pressurePin = 5;
float var;

void setup() {
Serial.begin(9600);
pinMode(pressurePin, INPUT);
}

void loop() {
var = analogRead(pressurePin);
Serial.println(var, DEC);
}
Re: Pressure sensor
Reply #1 - Jun 5th, 2008, 10:41pm
 
Your code is working just fine. Smiley


48 = the ascii code for the "0" character (the value you're getting from the sensor).
13 = the ascii code for carriage return
10 = the ascii code for a line feed.

You're seeing the 13 and 10 because "println" adds them to trigger a new line.  In the the arduino debug terminal you'd just see a bunch of zeros with line breaks because it will interpret the bytes as ascii characters.  In processing, the serial library will just read them as raw values which is what you're experiencing.

It sounds like the pressure sensor might not be connected/working properly if you're only seeing zeros (the 48's) though.
Re: Pressure sensor
Reply #2 - Jun 5th, 2008, 10:58pm
 
ok...

Is there a better way to get the values than just using Serial.print() in instead of Serial.println() in the Arduino code and subtracting 48 from this value in Processing?
Re: Pressure sensor
Reply #3 - Jun 5th, 2008, 11:21pm
 
That depends on the resolution you need for the sensor values.

If you don't need the full 0 to 1024 range provided by the analog pin, you can use "map" to adjust the value so it's only between 0 and 255 then use Serial.print() and just send the raw byte (use BYTE instead of DEC). Then the data received by processing would directly reflect the sensor value.

Note: in the example below, the serial output from the arduino will look like nonsense if you view it in the debug terminal.

Something like this (arduino code) -
Code:

int pressurePin = 5;
int var;

void setup() {
Serial.begin(9600);
pinMode(pressurePin, INPUT);
}

void loop() {
var = analogRead(pressurePin);
var = map(var, 0, 1023, 0, 255);
Serial.print(var, BYTE);
}


If you DO need the full 0 to 1024 range, you will have to send more than one byte to represent the value.  I can help with that if you need it, but if you're just getting started I would recommend you stick to the simpler way I listed here..
Re: Pressure sensor
Reply #4 - Jun 5th, 2008, 11:35pm
 
your code works just fine for me. thanks for helping me out.
Page Index Toggle Pages: 1