public
Created

code for hooking up two potentiometers to my spinning shapes code

  • Download Gist
spin_serial.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
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();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.