/** * Serial Call-Response * by Tom Igoe. * * Sends a byte out the serial port, and reads 3 bytes in. * Sets foregound color, xpos, and ypos of a circle onstage * using the values returned from the serial port. * Thanks to Daniel Shiffman and Greg Shakar for the improvements. * * Note: This sketch assumes that the device on the other end of the serial * port is going to send a single byte of value 65 (ASCII A) on startup. * The sketch waits for that byte, then sends an ASCII A whenever * it wants more data. */ import processing.serial.*; import ddf.minim.*; Minim minim; AudioSample kick; AudioSample snare; AudioSample one; AudioSample two; int bgcolor; // Background color int fgcolor; // Fill color Serial myPort; // The serial port int[] serialInArray = new int[3]; // Where we'll put what we receive int serialCount = 0; // A count of how many bytes we receive int xpos, ypos; // Starting position of the ball boolean firstContact = false; // Whether we've heard from the microcontroller AudioSample[] samples = new AudioSample[6]; int sampleCounter = 0; int stepCounter = 0; void setup() { size(256, 256); // Stage size noStroke(); // No border on the next thing drawn minim = new Minim(this); // load BD.wav from the data folder //kick = minim.loadSample( "BD.mp3", // filename samples[0] = minim.loadSample("1eng2.aiff", 512); samples[1] = minim.loadSample("2eng2.aiff", 512); samples[2] = minim.loadSample("1eng2.aiff", 512); samples[3] = minim.loadSample("2eng2.aiff", 512); samples[4] = minim.loadSample("1eng2.aiff", 512); samples[5] = minim.loadSample("2eng2.aiff", 512); one = samples[sampleCounter]; two = samples[sampleCounter + 1]; // Print a list of the serial ports, for debugging purposes: printArray(Serial.list()); // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. String portName = Serial.list()[5]; myPort = new Serial(this, portName, 9600); } void draw() { } void serialEvent(Serial myPort) { // read a byte from the serial port: int inByte = myPort.read(); println(inByte); //if ( inByte == 0 ) one.trigger(); if ( inByte % 2 == 0 ) { one.trigger(); } else { two.trigger(); } //if ( inByte == 1 ) two.trigger(); stepCounter = stepCounter + 1; if(stepCounter > 100) { changeLanguage(); stepCounter = 0; } } void changeLanguage() { sampleCounter = sampleCounter + 2; if (sampleCounter >= samples.length) { sampleCounter = 0; } one = samples[sampleCounter]; two = samples[sampleCounter + 1]; }