void loop() {
Serial.print(get_x());
Serial.print(" ");
Serial.print(get_y());
Serial.print(" ");
Serial.print(get_z());
Serial.print(" ");
if (button.update()) {
button_pressed = button.read() == HIGH;
}
Serial.println(button_pressed == HIGH ? "1" : "0");
delay(10);
}
As in Building a Dice Game, on page 55, we use the
Bounce
class to debounce
the button. The rest of the code is pretty much standard, and the only thing
worth mentioning is that we use a 38,400 baud rate to transfer the controller
data sufficiently fast.
Compile and upload the code, open the serial terminal, and play around with
the controller. Move it, press the button sometimes, and it should output
something like the following:
324 365 396 0
325 364 397 0
325 364 397 1
325 364 397 0
325 365 397 0
325 365 397 1
326 364 397 0
A homemade game controller is nice, and you can use it for many projects.
For example, you could use it to control devices such as a robot, a marble
maze, or something similar. Its original purpose is to control games, so we’ll
build one in the next chapter.
More Projects
If you keep your eyes open, you’ll quickly find many more applications for
accelerometers than you might imagine. Here’s a small collection of both
commercial and free products:
report erratum • discuss
More Projects • 109
www.it-ebooks.info