Pages: 1 2 [3]   Go Down
Author Topic: arduino/processing serial communication being slow  (Read 2539 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nope.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Nope.
Whoosh.

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

Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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);
  }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Are you referring to the 3D part of my Processing code?
Not specifically, we really need to see all of your processing code.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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'));
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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:
    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.

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try reducing height and width and see if that has any effect.
Logged

Pages: 1 2 [3]   Go Up
Jump to: