/* Created by Cory Forsyth Oct 9 2005 This program simulates an etch-a-sketch using serial data from a PIC microcontroller (which gets its numbers from two potentiometers and a switch) based on an example by Dan O'Sullivan and Tom Igoe */ import processing.serial.*; // Get the serial functions Serial myPort; // The serial port int xPos; // horiz position of the nub int yPos; // vert pos of the nub int button = 0; // whether the switch has been pressed int size = 3; // the size of the drawing dot String accumulation = ""; void setup() { // List all the available serial ports: println(Serial.list()); size(255,255); // 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); //prime the pump in case your microcontroller is stuck in serin myPort.write(65); background(255); fill(0); noStroke(); } void draw() { ellipse(xPos,yPos,size,size); /* If the button is pressed, do the erase function. In trying to simulate a real etch-a-sketch, the erase is not perfect...it merely dims the material there. You have to erase a couple times to really get everything gone. We fill a rectangle with edges offscreen with a 30% white to partially cover up the drawing. */ if (button == 1) { //background(255); // erase the screen fill(255,30); rect(-10,-10,width+10,height+10); button = 0; fill(0); } } void serialEvent(Serial p) { int input = myPort.read(); /* if the last thing in was a carriage return, it means that a whole reading is ready */ if (input== 13) { String[] asText = accumulation.split(","); //separate out thereading based on the comma (44) int[] asNumbers = int(asText); /* turn the text reading in to numbers, beware if there is a 13 still attached at the end */ println("accumulation " + accumulation ); if (asNumbers.length >=3){ xPos = asNumbers[0]/4; // this pot will deliver 0-1020 yPos = asNumbers[1]/4; /* easy scaling because it is a potentiometer which delivers solid 0-1020 */ button = asNumbers[2]; println("xPos " + xPos + ", slide " + yPos + ", button " + button); } //now that you have a whole reading, clear your accumulation accumulation = ""; myPort.write(65); } else{ accumulation= accumulation + char(input); //if you did not hear 13, accumulate } }