Sararamalama
YaBB Newbies
Offline
Posts: 5
Re: OpenGL + serial library = overload?
Reply #2 - Sep 5th , 2007, 8:32pm
Hi again That might be the case, even though I'm not sure exactly what you mean. We have tried to solve the communication from Arduino into Processing with a seemingly simple routine called Firmata, that unfortunately didn't work the way it should. Now we're using a 'call and response' method created by Tom Igoe. I have tried to simplify the code as much as I could, to post it here if you want to take a look. I've also marked out the passages with code that has to do with the serial reading. Do you have any suggestions of how to change the code, please tell me. Thanks Sara /** * 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 for the improvements. */ import processing.serial.*; import processing.opengl.*; //////////////// SENSOR CODE ////////////////////////////////////////////// int sensor1; int sensor2; int sensor3; 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 boolean firstContact = false; // Whether we've heard from the microcontroller //////////////////////////////////////////////////////////////////////////// // Global variables for the beam float beamX; float beamY; float distA; float beamDir = 1; float beamSize = 50; //////////// tune radius of beam float dy = 0; // Direction float speed = 0.5; //////////// tune speed of pads int beamSpeed = 2; //////////// tune speed of beam // Global variables for paddle float paddleWidth = 5; /////////////////////////////// scale float paddleHeight = 20; /////////////////////////////// scale float paddleY; float earthX; float earthY; float earthSize; void setup() { ///////////////// SENSOR CODE ///////////////// // Print a list of the serial ports, for debugging purposes: //println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Keyspan adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. port = new Serial(this, Serial.list()[0], 9600); port.write(65); // Send a capital A to start the microcontroller sending /////////////////////////////////// frameRate(200); size(1400, 787, OPENGL); rectMode(CENTER_RADIUS); ellipseMode(CENTER_RADIUS); colorMode(HSB,100); //noStroke(); smooth(); beamX = 1; beamY = height/2; distA = width*0.1; paddleY = height; earthX = width*1.05; earthY = height/2; earthSize = height/4; } void draw() { background(55,40,90); noStroke(); //////////////// SENSOR CODE //////////// print(" s1: "); print(sensor1); print(" s2: "); print(sensor2); print(" s3: "); print(sensor3); println(); // Get any new serial data 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); } ///////////////////////////////////////// beamX += beamDir * beamSpeed; beamY += dy; if(beamX > width + beamSize) { beamX = -width/2 - beamSize; beamY = random(0, height); dy = 0; } // Place paddle on screen if(paddleY < height-paddleHeight && paddleY > 0+paddleHeight) { if (sensor1 == 255) { paddleY = paddleY - speed; paddiY = paddiY + speed; padY = padY + speed; } else if (sensor1 == 0) { paddleY = paddleY + speed; paddiY = paddiY + speed; padY = padY + speed; } } if(paddleY >= height-paddleHeight) { paddleY = height-paddleHeight-speed; } if(paddleY <= 0+paddleHeight) { paddleY = paddleHeight+speed; } // Draw beam fill(random(40,60),10,100); ellipse(beamX, beamY, beamSize, beamSize/6); // Draw paddle fill(0,0,100); rect(width - distA, paddleY, paddleWidth, paddleHeight); // Draw earth fill(32,40,70); ellipse(earthX, earthY, earthSize, earthSize); } ////////////// VOIDS /////////////// 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 ) { sensor1 = (int)serialInArray[0]; sensor2 = (int)serialInArray[1]; sensor3 = (int)serialInArray[2]; // Send a capital A to request new sensor readings: port.write(65); // Reset serialCount: serialCount = 0; } }