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!