Reading data from Processing to Arduino as a String/Substring

Hello,

I am working on a project for class where I am reading accelerometer data from an iPad via TouchOSC to Processing. I have gotten my Processing sketch to run well and output the data from all three axis. I am now trying to read this data in Arduino to operate two digital potentiometers based on the reading from the xrot and zrot values. Can anyone help me isolate the data from Processing as I call it from Arduino. My professor explained that I could use numerous variations of substrings, but I have been unable to make it work. Thanks in advance! My Processing code is below…

import processing.serial.*;
import processing.opengl.*;
import oscP5.*;

Serial myPort;    //create object from Serial class
int val;          //data received from the serial port

OscP5 oscP5;

float xrot = 0;
float zrot = 0;
float orientation = 0;

float xrot_targ = 0;
float zrot_targ = 0;
float orientation_targ = 0;

float dampSpeed = 5;

void setup() {
  size(400, 400, OPENGL);
  oscP5 = new OscP5(this, 8000);
  smooth();
}

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);
  }

  */if (orientation_targ > orientation) {
    orientation = orientation + ((orientation_targ - orientation) / dampSpeed);
  } 
  else {
    orientation = orientation - ((orientation - orientation_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);
}

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_targ = theOscMessage.get(2).floatValue();
  }
  println("x " + int(xrot) + "\tz "  + int(zrot) + "\ty "  + orientation );

  myPort.write("acc " + xrot + zrot + orientation);
}

Moderator edit: The usual. Yawn. AWOL.

tech10: My Processing code is below...

I'm confused. This code doesn't look like a buildable sketch to me. Is this meant to build and run on an Arduino?

To receive and parse commands or date over the serial line on Arduino is not at all difficult, but is not completely trivial. It helps if you can set out the format of data that will be transferred so that it is easy and reliable to parse and easy to mock up using a serial monitor. Try to make sure that each record starts with something that isn't in the data, that the data is plain typeable text, that the record is shortish and ends in, or may end in a carriage return. These things make it easier to type valid records into a serial monitor which makes testing easier.

This code doesn’t look like a buildable sketch to me.

It is in Processing.

My professor explained that I could use numerous variations of substrings, but I have been unable to make it work.

There aren’t any strings involved in that processing code. You really need to read through that code, and understand what it is doing. It’s not that complicated.

The oscEvent function gets the data from the message that the iWhatever is sending to the PC. You are then sending that data, all concatenated together, to the Arduino.

Using delimiters between the values is going to be essential to proper parsing on the Arduino side.
myPort.write("<acc " + xrot + ", " + zrot + ", " + orientation + “>”);

Then, you can use this code to read the data on the Arduino:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

The strtok() function will allow you to extract tokens. The atoi() or atof() functions will convert the tokens to numbers.