Processing Vertex Flickering

Hi everyone,

I am using Processing to display a moving cube which simulates the movement of an MPU6050 IMU sensor
The code works fine, however it seems to flicker continuously while running.
I have tried to separate the serial read into another function, however the flicker became worse.
I'd appreciate some help, Thanks :slight_smile:

/**
 * RGB Cube.
 * 
 * The three primary colors of the additive color model are red, green, and blue.
 * This RGB color cube displays smooth transitions between these colors. 
 */
import processing.serial.*;
float xmag, ymag = 0;
float newXmag, newYmag = 0; 
 String [] PortList;
Serial  myPort;
String input;
float movement;
void setup()  { 
  size(800, 450, P3D); 
  noStroke(); 
  colorMode(RGB, 1); 
   println(Serial.list());
 // Open the port that the Arduino board is connected to (in this case #0)
 // Make sure to open the port at the same speed Arduino is using (9600bps)
myPort = new Serial(this, Serial.list()[0],9600 );
} 
 
void draw()  { 
  background(0.5);
   input = myPort.readString();
       movement = float(input);
  drawCube();
  
//   text(input,100,100) ;

//   rect( 10,  10, 30, 30);
} 


void Draw_box()
{
  
    beginShape(QUADS);
  
    fill(1, 1, 0);
    stroke(1);
  vertex(-100,  130,  20);
  vertex( 100,  130,  20);
  vertex( 100, -130,  20);
  vertex(-100, -130,  20);
 fill(1, 0, 0);
  vertex( 100,  130,  20);
  vertex( 100,  130, -20);
  vertex( 100, -130, -20);
  vertex( 100, -130,  20);
 fill(0, 0, 1);
  vertex( 100,  130, -20);
  vertex(-100,  130, -20);
  vertex(-100, -130, -20);
  vertex( 100, -130, -20);
 fill(1, 0, 0);
  vertex(-100,  130, -20);
  vertex(-100,  130,  20);
  vertex(-100, -130,  20);
  vertex(-100, -130, -20);
 fill(1, 0, 0);
  vertex(-100,  130, -20);
  vertex( 100,  130, -20);
  vertex( 100,  130,  20);
  vertex(-100,  130,  20);
 fill(1, 0, 0);
  vertex(-100, -130, -20);
  vertex( 100, -130, -20);
  vertex( 100, -130,  20);
  vertex(-100, -130,  20);

  endShape();
}
void drawCube() {  
    pushMatrix(); 
  translate(width/2, height/2, -30); 
  
 
  
  rotateX(-90); 
//  rotateY(-xmag); 
  rotateZ(-movement);
  Draw_box();
//  scale(90);

  popMatrix(); 
  
}

Try increasing the Serial baud rate (at both ends). 9600 is rather slow.

Pete

iv'e tried 115200 but same thing happened :frowning:
thanks pete

You should NOT be reading serial data in the draw() function. Use the Serial::bufferUntil() method in setup(). Add a serialEvent() method to your sketch, and read the buffered serial data there.

Don't redraw the cube if it is still in the same position.

You should know that, unlike on the Arduino, draw() is NOT called as often is possible. It is called as often as need to match the specified frame rate. You can change the frame rate. It's rather low, by default.

Hey paul, Thank you so much, everything seems to work after adding bufferuntil() method (even though i don't understand the theory)

heres my new code

/**
 * RGB Cube.
 * 
 * The three primary colors of the additive color model are red, green, and blue.
 * This RGB color cube displays smooth transitions between these colors. 
 */
import processing.serial.*;
float xmag, ymag = 0;
float newXmag, newYmag = 0; 
 String [] PortList;
Serial  myPort;
String input;
float movement;
int lf = 10;
void setup()  { 
  size(800, 450, P3D); 
  noStroke(); 
  colorMode(RGB, 1); 
   println(Serial.list());
 // Open the port that the Arduino board is connected to (in this case #0)
 // Make sure to open the port at the same speed Arduino is using (9600bps)
myPort = new Serial(this, Serial.list()[0],9600 );
myPort.bufferUntil(lf);
} 
 
void draw()  { 
  background(0.5);
   
       movement = float(input);
  drawCube();
  
//   text(input,100,100) ;

//   rect( 10,  10, 30, 30);
} 


void Draw_box()
{
  
    beginShape(QUADS);
  
    fill(1, 1, 0);
    stroke(1);
  vertex(-100,  130,  20);
  vertex( 100,  130,  20);
  vertex( 100, -130,  20);
  vertex(-100, -130,  20);
 fill(1, 0, 0);
  vertex( 100,  130,  20);
  vertex( 100,  130, -20);
  vertex( 100, -130, -20);
  vertex( 100, -130,  20);
 fill(0, 0, 1);
  vertex( 100,  130, -20);
  vertex(-100,  130, -20);
  vertex(-100, -130, -20);
  vertex( 100, -130, -20);
 fill(1, 0, 0);
  vertex(-100,  130, -20);
  vertex(-100,  130,  20);
  vertex(-100, -130,  20);
  vertex(-100, -130, -20);
 fill(1, 0, 0);
  vertex(-100,  130, -20);
  vertex( 100,  130, -20);
  vertex( 100,  130,  20);
  vertex(-100,  130,  20);
 fill(1, 0, 0);
  vertex(-100, -130, -20);
  vertex( 100, -130, -20);
  vertex( 100, -130,  20);
  vertex(-100, -130,  20);

  endShape();
}
void drawCube() {  
    pushMatrix(); 
  translate(width/2, height/2, -30); 
  
 
  
  rotateX(-90); 
//  rotateY(-xmag); 
  rotateZ(-movement);
  Draw_box();
//  scale(90);

  popMatrix(); 
  
}
void serialEvent(Serial p) {
   input = myPort.readString();
}

everything seems to work after adding bufferuntil() method (even though i don't understand the theory

The draw() method is not interfered with. The serial data is buffered until the specific character arrives. Then the serialEvent() method is called. That happens far less often than a character arrives on the serial port. which is what you were dealing with before.

Thank you so much Paul, i think I understand how it all works now.
After your explanation i changed the code to myPort.bufferUntil('\n');
which i found to work much better, as i was getting an everytime i restarted the program
"Error, disabling serialEvent() for COM5"

Thanks again for your help Paul :slight_smile: