Sending and Receiving float variables over serial connection

Hi guys

I'm working on this project that involves using some calculations in processing that stores values for x,y,z that are float variables. I need to be able to send this to arduino, store them and use them for further processing in my arduino sketch.
At the moment, I can print the values of my calculation(in processing) and I've established a simple serial link between both parties to light up the Arduino onboard LED using a mouse click on processing.

  • Is what I'm attempting feasible?
  • Is it possible to send float values over serial connection? if not, how do you convert it to a string and then read necessary characters in Arduino?

Please find both sketches for Processing and Arduino respectively below

// Processing Read & Write Program incl. Lorenz Attractor

import peasy.;
import processing.serial.
;
Serial myPort; // create object from serial class
String val; // data recieved from serial port
boolean firstContact = false;

// Main Program
float x = 0.01;
float y = 0;
float z = 0;

float a = 10;
float b = 28;
float c = 8.0/3.0;

ArrayList points = new ArrayList();
PeasyCam cam;

void setup()
{

size(200,200);
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, "/dev/cu.usbmodem1421", 9600);
myPort.bufferUntil('\n');

}

void serialEvent( Serial myPort) {

//put the incoming data into a String -
//the '\n' is our end delimiter indicating the end of a complete packet

val = myPort.readStringUntil('\n');

//make sure our data isn't empty before continuing

if (val != null) {
//trim whitespace and formatting characters (like carriage return)
val = trim(val);
println(val);

//look for our 'A' string to start the handshake
//if it's there, clear the buffer, and send a request for data
if (firstContact == false) {
if (val.equals("A")) {
myPort.clear();
firstContact = true;
myPort.write("A");
println("contact");
}
}
else { //if we've already established contact, keep getting and parsing data
println(val);

if (mousePressed == true)
{ //if we clicked in the window
myPort.write('1'); //send a 1
println("1");
}

// when you've parsed the data you have, ask for more:
myPort.write("A");
}
}
}

void draw()
{
float dt = 0.01;
float dx = (a * (y - x))*dt;
float dy = (x * (b - z) - y)*dt;
float dz = (x * y - c * z)*dt;
x = x + dx;
y = y + dy;
z = z + dz;

int q = Math.round(x);
int w = Math.round(y);
int e = Math.round(z);

// println(x,y,z);
//println(q,w,e);
}


Arduino:

char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13
boolean ledState = LOW; //to toggle our LED

void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
establishContact(); // send a byte to establish contact until receiver responds
}

void loop()
{
if (Serial.available() > 0) { // If data is available to read,
val = Serial.read(); // read it and store it in val

if(val == '1') //if we get a 1
{
ledState = !ledState; //flip the ledState
digitalWrite(ledPin, ledState);
}
delay(100);
}
else {
Serial.println("A dog"); //send back a hello world
delay(50);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("A"); // send a capital A
delay(300);
}
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

Will you have sufficient precision if your PC sends the data as character string with a specified number of decimal places? For example 123.456

If so that will be the easiest method and you can use the examples in Serial Input Basics to receive the data. The included parse example illustrates how to turn the text back into a float variable.

...R

You might want to glance over this post:

How To: Sending float over serial

Not sure if it'll work for your application exactly.