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.
IndexProgramming Questions & HelpElectronics,  Serial Library › How to read float variable from serial port
Page Index Toggle Pages: 1
How to read float variable from serial port? (Read 4012 times)
How to read float variable from serial port?
Oct 2nd, 2009, 12:22pm
 
Hi, boys,
I have a problem. I use my Arduino to read a float value and to send it to my Processing. In this code I just send the same value over and over.
Code:
float tempC=23.32;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print(tempC);
delay(1000);  
}

I can then see this value in Serial Monitor and in COM Port Toolkit, so it is working, but how can I read it with Processing? I think you can't read a float value from serial port. Could someone include any example of working code? It would really help me, I can't figure it out Cry
Re: How to read float variable from serial port?
Reply #1 - Oct 3rd, 2009, 10:36pm
 
A couple hints:

-Use println() on the Arduino. This will send an 'end of line' character.

-Parse the serial message as it comes in and watch for that character.

-Use float() to convert a string into a numeric value.

If you still have trouble, I (and I'm sure others) can help. I might have to dig thru my code library to find a working example...
Re: How to read float variable from serial port?
Reply #2 - Oct 5th, 2009, 11:50am
 
Thank you for your response!
I was really confused because I think this should be solved inside processing as it is solved in Wiring - two functions, one for sending(W) and the other(P) for receiving. I will try your hints, but if you find something that seems to be working, post it here. I believe I am not the only one (staring at the Sun Grin ) who is facing this problem.

Btw, I hope, it works good when I send data from Processing to Arduino Undecided
Re: How to read float variable from serial port?
Reply #3 - Oct 11th, 2009, 9:33am
 
Hi, finally I had some time for programming and thanks to NoahBuddy's hints I was successful. If you are facing the same problem I was, my code can help you.

Arduino reads analog input and sends float variable.
Code:
int analogPin = 0;
float temp = 0;

void setup() {
 Serial.begin(9600);
}

void loop() {
 temp=analogRead(analogPin);
 temp=(map(temp,0,1024,0,5000)/10.0);
 Serial.println(temp); // println add Linefeed to my float
 delay(1000);
}


Processing reads and displays float from serial.
Code:
import processing.serial.*;

int lf = 10;    // Linefeed in ASCII
String myString = null;
Serial myPort;  // Serial port you are using
float num;

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

void draw() {
 while (myPort.available() > 0) {
   myString = myPort.readStringUntil(lf);
   if (myString != null) {
print(myString); // Prints String
num=float(myString); // Converts and prints float
println(num);
   }
 }
 myPort.clear();
}
Re: How to read float variable from serial port?
Reply #4 - Nov 27th, 2009, 7:06am
 
Your code indeed helped me. So, thanks a bunch. You just saved me a few hours. Hope I will be able to return the favour somewhere in the future.
Page Index Toggle Pages: 1