Figure 20 An empty Proto Shield (left), and one holding our game controller (right)
const unsigned int
BUFFER_SIZE = 16;
const unsigned int
BAUD_RATE = 38400;
int
buffer[NUM_AXES][BUFFER_SIZE];
int
buffer_pos[NUM_AXES] = { 0 };
boolean button_pressed = false;
Bounce button;
void
setup() {
Serial.begin(BAUD_RATE);
pinMode(BUTTON_PIN, INPUT);
button.attach(BUTTON_PIN);
button.interval(20);
}
int get_axis(const int
delay(1);
buffer[axis][buffer_pos[axis]] = analogRead(PINS[axis]);
buffer_pos[axis] = (buffer_pos[axis] + 1) % BUFFER_SIZE;
long
sum = 0;
for (unsigned int
i = 0; i < BUFFER_SIZE; i++)
sum += buffer[axis][i];
return
round(sum / BUFFER_SIZE);
}
int
get_x() {
return
get_axis(0); }
int
get_y() {
return
get_axis(1); }
int
get_z() {
return
get_axis(2); }
Chapter 6. Building a Motion-Sensing Game Controller
axis) {
www.it-ebooks.info
108
report erratum
discuss
Need help?
Do you have a question about the Arduino Uno and is the answer not in the manual?