
290 MAKING THINGS TALK
»
Continued from previous page.
void loop() {
// read 2 channels of the accelerometer:
for (int i = 0; i < 2; i++) {
accelerometer[i] = analogRead(i);
// delay to allow analog-to-digital converter to settle:
delay(10);
}
// if there's serial data in, print sensor values out:
if (Serial.available() > 0) {
// read incoming data to clear serial input buffer:
int inByte = Serial.read();
for (int i = 0; i < 2; i++) {
// values as ASCII strings:
Serial.print(accelerometer[i], DEC);
// print commas between values:
Serial.print(",");
}
// print \r and \n after values are sent:
Serial.println();
}
}
This sketch
uses a call-and-
response serial method, so you won’t
see any data coming out unless you
send it a character serially. You can test
it using your favorite serial terminal
program. With the serial port open in
the serial terminal ...