Go Down

Topic: arduino/processing serial communication being slow (Read 3075 times) previous topic - next topic


PaulS

Quote
Nope.

Whoosh.

That was hint for you to post your code - using the # button, not as an attachment.

ahhreeyell

Heh, I'm not always that good at picking up hints.

Are you referring to the 3D part of my Processing code? Here it is:

Code: [Select]
  if(derp>=201 && derp<=400) {
    float herp = map(derp, 201, 400, 0, 1023);
   
   if(millis()%1000 == 0){
     rect(0,0,width,height);
     background(#ffffff);
   }
   
    boxFill = color(random(255), random(255), random(255), random(105));
    translate(400,300,0);
    rotateY(random(herp));
    rotateX(random(herp));
    fill(boxFill);
    box(random(herp), random(herp), random(.5*(herp+herp)) );

   // println(herp);
  }

PaulS

Quote
Are you referring to the 3D part of my Processing code?

Not specifically, we really need to see all of your processing code.

ahhreeyell

Code: [Select]
import processing.opengl.*;
import processing.serial.*;
Serial port;

float znoise = 0.0;
float derp;
color boxFill;

void setup() {
  size(1000,800, OPENGL);
  port = new Serial(this, "/dev/cu.usbserial-A700fiDh", 9600);
  port.bufferUntil('\n');
  noStroke();
}

void draw() {
  float x = 0;
  float y = 0;

println(derp);

  if(derp>=201 && derp<=400) {
    float herp = map(derp, 201, 400, 0, 1023);
   
   if(millis()%1000 == 0){
     rect(0,0,width,height);
     background(#ffffff);
   }
   
    boxFill = color(random(255), random(255), random(255), random(105));
    translate(400,300,0);
    rotateY(random(herp));
    rotateX(random(herp));
    fill(boxFill);
    box(random(herp), random(herp), random(.5*(herp+herp)) );

   // println(herp);
  }

  if(derp>=0 && derp<=200) {

    float herp = map(derp, 0, 200, 0, 1023);

    if(herp>500) {
      x=20;
      y=20;
    }
    else if(herp>350 && herp<=500) {
      x = 10;
      y=10;
    }
    else if(herp<=350 && herp>100) {
      x=6;
      y=6;
    }
    else if(herp<=120 && herp>90) {
      x=5;
      y=5;
    }
    else if(herp<=90 && herp>60) {
      x=4;
      y=4;
    }
    else if(herp<=60 && herp>30) {
      x=3;
      y=3;
    }
    else if(herp<=30) {
      x=2;
      y=2;
    }

    float xnoise = 0.0;
    float ynoise = 0.0;
    for(int i=0; i<=height; i+=y) {
      for(int j=0; j<=width; j+=x) {
        float blah = noise(xnoise, ynoise, znoise)*256;
        fill(blah);
        rect(j, i, x,y);

        if(key=='c' || key == 'C') {
          fill(i, blah, j);
          rect(j, i, x,y);
        }

        xnoise += 0.09;
      }
      ynoise += 0.09;
    }
    znoise += 0.09;
   
   
  //println(herp);
  }
}

void serialEvent (Serial port)
{
  derp = float(port.readStringUntil('\n'));
}

PaulS

Code: [Select]
  size(1000,800, OPENGL);
So, this defines height and width.
If the value received from the serial port, as a float (?) is in the range 0 to 200, it is mapped to a range from 0 to 1023. Where in the range the value falls defines the step size for these loops:
Code: [Select]
    for(int i=0; i<=height; i+=y)
    {
      for(int j=0; j<=width; j+=x)
      {
        float blah = noise(xnoise, ynoise, znoise)*256;
        fill(blah);
        rect(j, i, x,y);

        if(key=='c' || key == 'C')
        {
          fill(i, blah, j);
          rect(j, i, x,y);
        }

        xnoise += 0.09;
      }
      ynoise += 0.09;
    }

The original minimum rectangle size was 1 by 1. That's 800,000 rectangles to draw. And you wonder why it takes time to draw 800,000 rectangles.


ahhreeyell

Yes, that's why I changed the minimum size to 2x2 pixels.

What about the 3D, though? Is there anything I can do to improve the speed?

PaulS

Code: [Select]
    box(random(herp), random(herp), random(.5*(herp+herp)) );
Even without taking my shoes off, I'm pretty sure that (herp + herp)/2 is going to equal herp.

I don't know if there is anything you can do to speed up the 3D stuff. Nothing in that block looks particularly slow. Perhaps some print statements to show when things occur would provide a clue. Knowing which statements take the longest would be useful.

Nick Gammon

Try reducing height and width and see if that has any effect.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up