
WIRELESS COMMUNICATION 203
#define sensorPin 0 // input sensor
#define txLed 2 // LED to indicate outgoing data
#define rxLed 3 // LED to indicate incoming data
#define analogLed 9 // LED that changes brightness with incoming value
#define threshold 10 // how much change you need to see on
// the sensor before sending
int lastSensorReading = 0; // previous state of the switch
int inByte= -1; // incoming byte from serial RX
char inString[6]; // string for incoming serial data
int stringPos = 0; // string index counter
First, give the I/O pins
names and set up
some variables for tracking the change
in the switch:
void setup() {
// configure serial communications:
Serial.begin(9600);
// configure output pins:
pinMode(txLed, OUTPUT);
pinMode(rxLed, OUTPUT);
pinMode (analogLed, OUTPUT);
// set XBee's destination address:
setDestination();
// blink the TX LED indicating that the main program's about to start:
blink(3);
}
Next, in the setup() method,
configure serial transmission, set the
modes on the I/O pins, and configure
the XBee’s destination address:
8
void setDestination() {
// put the radio in command mode:
Serial.print("+++");
// wait for the radio to respond with "OK\r"
char thisByte = 0;
while (thisByte != '\r') {
if (Serial.available() > 0) {
thisByte = Serial.read();
}