Receiving accelerometer data from iPhone with Arduino through Processing

Hi Everyone,

Thanks in advance for taking time to answer me/read this post.
I'm working on an Arduino programme that should take in the z-orientation of a product when a button is pressed and use that information to calculate a new number that it will display in the serial monitor.

I have no problems with pressing the button or calculating the information, but It'd like to determine the z-orientation of the product with an iPhone. I have found tutorials that show how to get the accelerometer data from an iPhone with processing and I have found tutorials that show how to communicate between Processing and Arduino, but I'm having trouble actually getting it to work. Mostly with the fact that it seems like Processing can only send out char data over serial.

Here's my Processing code:

import processing.serial.*;
import processing.opengl.*;
import oscP5.*;
 
OscP5 oscP5;
Serial myPort;
 
float xrot = 0;
float zrot = 0;
 
float xrot_targ = 0;
float zrot_targ = 0;
float orientation = 0;
 
float dampSpeed = 5;
 
void setup() {
  size(400,400, OPENGL);
  oscP5 = new OscP5(this,8000);
  smooth();
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
}
 
void draw() {
  camera(  0, 0, 300,
         0, 0, 0,
         0.0, 1.0, 0.0
     );
  background(0); 
 
  // Basic value smoothing
 
  if (xrot_targ > xrot) {
    xrot = xrot + ((xrot_targ - xrot) / dampSpeed);
  } else {
    xrot = xrot - ((xrot - xrot_targ) / dampSpeed);
  }
 
  if (zrot_targ > zrot) {
    zrot = zrot + ((zrot_targ - zrot) / dampSpeed);
  } else {
    zrot = zrot - ((zrot - zrot_targ) / dampSpeed);
  }
 
 // Detection for if the iPhone is upsidedown or not
 
  if (orientation < 0) {
    fill(255,0,0);
    rotateX(radians(xrot));
    rotateZ(radians(zrot));
  } else {
    fill(255,255,0);
    rotateX(radians(xrot*-1));
    rotateZ(radians(zrot*-1));
  }
  box(130,10,60);

  int zrotInt = int(zrot);
  myPort.write(zrotInt);
 
}
 
void oscEvent(OscMessage theOscMessage) {
  if(theOscMessage.checkAddrPattern("/accxyz")==true) {
      xrot_targ = (theOscMessage.get(0).floatValue()*90);
      zrot_targ = (theOscMessage.get(1).floatValue()*90)*-1;
      orientation = theOscMessage.get(2).floatValue();
  }
}

It's a whole lot, and I copied most of it, but I think the value zrot is a number that represents the z-rotation of the iPhone.

Trying to send this to Arduino, I imported the serial library on the top and I used this code:

  int zrotInt = int(zrot);
  myPort.write(zrotInt);

It transforms the float value of zrot to an int and writes it to the port defined in setup.

In Arduino, I use this code to receive the data:

     val = Serial.read() - '0';

It's not working yet, but I'm not sure where to go next. Any help is much appreciated!

n Arduino, I use this code to receive the data:
Code:

val = Serial.read() - '0';

Are you saying all the information is contained in a single decimal digit?

Sorry, val was earlier defined as being an int in the code. Here's the full arduino program:

int i = 0;
int val;

//Setup
void setup() {
  pinMode(2, INPUT);
  Serial.begin(9600);
}

//De loop
void loop() {
   int button = digitalRead(2);
   if(button == 0 && i == 0) {
     val = Serial.read() - '0';
     Serial.println(val);
     i = 1;
    }
}

The information should be in a float, but going off of what I've read, it's not possible to write a float from processing.

I'm no Processing expert, but I can't imagine that it can't print a float.
However, your Arduino code assumes that all the information is contained in a single decimal digit.

AWOL:
all the information is contained in a single decimal digit.

What exactly do you mean with that?

I tried using the write() function to write a float number to the serial port, but it gave me an error that it can only take Char datatypes.

I tried using the write() function to write a float number to the serial port,

That's what the write method is for - passing a char array unchanged from memory to serial.
In C if you wanted to write a float, you'd take the address of the float and cast the address to be a char pointer.

However, that's tricky between computers where there may be endian differences in the representation of values.

More usual is to print the value, to give an ASCII representation of the value, and then convert back, though this carries with it the potential for loss of precision.
Best to avoid floats.

Ok, that's more clear! What would you advise me to do with the float number? Change it to a string, or to an int? Do you have any code examples?

I really appreciate your help!