Hardware Reference
In-Depth Information
BTDisconnect() is similar to
BTConnect() , but in reverse. It drops
into command mode and sends the dis-
connect message you used earlier.
8
void BTDisconnect() {
// if in data mode, send $$$
if (!commandMode) {
Serial.print("$$$");
// wait for a response:
if (finder.find("CMD")) {
commandMode = true;
}
}
// once you're in command mode,
// send the disconnect command:
if (commandMode) {
// attempt to connect
Serial.print("K,\r");
// wait for a successful disconnect message:
if (finder.find("BTDISCONNECT")) {
connected = false;
// radio automatically drops into data mode
// when it disconnects:
commandMode = false;
}
}
}
8
handleSerial() also looks for text
messages, but it doesn't use TextFinder
because it needs to be able to see one of
three different options. TextFinder makes
only one pass through the serial stream,
so you can't check for a second string if it
doesn't find the first string.
void handleSerial() {
// look for message string
// if it's BTCONNECT, connected = true;
// if it's BTDISCONNECT, connected = false;
// if it's CONNECT failed, connected = false;
// if it's a number, set the LED
char inByte = Serial.read();
If a valid number is found, this method
uses it to set the brightness of the LED
on pin 3.
// add any ASCII alphanumeric characters
// to the message string:
if (isAscii(inByte)) {
messageString = messageString + inByte;
}
// handle CONNECT and DISCONNECT messages:
if (messageString == "BTDISCONNECT") {
connected = false;
}
if (messageString == "BTCONNECT") {
connected = true;
}
if (connected) {
// convert the string to a number:
int brightness = messageString.toInt();
// set the analog output LED:
ยป
 
Search WWH ::




Custom Search