In this activity you will create a controller to be used with several games.
If you get stuck, go to the Troubleshooting links at the end of the example.
2. Add the XBees to XCTU
Ensure that the XBee is seated firmly in the XBee USB adapter.
Plug the XBee USB adapter into your computer using the mini-USB cable provided.
Make sure you are in Configuration working mode.
Click Discover radio modules from the toolbar.
In the Discover radio devices dialog, select the serial port(s) in which you want to look for radio modules. If you do not know the serial ports where your modules are attached, select all ports. Click Next.
In the Set port parameters window, maintain the default values and click Finish.
As XCTU locates radio modules, they appear in the Discovering radio modules… dialog box. Once the discovery process has finished, click Add selected devices.
- At this point, you should see something like this in the Radio Modules section on the left:
The port number and MAC address do not necessarily have to be equal as shown in the picture.
3. Configure the XBees
One of the XBees will be the controller and the other the receiver attached to the PC and talking to Processing:
- Restore the default settings of all XBees with the Load default firmware settings
Use XCTU to configure the following parameters:
Param Controller XBee Receiver XBee Effect CH C C Defines the frequency to use to communicate. This must be the same for all radios on your network. ID 2015 2015 Defines the network that a radio will attach to. This must be the same for all radios on your network. DH 0 — DH and DL combined form the destination address. This is where the new position of the joystick and the button values are sent. The address being configured here is known as a short address. A short address is assigned during configuration. An XBee also has a long address (SH and SL combined) which is assigned permanently when the unit is manufactured. DL 1234 — MY — 1234 Defines the XBee's short address. AP — API enabled  Enables API mode. D1 ADC  — Sets the DIO1/AD1 pin as ADC in the controller to read the analog value from the joystick y-axis. D2 ADC  — Sets the DIO2/AD2 pin as ADC in the controller to read the analog value from the joystick x-axis. D4 DI  — Sets the DIO4/AD4 pin as digital input in the controller. This pin is connected to the right button. D6 DI  — Sets the DIO6/AD6 pin as digital input in the controller. This pin is connected to the left button. IC 50 — Configure the controller XBee to transmit an IO sample when pin DIO4/AD4 and DIO6/AD6 changes. In other words, when left or right button is pressed.
01010000 (binary) = 50 (hexadecimal)
IR 32 — Sample quickly (every 50ms) so controller latency is minimized (50 ms = 32 in hexadecimal).
— keep the default value.
- Write the settings of all XBees with the Write radio settings button at the top of the Radio Configuration section.
4. Connect the components
Create the following circuit with your components.
Ensure that each wire is connected to the same column on the breadboard as the pin of the XBee breadboard adapter footprint. This ensures that the signals to the XBee are correct.
Click to expand diagram
- Push down firmly on the breadboard adapter board to ensure it is seated properly on the breadboard. Do not be afraid to use a little force.
- Insert the Controller XBee into the XBee breadboard adapter. Push down firmly on the XBee to ensure it is seated properly on the breadboard adapter.
Double check that the breadboard adapter and XBee are positioned in the correct location and oriented the same as the image below. Incorrect placement has the potential to result in damage to components.
Click to expand diagram
The headers connecting the joystick to the breadboard provide stability on one side. If the joystick is not as stable as you would like, wrap a piece of tape from the top of the joystick around to the bottom of the breadboard to hold the other side firmly in place.
- Insert the Receiver XBee into the XBee USB adapter.
- Connect the XBee USB adapter to the USB port of the computer which will run Processing.
5. Put it all together
- If you have not already done so, download and extract the latest zip archive from the XBee/Arduino Compatible Coding Platform release page.
- Follow the instructions in the Manual Install section of How to install a Contributed Library to install the provided library in your Processing environment. From the
processing_getting_startedfolder, install the contents of the files
- Enjoy playing some of the provided games, or make your own.
6. Use the library yourself
- Start the Processing application.
- Add the library to your sketch with Sketch > Import Library.... The library appears as usable at the bottom under the Contributed section.
The library has one class named Controller . A Controller object can be created as follows:
The arguments to the constructor are:
- A reference to your application (represented by the keyword
- Serial port device name
- Serial port baud rate
Match the serial settings as appropriate to communicate with the XBee attached to the Processing computer.
Currently the library assumes that only one controller will send it data.
Once you create the Controller object, it provides getX and getY methods that return the 10-bit ADC value from the XBee. The value is configured to match the orientation of the Processing drawing context; up reduces the y-axis value, down increases it. The draw method can use these getters to retrieve the joystick position in each frame for the game logic.
To be informed of button-press events, provide a
void buttonEvent(boolean leftButton, boolean rightButton)method in the sketch. This will be called each time the button state changes.
If you are encountering problems, these suggestions may help:
For more information about the hardware and software you use in this activity, see: