Hardware Reference
In-Depth Information
Next, add a method to interpret
the protocol. Not surprisingly, this
looks a lot like the parsePacket()
function from the Arduino sketch in the
previous project. Add this method to
the end of your program.
8
void parseData(int[] thisPacket) {
// make sure the packet is 22 bytes long first:
if (thisPacket.length >= 22) {
int adcStart = 11; // ADC reading starts at byte 12
int numSamples = thisPacket[8]; // number of samples in packet
int[] adcValues = new int[numSamples]; // array to hold the 5 readings
int total = 0; // sum of all the ADC readings
To call it, replace the print() and
println() statements in the receive()
method with this:
// read the address. It's a two-byte value, so you
// add the two bytes as follows:
int address = thisPacket[5] + thisPacket[4] * 256;
parseData(inString);
// read the received signal strength:
int signalStrength = thisPacket[6];
// 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;
}
}
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
You'll also need to add a line at the
beginning of the setup() method to
set the size of the window.
// set the window size:
size(400,300);
8
 
Search WWH ::




Custom Search