Skip to content

Instantly share code, notes, and snippets.

@ohmygodwin
Created October 2, 2013 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohmygodwin/6788150 to your computer and use it in GitHub Desktop.
Save ohmygodwin/6788150 to your computer and use it in GitHub Desktop.
code for hooking up two potentiometers to my spinning shapes code
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
int squareCount = 0;
int circleCount = 0;
SquareSpin[] square = new SquareSpin[300];
CircleSpin[] circle = new CircleSpin[300];
float xpos, ypos;
void setup() {
size(662, 662);
for (int a = -320; a <= 320; a = a + 40) {
for (int b = -320; b <= 320; b = b + 40) {
square[squareCount] = new SquareSpin(a, b);
squareCount++;
}
}
for (int c = -320; c <= 320; c = c + 40) {
for (int d = -320; d <= 320; d = d + 40) {
circle[circleCount] = new CircleSpin(c, d);
circleCount++;
}
}
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void draw() {
background(#F5E5C0);
pushMatrix();
translate(width/128, height/128);
int f = 0;
int j = 0;
float x = map(xpos, 0, 1023, 7, 70);
float y = map(ypos, 0, 640, 7, 70);
while (f < squareCount) {
square[f].go();
square[f].show();
square[f].updateSize(x);
f++;
}
while (j < circleCount) {
circle[j].go();
circle[j].show();
circle[j].updateSize(y);
j++;
}
popMatrix();
}
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
if (myString != null) {
println(myString);
}
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
// make sure there are three values before you use them:
if (sensors.length > 1) {
xpos = sensors[0];
ypos = sensors[1];
// the switch values are 0 and 1. This makes them 0 and 255:
}
}
class Spin {
float speed = random(-0.05, 0.05);
float angle = 0;
float d = 20;
int x, y;
Spin(int xpos, int ypos) {
x = xpos;
y = ypos;
}
void go() {
angle += speed;
}
void updateSize(float size) {
d = size;
}
}
class SquareSpin extends Spin {
SquareSpin(int x, int y) {
super(x, y);
}
void show() {
pushMatrix();
translate(((width/2)+x), ((height/2)+y));
rotate(angle);
rectMode(CENTER);
noStroke();
fill(#3F5666, 200);
rect(x/128, y/128, d, d);
popMatrix();
}
}
class CircleSpin extends Spin {
CircleSpin(int x, int y) {
super(x, y);
}
void show() {
pushMatrix();
translate(((width/2)+x), ((height/2)+y));
rotate(angle);
ellipseMode(CORNER);
noFill();
strokeWeight(2);
stroke(#DEB51E);
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