
256 MAKING THINGS TALK
Continued from previous page.
// read <numSamples> 10-bit analog values, two at a time
// because each reading is two bytes long:
for (int i = 0; i < numSamples * 2; i=i+2) {
// 10-bit value = high byte * 256 + low byte:
int thisSample = (thisPacket[i + adcStart] * 256) +
thisPacket[(i + 1) + adcStart];
// put the result in one of 5 bytes:
adcValues[i/2] = thisSample;
// add the result to the total for averaging later:
total = total + thisSample;
}
// average the result:
int average = total / numSamples;
print("Average reading:" + average + "\t");
// print the received signal strength:
println("Signal Strength:" + rssi);
}
Now that you’ve got the average
reading printing out, add some code to
graph the result. For this, you’ll need a
new global variable before the setup()
method that keeps track of where you
are horizontally on the graph:
8
int hPos = 0; // horizontal position on the graph
Now add a new method,
drawGraph(), to the end of the
program:
Call this from the parseData() method,
replacing the println() statement that
prints out the average, as well as the
println() statement that prints out the
signal strength (rssi), like so:
// draw a line on the graph:
drawGraph(average/4);
Now when you run the program, it
should draw a graph of the sensor
readings, updating every time it gets
a new datagram.
8
/*
upd