Stefan Goodchild

TouchOSC and Processing Accelerometer Example Code

Playing with my iPhone and Processing v1 on the train last night I started messing with TouchOSC and thought that this super simple (and fairly rough) example code using the oscP5 library may help someone on their way to interactive wobbly nirvana.

Code after the jump.

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

OscP5 oscP5;

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

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

}

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

Other people had this to say

  • THANK YOU! I am the Processing/Osc newb you were probably thinking of in posting this, and it has saved me from hours of hairpulling agony.

    Margaret

    August 11th, 2009 at 11:29 am |

  • thanks for this, I was just playing around and was looking for some code to delve in. this was to the point!

    Iman

    December 14th, 2009 at 1:47 pm |

  • Wow! I never had so much fun with my iPhone’s accelerometer until now! I can some really cool uses for this coupled with my Arduino and a pair of Zigbee radios in the near future. Thanks!

    Brett W.

    January 27th, 2010 at 10:37 pm |

  • Wow, so cool. Thanks for sharing.
    Very new to TouchOSC and processing - but hooked now!

    rebecca

    January 29th, 2010 at 9:14 pm |

  • Thank you so much! I am in the process of learning processing; is there a way to output x,y data from the accelerometer through OSC’s? Looking to control animation through puredata and Animata… just need the accelerometer to finish the project!

    Kevin C. Walker

    May 28th, 2010 at 10:58 pm |

  • Thank you, Stefan. I’m a newbie to Processing and OSC also. This is super helpful!

    Alan

    August 1st, 2010 at 12:18 pm |

  • @Kevin The oscP5 library will output OSC as well as read it in so you can pass anything through. You shouldn’t need to use Processing at all though as PD support OSC directly doesn’t it? Puredata / OSC Tutorial

    admin

    August 18th, 2010 at 3:48 am |

Leave Your Reply

Comment Feed | Trackback