A Simple Guessing Game - Python + XBees + Arduino

by thesharanmohan in Circuits > Arduino

714 Views, 1 Favorites, 0 Comments

A Simple Guessing Game - Python + XBees + Arduino

A SIMPLE GUESSING GAME

Here’s how the game works:
You have 4 tries to guess a letter between ‘a’ – ‘h’

-> Guess the right letter: You win! 🙂
-> Guess the wrong letter: Game over 🙁
-> Guess any other character outside of ‘a’ – ‘h’: Game over 🙁

Your Arduino will let you know the response by displaying it on a 16×2 LCD. The LEDs turn on in different colors if the guess is higher/lower or right or wrong.

Diagram

BLOCK_DIAGRAM_37.PNG

Hardware

HARDWARE_37.jpeg

What you'll need

Arduino board (Mega), 16×2 LCD display, 10K ohm potentiometer, XBee S2 radios (2), Breakout board for XBee module, XBee Explorer USB, LEDs (3), jumper wires, USB cable (A-to-B), 9V battery + connector and breadboard.


Software

SOFTWARE_37.png

XCTU

XCTU_SW_37.PNG
XCTU_COM_37.PNG
XCTU_SET_PORT_PARAMETERS.PNG
XCTU_DISCOVERING_RADIO_37.png
RADIO_FW_MODULE_UPDATE_37.png
ZIGBEE_ROUTER_COORDINATOR_AT_37.png
EXCHANGE_ADDR_SL_DL_37.PNG

X1. Plug one of your XBee radios into an Explorer module and connect the module to your computer’s USB port. Launch the XCTU program.

X2. Click “Discover devices” and select the port to be scanned. Click “Next”.

X3.
Select the port parameters to be configured and click “Finish”.

X4.
Your radio module should be discovered under the “Devices discovered” list. Click “Add selected devices”.

X5. Click on “Read” in the Modem Configuration screen to see what firmware is on that radio. Each XBee radio should be running the newest firmware version of the ZigBee Coordinator AT or ZigBee Router AT. Update the radio firmware module by clicking “Update”.

X6. To get the radios talking, there are 3 important things we must check. The first is the “PAN ID”. The PAN ID is the network ID that these radios are going to talk on. We will be setting this to a unique value = 2019 (You can choose a value between 0 and 0xFFFF).

X7. Every XBee radio has a 64-bit serial number address printed on the back. The beginning (SH) or “high” part of the address will be 13A200. The last or “low” (SL) part of the address will be different for every radio.

X8. To make the XBee radios chat with each other, the addresses of the Router and the Coordinator are switched.

X9. Click on the “Write” button to program your radio. Once you’ve finished configuring your first radio with the required configuration software, gently remove that radio from the explorer module and carefully seat a second radio in the same module and repeat the above steps.

Arduino IDE

ARDUINO_SW_37.PNG
ARDUINO_PORT_BOARD_37.png

A1. Connect the Arduino board (Mega) to one of your computer’s USB ports and open Arduino IDE.
A2. Select the correct serial port and board.
A3. Upload the sketch to the Arduino board by clicking on the Upload button.

NOTE: The sketch can be downloaded from here.

Circuit

FRITZING_37.PNG

Plug the XBee radio configured as the Coordinator into your XBee Breakout board and connect it to the Arduino by following the schematic.

Hardware Setup

HW_SETUP.png

H1. The Arduino board (Mega) is powered by a 9V battery.
H2. Plug the XBee radio configured as the Router into your XBee Explorer module and connect it to one of your computer’s USB ports.

PyCharm

PYTHON_DOWNLOADS_PAGE_37.PNG
PYCHARM_COMMUNITY_2019_37.png

You can use any Python IDE, in this case I’m using PyCharm. Before you begin, make sure that the following prerequisites are met:

-> You have installed Python. You can get it from: https://www.python.org/downloads/
-> You are working with PyCharm Community.

Creating a Python Script in PyCharm

PYCHARM_CREATE_PROJ_37.PNG
PYCHARM_CREATE_NEW_PYFILE_37.png
PYCHARM_SW_37.PNG
PYCHARM_FILE_SETTINGS.png
PYCHARM_PROJ_INTERPRETER_37.png
PYCHARM_INSTALL_PACKAGE_37.PNG

P1. Let’s start our project: if you’re on the Welcome screen, click Create New Project. If you’ve already got a project open, choose File -> New Project.

P2. Select Pure Python -> Location (Specify the directory) -> Project Interpreter: New Virtualenv Environment -> Virtualenv tool -> Create.

P3. Select the project root in the Project tool window, then select File -> New -> Python file -> Type the new filename.

P4. PyCharm creates a new Python file and opens it for editing.

P5. Install the following package: PySerial (a Python library which provides support for serial connections over a variety of different devices)

To install any package in PyCharm:
P6.
File -> Settings.

P7. Under Project, select Project Interpreter and click on the “+” icon.

P8. In the search bar, type the package you wish to install and click on Install Package.

NOTE: Make sure the COM port number that is used in the Python code is that of the XBee Explorer module connected to the PC. The COM port number can be found in Device Manager -> Ports (COM#)

Running Your Program

OUTPUT_1_37.png
OUTPUT_5_37.png
OUTPUT_2_37.png
OUTPUT_3_37.png
OUTPUT_4_37.png