Hardware Reference
In-Depth Information
Finally, handleSerial() looks for a
carriage return; when it finds one,
it clears the messageString in order to
get the next message.
8
Continued from previous page.
if (brightness > 0) {
analogWrite(analogLed, brightness);
}
}
// if you get an ASCII carriage return:
if (inByte == '\r') {
// clear the input string for the
// next value:
messageString = "";
}
}
That's the whole program. Run this on your
microcontroller, filling in the address of the
radio on Arduino #2 for the address in the
remoteAddress array above. When you press the button
on the first microcontroller, it will make a connection to
the second and start sending sensor values through. In the
serial terminal, your initial messages should look like this:
12\r
120\r
255\r
1*
The LED should start dim, get brighter, then brightest,
then get very dim. You should get four sensor readings
in response. Then, when you press the button again, you
should get:
BTCONNECT
121
132
83
BTDISCONNECT
When you've connected and disconnected a few times,
you're ready for the final step.
When you're connected, you can respond as if you were
sending your own sensor values, and the microcontroller
will fade the LED on pin 3 accordingly. Type:
Step 4: Connecting Two
Microcontrollers via Bluetooth
If you've been noticing the parallels between the XBee
example and this one, you probably know what's coming.
Build the same circuit for your second microcontroller,
using the second radio by adding the switch and potenti-
ometer, and swapping the serial TX and RX connections.
Then change the Bluetooth address in the sketch above
to be the address of the first radio, and program the
second microcontroller. Then reset both microcontrollers.
When you press the button on either one, it will attempt
to connect to the other and begin exchanging data. When
you press the button on either one a second time, they'll
disconnect.
Now that you've got the basics down, you can modify this
to handle a variety of situations with Bluetooth radios.
X
 
Search WWH ::




Custom Search