Search code examples
processing

My shape will not rotate smoothly in processing


I am trying to connect rotate a shape in processing using serial data from a gyroscope. The shape rotates fine from 0-90 but when the angle is greater than 90 it doesn't rotate until it reaches approx 180. The serial data is correct as I am printing it on the serial monitor. I should note that it does work rarely in the range of 90-180. What can I do.

void draw(){

  pushMatrix();
  translate(200,200);
  rotateX(radians(90));

  rotateY(myVal);
  scale(50);
  beginShape(QUADS);


  while (mySerial.available() > 0){
    myString = mySerial.readStringUntil(nl);

    if(myString != null){
  
      background(0.5);

      myVal = float(myString) ;
      myVal = radians(-1 * myVal);

Solution

  • The while loop is blocking the main animation/rendering thread.

    An alternative is to pair bufferUntil() with serialEvent():

    e.g.

    void setup(){
      ...
      mySerial.bufferUntil(lf);
    }
    ...
    void serialEvent(Serial p){
       myString = mySerial.readString();
       myVal = float(myString.trim()) ;
       myVal = radians(-1 * myVal);
    }