Hardware Reference
In-Depth Information
Let's take a break from writing code
and test out the sketch. Make sure
you've closed the Serial Monitor or
serial port in your serial terminal appli-
cation so that it releases the serial port.
Then run this Processing application.
You should see a list of the sensor
values in the console and in the applet
window, as shown on the right.
Next, it's time to use the data to
play pong. First, add a few variables at
the beginning of the Processing sketch
before the setup() method, and change
the setup() to set the window size and
initialize some of the variables (the new
lines are shown in blue).
8
float leftPaddle, rightPaddle; // variables for the flex sensor values
int resetButton, serveButton; // variables for the button values
int leftPaddleX, rightPaddleX; // horizontal positions of the paddles
int paddleHeight = 50; // vertical dimension of the paddles
int paddleWidth = 10; // horizontal dimension of the paddles
float leftMinimum = 120; // minimum value of the left flex sensor
float rightMinimum = 100; // minimum value of the right flex sensor
float leftMaximum = 530; // maximum value of the left flex sensor
float rightMaximum = 500; // maximum value of the right flex sensor
NOTE: The variables relating to the paddle
range in this example are floating-point
numbers ( floats ), because when you divide
integers, you get integer results only. For
example, 480/400, gives 1, not 1.2, when
both are integers. Likewise, 400/480
returns 0, not 0.8333. Using integers when
you're dividing two numbers that are in the
same order of magnitude produces useless
results. Beware of this when using scaling
functions like map() .
void setup() {
size(640, 480); // set the size of the applet window
String portName = Serial.list()[0];
// open the serial port:
myPort = new Serial(this, portName, 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
// initialize the sensor values:
leftPaddle = height/2;
rightPaddle = height/2;
resetButton = 0;
serveButton = 0;
// initialize the paddle horizontal positions:
leftPaddleX = 50;
rightPaddleX = width - 50;
// set no borders on drawn shapes:
noStroke();
}
 
Search WWH ::




Custom Search