/* Serial call-and-response by Tom Igoe (with adjustments by Dan) Sends a byte out the serial port, and reads 3 bytes in. sets background color, x location and rotation of a rect Updated October 12, 2004 */ int bkcolor; // background color int[] serialStuff = new int[3]; // where we'll put what we receive int serialCount = 0; // a count of how many bytes we receive int xpos; // x location of rectangle int rot; // rotation of rectangle BFont font; void setup() { //begin serial communication beginSerial(); size(255, 200); font = loadFont("Garamond-Bold.vlw.gz"); //start off our call and response by sending 65; serialWrite(65); } void loop() { //draw background with red value from sensor background(bkcolor,0,0); textFont(font, 44); fill(255); //display rotation value text(xpos, 5, 30); fill(0,0,255); rectMode(CENTER_DIAMETER); //draw rect at "xpos" and rotate with "rot" translate(xpos,100); rotate(radians(rot*2)); rect(0,0, 50, 50); } void serialEvent() { // add the latest byte from the serial port to array: serialStuff[serialCount] = serial; serialCount++; // if we have 3 things set our variables: if (serialCount > 2 ) { bkcolor = serialStuff[0]; xpos = serialStuff[1]; rot = serialStuff[2]; // clear the string when we're done, and ask for more: serialCount = 0; // send a capital A to request new sensor readings: serialWrite(65); } }