
WIRELESS COMMUNICATION 213
void blink(int howManyTimes) {
for (int i=0; i< howManyTimes; i++) {
digitalWrite(txLed, HIGH);
delay(200);
digitalWrite(txLed, LOW);
delay(200);
}
}
The blink() method is the same as
it was in the earlier XBee example:
8
void loop() {
if (Serial.available() > 0) {
// signal that there's incoming data using the rx LED:
digitalWrite(rxLed, HIGH);
// do something with the incoming byte:
handleSerial();
// turn the rx LED off.
digitalWrite(rxLed, LOW);
}
// if you're not connected and 5 seconds have passed in that state,
// make an attempt to connect to the other radio:
if (!connected && millis() - lastConnectTry > connectTimeout) {
BTConnect();
lastConnectTry = millis();
}
}
The main loop listens for incoming
serial data and handles it. If more than
five seconds have passed and the
radio’s still not connected to the other
radio, the microcontroller attempts to
connect again:
8
The handleSerial() method is similar to the
one in the XBee project, but there are some
important differences. First, because there’s a dedicated
connection between the two radios, you need to keep track
of the connection status. When a new connection is made,
the BlueSMiRF will send a serial message like this before
dropping into data mode:
CONNECT,0016CB202BF3
When the connection’s broken, it will send this message,
and stay in command mode: ...