# Problem with accelerometer

**URL:** <https://discourse.processing.org/t/problem-with-accelerometer/5131>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [November 2, 2018, 7:55pm UTC](https://discourse.processing.org/t/problem-with-accelerometer/5131 "2018-11-02T19:55:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chalita](https://avatars.discourse-cdn.com/v4/letter/c/439d5e/32.png) [@chalita](https://discourse.processing.org/u/chalita)\
**Post date:** [November 2, 2018, 7:55pm UTC](https://discourse.processing.org/t/problem-with-accelerometer/5131/1 "2018-11-02T19:55:13Z")

</div>

```auto
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
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [November 3, 2018, 10:44am UTC](https://discourse.processing.org/t/problem-with-accelerometer/5131/2 "2018-11-03T10:44:22Z")

</div>

Hey,

It’s an error with OPENGL :

- [https://github.com/processing/processing/issues/5462](https://github.com/processing/processing/issues/5462)

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