pyramidal trash: Arduino + Processing

Arduino controlled Processing sketch.

pyramidal trash from Ricardo Munoz on Vimeo.

 

Arduino code:

int sensorValue;
int sensorPin;

void setup() {

// initialize the serial communication:
Serial.begin(9600);
sensorPin = A0;
}

void loop() {
sensorValue = analogRead(sensorPin);
delay(10);
// send the value of analog input 0:
Serial.println(sensorValue);

}

Processing sketch

/**
* Based on Space Junk by Ira Greenberg.
*
* Rotating pyramids in space using a custom Pyramid class.
* Color controlled by a potentiometer connected to Arduino
*/

import processing.opengl.*;

import processing.serial.*;

Serial myPort; // The serial port

float sensorValue;

// overall rotation
float ang;

//Pyramid count
int limit = 50;

//Array for Pyramids

Pyramid[]pyramids = new Pyramid[limit];

void setup() {
size (600, 600, OPENGL);
background(35);
noStroke();

//init Pyramids, values for size and pos

for (int i = 0; i < pyramids.length; i++) {
pyramids[i] = new Pyramid(int(random(0, 40)), int(random(-140, 140)), int(random(-140, 140)),
int(random(-sensorValue, 140)), int(random(1, 20)));
}

// List all the available serial ports
println(Serial.list());

myPort = new Serial(this, Serial.list()[0], 9600);
// don’t generate a serialEvent() unless you get a newline character:
myPort.bufferUntil(‘\n’);
}

void draw() {
background(35);
//colored lights
pointLight(51, 102, 255, 65, 60, 100);
pointLight(200, 40, 60, -65, -60, -150);

//overall light
ambientLight(70, 70, 10);

//change center of doc
//translate (width/2, height/2, -200 + mouseX);
translate (width/2, height/2, sensorValue + sensorValue);

//rotate around x and y axes
rotateY(radians(ang));
rotateX(radians(ang));

//draw Pyramids
for (int i = 0; i < pyramids.length; i++) {
pyramids[i].drawPyramid();
}

//used in rotation
ang++;
}

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil(‘\n’);
println (inString);

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);

sensorValue = map(inByte, 22, 1023, 10, 300);

}

}

Pyramid class

// Pyramid construction learned @ Learning Processing

// Example 14-10: Pyramid by Daniel Shiffman

// http://www.learningprocessing.com

class Pyramid {

// properties
int t;
int shiftX, shiftY, shiftZ;
int rot;

//constructor
Pyramid(int t, int shiftX, int shiftY, int shiftZ, int rot) {
this.t = t;
this.shiftX = shiftX;
this.shiftY = shiftY;
this.shiftZ = shiftZ;
this.rot = rot;
}
void drawPyramid() {
noStroke();

//pyramid has 4 sides, each side is a separate triangle
//each triangle has 3 vertices
// “t” determines the size of the pyramid

beginShape(TRIANGLES);

fill(255, 0, 0, 127);
vertex(-t + shiftX, -t + shiftY, -t + shiftZ);
vertex( t + shiftX, -t + shiftY, -t + shiftZ);
vertex( 0 + shiftX, 0 + shiftY, t + shiftZ);

fill(255, 0, 0, 127);
vertex( t + shiftX, -t + shiftY, -t + shiftZ);
vertex( t + shiftX, t + shiftY, -t + shiftZ);
vertex( 0 + shiftX, 0 + shiftY, t + shiftZ);

fill(255, 0, 0, 127);
vertex( t + shiftX, t + shiftY, -t + shiftZ);
vertex(-t + shiftX, t + shiftY, -t + shiftZ);
vertex( 0 + shiftX, 0 + shiftY, t + shiftZ);

fill(255, 0, 0, 127);
vertex(-t + shiftX, t + shiftY, -t + shiftZ);
vertex(-t + shiftX, -t + shiftY, -t + shiftZ);
vertex( 0 + shiftX, 0 + shiftY, t + shiftZ);

rotate(PI*rot);
endShape();
}
}

Advertisements

Your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s