Hardware Reference
In-Depth Information
Continued from previous page.
NOTE: You should disconnect the XBee's
receive and transmit connections to the
microcontroller while programming (if
you're using the Arduino wireless shield,
use the serial select switch). The serial
communications with the XBee can
interfere with the serial communications
with the programming computer. Once the
microcontroller's programmed, you can re-
connect the transmit and receive lines.
// look for a change from the last reading
// that's greater than the threshold:
if (abs(result - lastSensorReading) > threshold) {
result = result/4;
lastSensorReading = result;
} else {
// if the change isn't significant, return 0:
result = 0;
}
return result;
}
In the main loop, notice that you're not using
any AT commands. That's because the XBee
goes back into data mode (called idle mode in
the XBee user's guide) automatically when you issue the
ATCN command in the setDestination() method.
Once you've programmed the microcontroller, set the des-
tination address on the computer's XBee to the address
of the microcontroller's radio. (If you did this in the earlier
step, you shouldn't need to do it again.) Then, turn the
potentiometer on the microcontroller. You should get a
message like this in your serial terminal window:
Remember, in data mode, any bytes sent to an AT-style
modem go through as-is. The only exception to this rule
is that if the string +++ is received, the modem switches
to command mode. This behavior is the same as that of
the Bluetooth module from Chapter 2, as well as almost
any device that implements this kind of protocol. It's
great because it means that once you're in data mode,
you can send data with no extra commands, letting the
radio itself handle all the error corrections for you.
120
The actual number will change as you turn the potenti-
ometer. It might overwrite itself in the serial window—
depending on your serial terminal application—because
you're not sending a newline character. Congratulations!
You've made your first wireless transceiver link. Keep
turning the potentiometer until you're bored, then move on
to Step 3.
X
Step 3: Two-Way Wireless
Communication Between
Microcontrollers
microcontroller. When the microcontroller receives a number
in the serial port, it uses it to set the brightness of an LED on
pin 3.
First, connect the second XBee module to the second
microcontroller. Then, program both microcontrollers with
the previous program, making sure to set the destination
addresses as noted in the program.
This step is simple. All you have to do is replace the
computer in the previous step with a second micro-
controller. Connect an Arduino to your second XBee
module, as shown in Figure 6-16, or use a wireless shield.
The program for both microcontrollers will be almost
identical to each other; only the destination address of
the XBee radio will be different. This program will both
send and receive data over the modules. Turning the
potentiometer causes it to send a number to the other
When you've programmed both modules, power them on and
turn the potentiometer several times. As you turn the potenti-
ometer, the LED on pin 3 of the other module should fade up
and down. Now you've got the capability for duplex wireless
communication between two microcontrollers. This opens up
all kinds of possibilitiesfor interaction.
X
 
Search WWH ::




Custom Search