Skip to content

Instantly share code, notes, and snippets.

@lucasdinonolte
Created January 12, 2015 20:37
Show Gist options
  • Select an option

  • Save lucasdinonolte/328e3d28ed9fae545134 to your computer and use it in GitHub Desktop.

Select an option

Save lucasdinonolte/328e3d28ed9fae545134 to your computer and use it in GitHub Desktop.
Trippy @daelzicht
// Run with AllInputsFirmata
import themidibus.*; //Import midibus library
import processing.serial.*; //Import serial library
import cc.arduino.*;
// Wanna use midi input?
boolean useMidi = false;
// Arduino Ports
int button = 7;
int distanceSensor = 0;
// Color Ports
int redPin = 5;
int greenPin = 6;
int bluePin = 3;
// Arduino stuff
Arduino arduino;
Arduino arduino2;
MidiBus myBus; // The MidiBus
Serial port;
int shapeCount = 500;
Rectangle[] shapes = new Rectangle[shapeCount];
float distance = 4;
boolean backwards = false;
int angle = 0;
color bg = color(0);
color nextColor;
float distanceTo = 0;
boolean flipped = false;
color off = color(255); // Background
color on = color(255); // Stars
int stars = shapeCount; // Number of Stars
Star[] starArray = new Star[stars]; // Array of stars
ImageBall photo;
float divider = 1; // Divider to speed up or slow down the zooming
int numReadings = 40; // Do 40 readings to calculate an average of the distance
float threshold = 1.15; // Threshold that triggers an animation
int readings[] = new int[numReadings];
int index = 0;
int total = 0;
int average = 0;
color[] favoriteColors = {#FF707A, #D26222, #127A83, #D1B79C};
int beat = 0;
long duration = 0;
long distance_old = 0;
int oldVal = 0;
int lastDist = 0;
int scene = 0;
int wait = 10;
void setup() {
if(useMidi) {
myBus = new MidiBus(this, 0, 1);
}
size(1280, 800, P3D);
background(bg);
smooth();
frameRate(100);
port = new Serial(this, "/dev/tty.usbmodem1411", 9600);
// You can use the output of this list, to find the right arduino to use
println(Arduino.list());
// Knöpfe
// Last specified number is the Baud Rate
arduino = new Arduino(this, Arduino.list()[7], 57600);
arduino.pinMode(button, Arduino.INPUT);
}
void noteOn(int channel, int pitch, int velocity) {
if(pitch == 36) {
addShape();
}
if(channel == 1) {
distanceTo = pitch / 10;
}
if(channel == 2) {
if(pitch > 50) {
nextColor = favoriteColors[1];
}
if(pitch > 53) {
nextColor = favoriteColors[0];
}
if(pitch > 55) {
nextColor = favoriteColors[3];
}
if(pitch > 58) {
nextColor = favoriteColors[2];
}
}
}
void addShape() {
shapes[beat] = new Rectangle();
starArray[beat] = new Star();
beat++;
port.write((int) random(3));
if(beat > starArray.length) {
beat = 0;
}
beat++;
if(beat >= shapes.length) {
beat = 0;
}
}
void serialEvent(Serial Port) {
total = total - readings[index];
readings[index] = (int) Float.parseFloat(port.readStringUntil('\n'));
total = total + readings[index];
index = index + 1;
if(index >= numReadings) {
index = 0;
}
average = total / numReadings;
//distance = 4;
}
void draw() {
// To allow for fading
background(bg);
if(!useMidi && frameCount%50 == 0) {
addShape();
}
//arduino2.analogWrite(redPin, (int) random(256));
//arduino2.analogWrite(greenPin, (int) random(256));
//arduino2.analogWrite(bluePin, (int) random(256));
total = total - readings[index];
readings[index] = arduino.analogRead(0);
total = total + readings[index];
index += 1;
if(index >= numReadings)
index = 0;
int dist = total / numReadings;
// Only apply if a threshold is crossed
if(abs(dist - oldVal) > threshold) {
translate(0, 0, dist/divider*-1);
lastDist = (int) distance;
} else {
translate(0, 0, lastDist/divider*-1);
}
// Cache the old value
oldVal = (int) distance;
if(arduino.digitalRead(button) == Arduino.HIGH && wait == 10) {
changeScene();
wait--;
}
if(arduino.digitalRead(button) == Arduino.LOW) {
wait = 10;
}
if(scene == 1) {
if(distanceTo != 0) {
if(distanceTo > (distance + 0.03)) {
distance = distance + 0.005;
} else if(distanceTo < (distance - 0.03)) {
distance = distance - 0.005;
}
}
rotateX(PI/(distance));
if(flipped) {
rotateX((PI/distance) * -1);
}
for(int i = 0; i < shapes.length; i++) {
if(shapes[i] != null) {
shapes[i].drawMe();
}
}
if(photo != null) {
photo.drawMe();
}
}
if(scene == 0) {
stroke(on);
// Draw the Photo
//photo.drawMe();
// Draw the stars
for(int i = 0; i < starArray.length; i++) {
if(starArray[i] != null) {
starArray[i].drawMe();
}
}
}
}
class Rectangle {
color fill;
int wide;
int high;
int currentHigh = 0;
int x;
float y;
float opacity;
float speed;
Rectangle() {
fill = favoriteColors[(int) random(favoriteColors.length)];
wide = (int) random(15, 25);
high = (int) random(80, 130);
x = (int) random(width);
y = 0;
opacity = random(100, 255);
speed = random(1.5, 3.5);
}
void drawMe() {
fill(fill, opacity);
noStroke();
rect(x, y, wide, currentHigh);
// Speed!
y = y+speed;
// Growth
if(currentHigh < high) {
currentHigh = currentHigh + 8;
}
}
}
void keyPressed() {
changeScene();
}
void changeScene() {
// Insert Randomnes here!
// Farbwechsel, Szenenwechsel, Fotos
if(scene == 0) {
scene = 1;
} else {
scene = 0;
}
}
class ImageBallRect {
float x;
float y;
float diameter;
float xmove, ymove;
int ftl = 255;
PImage photo;
ImageBallRect() {
x = (int) random(width/2) + (width/3);
y = 0;
diameter = random(15, 40);
ymove = random(1, 2); // Movement speed of Stars Y
photo = loadImage("photos/photo.jpg");
}
void drawMe() {
noStroke();
y += ymove;
image(photo, x, y, photo.width/2, photo.height/2);
ftl--;
}
}
class Star {
float x;
float y;
float diameter;
float xmove, ymove;
color fill;
float opacity;
float ftl = 3000;
float framesLived = 0;
PImage bg;
Star() {
x = random(width);
y = random(height);
diameter = random(40);
xmove = random(-0.5, 0.5)/4; // Movement speed of Stars X
ymove = random(-0.5, 0.5)/4; // Movement speed of Stars Y
fill = favoriteColors[(int) random(favoriteColors.length)];
opacity = (int) random(100, 255);
}
void drawMe() {
noStroke();
x += xmove;
y += ymove;
if (x > (width+diameter)) { x = 0 - diameter; }
if (x < (0-diameter)) { x = width+diameter; }
if (y > (height+diameter)) { y = 0 - diameter; }
if (y < (0-diameter)) { y = height+diameter; }
opacity = ((ftl - framesLived)/ftl) * 255;
fill(fill, opacity);
framesLived++;
ellipse(x, y, diameter, diameter);
}
}
class ImageBall {
float x;
float y;
float diameter;
float xmove, ymove;
int ftl = 255;
PImage photo;
ImageBall() {
x = random(width);
y = random(height);
diameter = random(15, 40);
xmove = random(1)/4; // Movement speed of Stars X
ymove = random(1)/4; // Movement speed of Stars Y
photo = loadImage("photos/photo.jpg");
}
void drawMe() {
int opacity = ftl;
noStroke();
x += xmove;
y += ymove;
if (x > (width+diameter)) { x = 0 - diameter; }
if (x < (0-diameter)) { x = width+diameter; }
if (y > (height+diameter)) { y = 0 - diameter; }
if (y < (0-diameter)) { y = height+diameter; }
tint(255, opacity);
image(photo, x, y, photo.width/2, photo.height/2);
ftl--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment