Hardware Reference
In-Depth Information
Test It
Now use the following
code on the Arduino
module to confirm that the sensors
are working.
/*
Sensor Reader
Context: Arduino
Reads two analog inputs and two digital inputs and outputs
their values.
If you open the Serial Monitor in
Arduino—or your preferred serial
terminal application at 9600 bits per
second, as you did in Chapter 1—you'll
see a stream of results like this:
Connections:
analog sensors on analog input pins 0 and 1
switches on digital I/O pins 2 and 3
284,284,1,1,
285,283,1,1,
286,284,1,1,
289,283,1,1,
*/
const int leftSensor = 0; // analog input for the left arm
const int rightSensor = 1; // analog input for tht right arm
const int resetButton = 2; // digital input for the reset button
const int serveButton = 3; // digital input for the serve button
Just as you programmed it, each value
is separated by a comma, and each set
of readings is on a line by itself.
int leftValue = 0; // reading from the left arm
int rightValue = 0; // reading from the right arm
int reset = 0; // reading from the reset button
int serve = 0; // reading from the serve button
void setup() {
// configure the serial connection:
Serial.begin(9600);
// configure the digital inputs:
pinMode(resetButton, INPUT);
pinMode(serveButton, INPUT);
}
void loop() {
// read the analog sensors:
leftValue = analogRead(leftSensor);
rightValue = analogRead(rightSensor);
// read the digital sensors:
reset = digitalRead(resetButton);
serve = digitalRead(serveButton);
// print the results:
Serial.print(leftValue);
Serial.print(",");
Serial.print(rightValue);
Serial.print(",");
Serial.print(reset);
Serial.print(",");
// print the last sensor value with a println() so that
// each set of four readings prints on a line by itself:
Serial.println(serve);
}
 
Search WWH ::




Custom Search