Problem with accelerometer


#1
import processing.serial.*;

Serial c;

void setup()
{
  size(800, 600, OPENGL);
  c = new Serial(this, "/dev/cu.usbserial-DN02SMXZ", 115200); // change the COM port
}

void draw()
{
  float x, y, z;

  if (c.available() > 0) {                   // check if any data available
    String a = c.readStringUntil('\n');      // get data which terminates with line feed
    if (a != null) {                         // make sure there is data and not an empty string
      println(a);                            // print string to console
      String[] b = split(a, " ");            // split data in string into individual elements
      if (b.length == 3) {                   // check if there are 3 elements, x,y,z
        background(0);                           // clear window and set background to black
        lights();                                // set default 3D lighting
        x = float(b[0]);                     // convert to floating point
        y = float(b[1]);
        z = float(trim(b[2]));               // use trim function to delete line feed from value
        fill(200);                           // set color of rectangles
        rect(50, 300, 100, x * 100);         // draw rectangle to show magnitude of each axis
        rect(150, 300, 100, y * 100);
        rect(250, 300, 100, z * 100);
        text("X = " + x, 50, 200);           // display values
        text("Y = " + y, 150, 200);
        text("Z = " + z, 250, 200);
        float angle2 = atan2(z, x);          // calculate angle between X and Z
        float angle1 = atan2(z, y);          // calculate angle between Y and Z
        pushMatrix();                        // work in local coordinates
        translate(700, 300);                 // translate cube to coordinates
        rotateX(angle1);                     // rotate about X axis
        rotateY(angle2);                     // rotate about Y axis
        fill(200, 0, 200);                   // make cube purple
        box(100, 100, 100);                  // render cube
        popMatrix();                         // get out of local coordinates
      }
    }
  }
}


#2

Hey,

It’s an error with OPENGL :

PS : format your code using Ctrl+T in the Processing IDE then use the </> button in the message editor to insert some code.