Hardware Reference
In-Depth Information
This Processing
sketch reads the
incoming data from the microcontroller
and uses it to change the attitude of
a disc onscreen in three dimensions.
It will work with either of the accel-
erometer sketches above, because
they both output the same data in the
same format. Make sure the serial port
opened by the sketch matches the
one to which your microcontroller is
connected.
Connect It
/*
Accelerometer Tilt
Context: Processing
Takes the values in serially from an accelerometer
attached to a microcontroller and uses them to set the
attitude of a disk on the screen.
*/
import processing.serial.*; // import the serial lib
float pitch, roll; // pitch and roll
float position; // position to translate to
Serial myPort; // the serial port
8
The setup() method initializes the
window, the serial connection, and sets
the graphics smoothing.
void setup() {
// draw the window:
size(400, 400, P3D);
// calculate translate position for disc:
position = width/2;
8 You will probably need
to look at the output of
Serial.list() and change
this number to match
the serial port that cor-
responds to your micro-
controller.
// List all the available serial ports
println(Serial.list());
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[ 2 ], 9600);
// only generate a serial event when you get a newline:
myPort.bufferUntil('\n');
// enable smoothing for 3D:
hint(ENABLE_OPENGL_4X_SMOOTH);
}
The draw() method just refreshes
the screen in the window, as usual.
It calls a method, setAttitude() , to
calculate the tilt of the plane. Then it
calls a method, tilt() , to actually tilt the
plane.
8
void draw () {
// colors inspired by the Amazon rainforest:
background(#20542E);
fill(#79BF3D);
// draw the disc:
tilt();
}
 
Search WWH ::




Custom Search