Hardware Reference
In-Depth Information
The 3D system in Processing
works on rotations from zero to 2*PI.
tilt() maps the accelerometer angles
into that range. It uses Processing's
translate() and rotate() methods to
move and rotate the plane of the disc
to correspond with the accelerometer's
movement.
8
void tilt() {
// translate from origin to center:
translate(position, position, position);
// X is front-to-back:
rotateX(radians(roll + 90));
// Y is left-to-right:
rotateY(radians(pitch) );
// set the disc fill color:
fill(#79BF3D);
// draw the disc:
ellipse(0, 0, width/4, width/4);
// set the text fill color:
fill(#20542E);
// Draw some text so you can tell front from back:
text(pitch + "," + roll, -40, 10, 1);
}
8
The serialEvent() method reads all
the incoming serial bytes and parses
them as comma-separated ASCII
values, just as you did in Project 2,
Monski pong in Chapter 2.
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// split the string at the commas
String items[] = split(myString, ',');
if (items.length > 1) {
pitch = float(items[0]);
roll = float(items[1]);
}
}
}
 
Search WWH ::




Custom Search