import krister.Ess.*; import processing.serial.*; Serial port; int val[] = new int[5]; // stores current value for each photoresistor // values for reading serial int zeroCount = 0; // number of sequential zeros we've read int valCounter = 0; // number of values we've read // last time we sent serial data long lastSerial = 0; // ess setup // C Eb G A Bb C float freq[] = new float[] { 261.63, 311.13, 349.23, 392.00, 466.17, 523.26 }; AudioChannel[] myScale = new AudioChannel[6]; SineWave[] myWaves = new SineWave[6]; void setup() { size(400, 400); port = new Serial(this, Serial.list()[3], 9600); noStroke(); smooth(); rectMode(CENTER); Ess.start(this); // initialize channels and waveforms for (int i = 0; i < 6; i++) { myScale[i] = new AudioChannel(); myScale[i].initChannel(myScale[i].frames(5000)); myScale[i].volume(0); myWaves[i] = new SineWave(freq[i], 0.5); myWaves[i].generate(myScale[i], 0, myScale[i].frames(5000)); } // play and pan for (int i = 0; i < 6; i++) { myScale[i].play(Ess.FOREVER); myScale[i].pan(-0.8 + (i/4.0) * 1.6); } myScale[5].pan(-0.8); } void draw() { background(0); for (int i = 0; i < val.length; i++) { pushMatrix(); translate(i*(width/val.length) + (width/val.length)/2, height/2); rotate(TWO_PI/360 * val[i]); rect(0, 0, val[i]/2, val[i]/2); popMatrix(); // fade to the appropriate volume if (myScale[i].fading == false) { myScale[i].fadeTo(val[i] / 255.0, 30); // fade top C as if it were bottom C if (i == 0) { myScale[5].fadeTo(val[i] / 512.0, 30); } } } // send a byte to arduino every half second if (millis() - 1000 > lastSerial) { int secondsPastHour = minute() * 60 + second(); byte byteToSend = byte((abs(1800 - secondsPastHour) / 1800.0) * 127); println(byteToSend); port.write(byteToSend); lastSerial = millis(); } } public void stop() { Ess.stop(); super.stop(); } void serialEvent(Serial p) { int in = port.read(); // wait for five zeros, then read five values; lather, rinse, repeat. if (zeroCount == 5) { if (valCounter < 5) { val[valCounter] = in; valCounter++; return; } else { valCounter = 0; zeroCount = 0; } } if (in == 0) { zeroCount++; } else { zeroCount = 0; } }