import cc.arduino.*;
import org.firmata.*;
import processing.serial.*;
import processing.opengl.*;

Serial port;
PVector[] objects = new PVector[20];
float x;
float y;

int in_data;
int backgroundCol;

void setup() {
  size(800, 400, P3D);
  port = new Serial(this, "/dev/cu.usbserial-FTBOAA5E", 9600);
  background(0, 0, 0);
  textAlign(CENTER);
  for (int i = 0; i < 20; i++) {
    objects[i] = new PVector(width/2 + random(-200,200), height/2 + random(-200,200), 0);
  }
}

void draw() {
  
  for(int i = 0 ; i < 20; i++){
    pushMatrix();
    PVector o = objects[i];
    translate(o.x, o.y, o.z);
    fill(random(255));
    box(20,20,20);
    popMatrix();
  }


  if (port.available() > 0) {
    in_data = port.read();
    backgroundCol = 255 - in_data;
    background(backgroundCol);

    x = width/2 + random(-3, 3);
    y = height/2 + random(-3, 3);

    textSize(in_data/10);
    fill(255);
    text(in_data, width/2, height/2);

    noFill();
    stroke(random(255), random(255), 255);
    ellipse(x, y, in_data, in_data);
    noStroke();
    fill(random(150), random(150), random(150), 10);
  }

  camera(mouseX, 0, mouseY, width/2, height/2, 0, 0, 1, 0);
}
