Skip to content

Instantly share code, notes, and snippets.

@ohmygodwin
Created November 6, 2013 21:54
Show Gist options
  • Save ohmygodwin/7344765 to your computer and use it in GitHub Desktop.
Save ohmygodwin/7344765 to your computer and use it in GitHub Desktop.
import processing.serial.*;
import processing.pdf.*;
Serial myPort;
//handshake method for serial communication
boolean firstContact = false;
//shape arrays
SquareSpin[] square = new SquareSpin[5000];
CircleSpin[] circle = new CircleSpin[5000];
//serial for shape size, scale, color
int xpos, ypos, dist, color1, color2, color3;
int squareCount = 0;
int circleCount = 0;
void setup() {
size(662, 662);
int distance = 40;
for (int a = 0; a <= distance*22; a = a + distance) {
for (int b = 0; b <= distance*22; b = b + distance) {
square[squareCount] = new SquareSpin(a-325, b-325);
squareCount++;
circle[circleCount] = new CircleSpin(a-325, b-325);
circleCount++;
}
}
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void draw() {
if (record == 1) {
saveFrame("frame####.jpg");
delay(1000);
record == 0;
}
//mapping distance
float d = map(dist, 0, 884, 40, 100);
int distance = round(d);
//mapping color
float c1 = map(color1, 0, 900, 0, 255);
float c2 = map(color2, 0, 900, 0, 255);
float c3 = map(color3, 0, 900, 0, 255);
background(c1, 180, 180);
pushMatrix();
translate(width/128, height/128);
float sqSize = map(xpos, 0, 1023, 7, 100);
float cirSize = map(ypos, 0, 1023, 7, 100);
squareCount = 0;
circleCount = 0;
for (int a = 0; a <= distance*22; a = a + distance) {
for (int b = 0; b <= distance*22; b = b + distance) {
square[squareCount].setPos(a-325, b-325);
squareCount++;
circle[circleCount].setPos(a-325, b-325);
circleCount++;
}
}
int sq = 0;
int cir = 0;
while (sq < squareCount) {
square[sq].go();
square[sq].show(200, c2, 200);
square[sq].updateSize(sqSize);
sq++;
}
while (cir < circleCount) {
circle[cir].go();
circle[cir].show(130, 150, c3);
circle[cir].updateSize(cirSize);
cir++;
}
popMatrix();
}
void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil('\n');
if (myString != null) {
myString = trim(myString);
if (firstContact == false) {
if (myString.equals("hello")) {
myPort.clear();
firstContact = true;
myPort.write('A');
}
}
else {
int sensors[] = int(split(myString, ','));
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
println();
if (sensors.length > 1) {
color1 = sensors[0];
color2 = sensors[1];
color3 = sensors[2];
dist = sensors[3];
xpos = sensors[4];
ypos = sensors[5];
record = sensors[6];
}
}
myPort.write("A");
}
}
class Spin {
float speed = random(-0.05, 0.05);
float angle = 0;
float d;
int x, y;
float hueSq, saturationSq, brightnessSq;
float hueCi, saturationCi, brightnessCi;
float hue2, brightness2, saturation2;
Spin(int xpos2, int ypos2) {
x = xpos2;
y = ypos2;
}
void go() {
angle += speed;
}
void updateSize(float size) {
d = size;
}
void setPos(int _x, int _y){
x = _x;
y = _y;
}
}
class SquareSpin extends Spin {
SquareSpin(int x, int y) {
super(x, y);
}
void show(float hueSq, float saturationSq, float brightnessSq) {
pushMatrix();
translate(((width/2)+x), ((height/2)+y));
rotate(angle);
rectMode(CENTER);
noStroke();
fill(hueSq, saturationSq, brightnessSq, 200);
rect(x/128, y/128, d, d);
popMatrix();
}
}
class CircleSpin extends Spin {
CircleSpin(int x, int y) {
super(x, y);
}
void show(float hueCi, float saturationCi, float brightnessCi) {
pushMatrix();
translate(((width/2)+x), ((height/2)+y));
rotate(angle);
ellipseMode(CORNER);
noFill();
strokeWeight(2);
stroke(hueCi, saturationCi, brightnessCi);
ellipse(x/128, y/128, d, d);
popMatrix();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment