//PONG CONTROLLED WITH POTS /* Utilizing: "Serial call-and-response for v.90 by Tom Igoe Sends a byte out the serial port, and reads 3 bytes in." This is a simple pong game. PIC should use at: A0, A1 POT, A3 Switch */ import processing.serial.*; Serial port; // 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 float input1, input2, input3; // Input variables //here input3 not utilized boolean firstContact = false; // whether we've heard from the microcontroller float left, right, x, y, vx, vy, iv; int kill; void setup() { size(200, 200); stroke(255); smooth(); background(0); left=right=100; x=y=height/2; iv=1.5; vx=iv; vy=0; kill=0; // Set initial input values input1 = height/2; input2 = height/2; // print a list of the serial ports, for debugging purposes: println(Serial.list()); //On Windows machines, this generally opens COM1. port = new Serial(this, Serial.list()[0], 9600); port.write(65); // send a capital A to start the microcontroller sending } void draw() { delaychoice(); background(0); line(0,0,200,0); line(0,199,200,199); left=input1*(200.0/255.0); right=input2*(200.0/255.0); drawpanel(left, 5); println(left); println("e"); drawpanel(right,195); decidethendraw(); callserial(); } //below are the pong functions void drawpanel(float h, float s) { line(s, h+10, s, h-10); } void delaychoice() { if(kill==1) { delay(2000); kill=0; } else { delay(30); } } void decidethendraw() { if(x==10 && (left+10>y && left-10y && right-10199-5) { vy=-1*vy; } x=x+vx; y=y+vy; ellipse(x,y,10,10); if(x<-1) { fill(255,0,0); stroke(255,0,0); rect(50,100,30,30); x=y=100; vx=iv; vy=0; left=right=100; fill(255); stroke(255); kill=1; } if(x>201) { fill(255,0,0); stroke(255,0,0); rect(150,100,30,30); x=y=100; vx=iv; vy=0; left=right=100; fill(255); stroke(255); kill=1; } } //Below are the serial functions void callserial(){ while (port.available() > 0) { serialEvent(); // note that we heard from the microntroller: firstContact = true; } // if there's no serial data, send again until we get some. // (in case you tend to start Processing before you start your external device): if (firstContact == false) { delay(300); port.write(65);} } void serialEvent() { processByte((char)port.read()); } void processByte(char inByte) { // add the latest byte from the serial port to array: serialInArray[serialCount] = inByte; serialCount++; // if we have 3 bytes: if (serialCount > 2 ) { input1 = (float)serialInArray[0]; input2 = (float)serialInArray[1]; input3 = (int)serialInArray[2]; // send a capital A to request new sensor readings: port.write(65); // reset serialCount: serialCount = 0; } }