Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Blinking Jellyfish_procssing code
import processing.serial.*;
int yspacing = 16; // How far apart should each horizontal location be spaced
int h; // Width of entire wave
Serial myPort;
float theta = 0.0; // Start angle at 0
float amplitude = 0; // Height of wave
float period = 500.0; // How many pixels before the wave repeats
float dy; // Value for incrementing X, a function of period and xspacing
float[] xvalues; // Using an array to store height values for the wave
float [] Arr = {
50, 20, 1, -12, -34, -54, -70, -80, -85, -90, -92, -80, -70, -53, -40, -30, -25, -20, -15, -10, -5, -3, -2
};
float off_set = -80;
float R;
float G;
float B;
void setup() {
size(640, 800, P3D);
h = 600;
dy = (TWO_PI / period) * yspacing;
xvalues = new float[h/yspacing];
println(Serial.list());
String portName = Serial.list()[5];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
}
void draw() {
background(0);
calcWave();
renderWave();
}
void calcWave() {
// Increment theta (try different values for 'angular velocity' here
theta -= 0.1;
// For every x value, calculate a y value with sine function
float y = theta;
for (int i = 0; i < xvalues.length; i++) {
xvalues[i] = cos(y)*(amplitude);
y+=dy;
if (amplitude<24) {
amplitude++;
}
}
}
void renderWave() {
strokeWeight(1.2);
//stroke(0, 150, 255, 65);
fill(R, G, B);
for (int i=1; i<100; i++) {
pushMatrix();
translate(320, 0);
rotateY(radians(20*i-180+5));
// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x <xvalues.length; x++) {
if (x<Arr.length) {
if (i%2!=0 &&x>12) {
} else if (i%2==0) {
ellipse( (xvalues[x]+off_set), x*yspacing+90, 9, 9);//body
} else {
ellipse( (xvalues[x]+off_set)+Arr[x], x*yspacing+30, 12, 12);//head
}
} else {
if (i%2 == 0) {
ellipse( xvalues[x]+off_set, x*yspacing+90, 8, 8);//legs
}
}
}
popMatrix();
}
}
void serialEvent(Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the resulting substrings into an integer array:
float[] colors = float(split(inString, ","));
// if the array has at least three elements, you know you got the whole thing. Put the numbers in the
// color variables:
if (colors.length >=3) {
// map them to the range 0-255:
R = colors[0];
G = colors[1];
B = colors[2];
}
}
println(R, G, B);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.