/* -------------- Blowing Leaves (rev6) by Zach Eveland Should be used with a 1.45 psi pressure sensor (like the kind at the NYU computer store) and an op-amp that delivers about 3.5V full-scale, single-ended. Clicking in the window also makes the leaves move - the decay value is proportional to mouseX. -------------- */ import processing.serial.*; PImage b; //placeholder for the leaf picture Serial myPort; int leaves = 50; //number of leaves to draw at once String accumulation = ""; //holding place for serial data until a full ADC value arrives boolean calibrated = false; //triggers the calibration routine when serial data is first received int calCounter = 0; //used by the calibration routine float calAccumulator = 0; //holding place for pressure sensor ADC values while finding offset float offset = 0; //offset value to adjust the pressure sensor reading - derived from measurements float worldRecord = 0; //highest adjusted pressure sensor value this session - for fun and debugging Leaf[] oLeaf = new Leaf[leaves]; //initialize the leaf object array void setup() { size(400,400); framerate(30); makeLeafObjects(); //call the function that calculates values and creates leaf objects b = loadImage("leaf4.gif"); println(Serial.list()); myPort = new Serial(this, Serial.list()[0], 9600); } void draw() { if(calibrated == false) { //triggers the calibration routine when serial data is first received calibrateSensor(); } background(255); for(int i=0;i= oLeaf[i].decay) { oLeaf[i].animate(asNumbers-offset); } if(asNumbers-offset > worldRecord) { worldRecord = asNumbers - offset; println("world record " + worldRecord); } } } else { calAccumulator = calAccumulator + asNumbers; calCounter = calCounter + 1; if(calCounter == 20) { offset = calAccumulator/calCounter + 5; calCounter = 0; calAccumulator = 0; calibrated = true; println("offset " + offset); } } accumulation = ""; } else { accumulation = accumulation + char(input); } } void mousePressed() { float x = mouseX/10; for(int i=0;i= oLeaf[i].decay) { oLeaf[i].animate(x); } } } void makeLeafObjects() { for(int i=0;i 0) { settle(); xPosDelta = xPosDelta - (1/decay) * xPosDelta; yPosDelta = yPosDelta - (1/decay) * yPosDelta; decay = decay - 1; } else { decay = 0; } } void settle() { xPos = xPos + xPosDelta; yPos = yPos + yPosDelta; rot = rot + rotDelta; if(xPos > width) { xPos = width; xPosDelta = -1 * xPosDelta; } if(xPos < 0) { xPos = 0; xPosDelta = -1 * xPosDelta; } if(yPos > height) { yPos = height; yPosDelta = -1 * yPosDelta; } if(yPos < 0) { yPos = 0; yPosDelta = -1 * yPosDelta; } } void animate(float x) { decay = random(x/2,3*x/4); xPosDelta = random(-decay, decay); yPosDelta = random(-decay, decay); } }