/**
* Physical Computing
* Serial port responsive visualization
* Physical device used: microphone
* Sources: Particle System http://www.processing.org/examples/multipleparticlesystems.html
* "Sing Me" Example with Microphone from http://www.dtic.upf.edu/~jlozano/interfaces/microphone.html
* http://www.dtic.upf.edu/~jlozano/interfaces/piezo_sopla/singme.pde
*/
import processing.serial.*;
final int MaxPortVal = 255;
String portname = "COM8";
Serial port;
int micro = 0;
float x = (250); // X-coordinate of ParticleSystem
float y = (250); // Y-coordinate of ParticleSystem
ArrayList<ParticleSystem> systems;
void setup() {
size(500, 500);
systems = new ArrayList<ParticleSystem>();
port = new Serial(this, portname, 57600);
}
void draw()
{
rect(0, 0, width, height);
if (port != null && port.available() > 0) {
// Note: my device is only reading 0 or 244
// displays bursts of particles to respond to on/off audio feedback
micro = port.read();
//println(micro);
port.clear();
// shift systems more sporadically when feedback is larger
x = x + random(-12, 12) + random(-2,2)*micro/2 ;
y = y + random(-12, 12) + random(-2,2)*micro/2;
}
background(0);
// if microphone is recieving significant feedback
if(micro > 200){
// max number of systems is 3
if(systems.size() < 3){
ParticleSystem ps = new ParticleSystem(1, new PVector(x, y));
systems.add(ps);
} else {
for(ParticleSystem ps: systems){
// add more particles to each system in a burst
// Note: causes framerate to drop
//ps.addBurst();
}
}
}
for (ParticleSystem ps: systems) {
ps.run();
ps.addParticle();
ps.origin.x = x;
ps.origin.y = y;
}
if (systems.isEmpty()) {
fill(255);
}
if (x>width-10 || y > height-10 || y < 10 || x < 10 ){
x = (500); // X-coordinate of text
y = (350); // Y-coordinate of text
}
}
// A simple Particle class
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float lifespan;
int[] colors;
Particle(PVector l) {
acceleration = new PVector(0,0.05);
velocity = new PVector(random(-1,1),random(-2,0));
location = l.get();
lifespan = 255.0;
colors = new int[3];
// Note: having issues with initalizing
// Fix for future (currently particles not initialized without assigned color)
//colors = {255,255,255};
}
// overloaded initializer - added to example Particle class
// added color
Particle(PVector l, int[] newColors) {
acceleration = new PVector(0,0.05);
velocity = new PVector(random(-1,1),random(-2,0));
location = l.get();
lifespan = 255.0;
colors = newColors;
}
void run() {
update();
display();
}
// Method to update location
void update() {
velocity.add(acceleration);
location.add(velocity);
lifespan -= 2.0;
}
// Method to display
void display() {
//stroke(colors[0],colors[1],colors[2], lifespan);
fill(colors[0],colors[1],colors[2], lifespan);
ellipse(location.x,location.y,8,8);
}
// Is the particle still useful?
boolean isDead() {
return (lifespan < 0.0);
}
// adjust particle velocity - added
// Note: current version does not use
void addVelocity(){
velocity.x *= 2;
velocity.y *= 2;
}
// A subclass of Particle
class CrazyParticle extends Particle {
// Just adding one new variable to a CrazyParticle
// It inherits all other fields from "Particle"
float theta;
// The CrazyParticle constructor can call the parent class (super class) constructor
CrazyParticle(PVector l) {
// "super" means do everything from the constructor in Particle
super(l);
// One more line of code to deal with the new variable, theta
theta = 0.0;
}
CrazyParticle(PVector l, int[] colors){
super(l,colors);
theta = 0.0;
}
// Notice we don't have the method run() here; it is inherited from Particle
// This update() method overrides the parent class update() method
void update() {
super.update();
// Increment rotation based on horizontal velocity
float theta_vel = (velocity.x * velocity.mag()) / 10.0f;
theta += theta_vel;
}
// This display() method overrides the parent class display() method
void display() {
// Render the ellipse just like in a regular particle
super.display();
// Then add a rotating line
pushMatrix();
translate(location.x,location.y);
rotate(theta);
stroke(255,lifespan);
line(0,0,25,0);
popMatrix();
}
// An ArrayList is used to manage the list of Particles
ArrayList<Particle> particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
int[] colors = {255,255,255};
ParticleSystem(int num, PVector v) {
particles = new ArrayList<Particle>(); // Initialize the arraylist
origin = v.get(); // Store the origin point
for(int j=0; j<3;j++){
colors[j] = floor(random(255));
}
for (int i = 0; i < num; i++) {
Particle p = new Particle(origin, colors);
//p.changeColor(colors[0],colors[1],colors[2]);
particles.add(p); // Add "num" amount of particles to the arraylist
}
}
void run() {
// Cycle through the ArrayList backwards, because we are deleting while iterating
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
void addParticle() {
Particle p;
// Add either a Particle or CrazyParticle to the system
if (int(random(0, 2)) == 0) {
p = new Particle(origin, colors);
}
else {
p = new CrazyParticle(origin, colors);
}
particles.add(p);
}
void addParticle(Particle p) {
particles.add(p);
}
void addBurst(){
for(int i=0;i<5;i++){
Particle p = new CrazyParticle(origin);
p.addVelocity();
particles.add(p);
}
}
// A method to test if the particle system still has particles
boolean dead() {
return particles.isEmpty();
}
}