Hardware Reference
In-Depth Information
You need to pick an Arduino and power supply. While any type of board
will work, I am using an Uno. I chose a USB cable connected to a wall adapter
for power. You could just as easily use a battery or a USB connection to your
computer. You can expand the functionality of the receiver by adding more
lights, motors, or controlling a Processing sketch on your computer.
TransmitterSoftware
Once your hardware is all set up, you need to write the software for both ends
of the system. Bear in mind that there are myriad ways to set up this commu-
nication scheme, and just one methodology is outlined here.
For this setup, you have the transmitter sending a value every 50ms. It will
be '0' when the button is not pressed and '1' when the button is pushed. It's
not necessary to debounce the button, because you are not looking for button
clicks; the receiver will ring as long as the transmitter button is held down.
The code changes slightly depending on what kind of Arduino you are using.
In the case of the Arduino Uno (or any other Arduino that has a separate Atmel
or FTDI chip for handling serial communication), the main MCU UART con-
nection is shared between the USB port and the RX/TX pins (pins 0 and 1) on
the Arduino. If using an Uno or Mega (or any other Arduino with a separate
USB-serial chip), you need to remove the XBee shield to program the Arduino,
or adjust the jumpers/switch if your shield has that functionality. On these
boards, Serial refers to both USB and UART communication over pins 0 and 1.
If you are using the Leonardo, or another Arduino that has USB communica-
tion integrated, you use Serial to talk over USB and Serial1 to talk over the
RX/TX pins. You do not need to remove an XBee shield to program a board
like the Leonardo because the UART is not shared. The code in Listing 11-5 is
written for the Leonardo and other similar Arduinos. If you are using an Uno-
based platform, replace references to Serial1 with Serial .
Listing 11-5: Doorbell Transmitter—doorbell/transmitting_arduino
//Code running on an Arduino to transmit the doorbell push
const int BUTTON =12; //Button on pin 12
void setup()
{
//NOTE: On the Leonardo, the RX/TX serial pins are
//not multiplexed with USB like they are on Uno.
//This sketch is written for the Leonardo (Serial1 = RX/TX pins)
//If you are using the Uno, change Serial1 to Serial, here and below
Serial1.begin(9600); //Start serial
}
Search WWH ::




Custom Search