// Program draws a black square in the middle of the screen. // It reacts to multiple key-presses, growing. // Includes import processing.serial.*; // Constants int HIGHVAL = 256; int LOWVAL = 120; int MININTERVAL = 100; float MULTIPLIER = 0.00004; float FORCE_MULTIPLIER = 1.5; float MINSIZE = 100.; float MAXSIZE = 800.; int XSIZE = 1280; int YSIZE = 800; int CONTRACTPERIOD = 1; int INCREMENT=10; // Program Variables int hits = 0; int i=0; int timelasthit = -2000; int hitinterval = 2000; float cursize = 100.; float lastsize = 100.; int inByte = 0; Serial myPort; // The serial port // Program void setup() { println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Keyspan adaptor, so I open Serial.list()[0]. // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[0], 9600); // size(200, 200); size(XSIZE, YSIZE); rectMode(CENTER); noStroke(); fill(0); background(255); } void draw() { float subtractor; hitinterval+=INCREMENT; subtractor = (float)Reciprocal_Logarithmic_model(cursize); cursize = constrain(cursize-subtractor, MINSIZE, MAXSIZE); //println(" - " + cursize + " - " + subtractor); background(255); rect(XSIZE/2, YSIZE/2, cursize, cursize); } double Logarithmic_model(double x_in) { //log model from zunzun.com //fit to data points // 100, 40 // 2000, 1 double temp; temp = 0.0; // coefficients double a = 9.9952499373538743E+01; double b = -1.3018519827118025E+01; temp = a + b*Math.log(x_in); // print (" - " + temp); return temp; } double Reciprocal_Logarithmic_model(double x_in) { //reciprocal log model from zunzun.com //fit to data points // 100, 0 // 800, 40 double temp; temp = 0.0; // coefficients double a = 2.7716949837720617E-01; double b = -3.8471867754609625E-02; temp = 1.0 /(a + b*Math.log(x_in)) + - 10; return temp; } void serialEvent(Serial myPort) { inByte = myPort.read(); int timenow = millis(); hitinterval = (timenow-timelasthit); //println(inByte); //print(hitinterval); timelasthit = timenow; if (hitinterval < 1) hitinterval = 1; lastsize = cursize; // println(((25.0-inByte)/25.0)); cursize = constrain(cursize + (float)(Logarithmic_model(hitinterval)*((25.0-inByte)/25.0)*FORCE_MULTIPLIER), MINSIZE, MAXSIZE); // cursize+=(25-inByte)*FORCE_MULTIPLIER; redraw(); } //void mousePressed() //{ // hits++; // int timenow = millis(); // hitinterval = (timenow-timelasthit); // print(hitinterval); // timelasthit = timenow; // // if (hitinterval < 1) hitinterval = 1; // // lastsize = cursize; // cursize = constrain(cursize + (float)Logarithmic_model(hitinterval), MINSIZE, MAXSIZE); // println(" - " + cursize); // inByte=myPort.read(); // cursize += inByte * FORCE_MULTIPLIER; // print(inByte + " - "); // // redraw(); //}