import processing.serial.*; Serial serialPort; // Create object from Serial class ParticleSystem ps; void setup() { size(600, 600); //set window size background(0); //set background color to black smooth(); ps = new ParticleSystem(new PVector(width/2, 200)); serialPort = new Serial(this, Serial.list()[4], 9600); } void draw() { //if there is data comming in from the serial port while (serialPort.available () > 0) { int light = serialPort.read(); if (light > 200) { background(0); ps.addParticle(); ps.run(); //redraw the screen redraw(); } } } // A class to describe a group of Particles // An ArrayList is used to manage the list of Particles class ParticleSystem { ArrayList particles; PVector origin; ParticleSystem(PVector location) { origin = location.get(); particles = new ArrayList(); } void addParticle() { particles.add(new Particle(origin)); } void run() { Iterator it = particles.iterator(); while (it.hasNext ()) { Particle p = it.next(); p.run(); if (p.isDead()) { it.remove(); } } } } // A simple Particle class class Particle { PVector location; PVector velocity; PVector acceleration; float lifespan; Particle(PVector l) { acceleration = new PVector(0, 1); velocity = new PVector(random(-5, 5), random(-20, -10)); location = l.get(); lifespan = 250.0; } 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(255, lifespan); fill(255, lifespan); ellipse(location.x, location.y, 10, 10); } boolean isDead() { if (lifespan < 0.0) { return true; } else { return false; } } }