Use micro:bit to Control a Motorbike Simulator
Musab is a 13 year-old Maker from Turkey. Below is his brilliant Motorbike Simulator Make tutorial, all powered by the micro:bit!
Hi, I’m Musab, a 13 year-old programmer. I started programming 3 years ago and developed myself over time - read more about my journey in this blog post!
Today I’m going to show you one of my projects, using micro:bit as a game controller. We will test it on a game called Road Rash(It could be any game but I will show this as an example).
Note: It works only for Windows.
Setting up the Enviroment
- Get a micro:bit and optionally a long USB cable.
- Get something like a handlebar.
- Connect micro:bit, USB cable and the handlebar
- Download the code and other necessary files here
- Extract the zip to a folder named MicroBike
- Download and install this driver here.
And we are ready to use it!
First, go to https://www.python.org/download/releases/2.7/ , then download and install Python 2.
Second, connect your micro:bit to your computer and go to the MicroBike folder. Send the file named “bitio.hex” to the micro:bit.
Then, start install.bat to install the required packages. You can close when you see the message “Installation Finished!”
Now, you are ready to go! Just run the controller.py and open the game. You can start the controller by pressing the button A or B.
Understanding the Code
We made the controller, but how? Let’s review the code and see how this works.
We need import the modules to use them later. We will use 3 modules; microbit module for controlling and reading data from the micro:bit, time module for waiting for a specific time step and pykeyboard module to control the keyboard(and the game of course).
changeKeyState is a function, it will help as to control the keyboard keys. For example if the handlebar goes left, it will press the left arrow key.
We will define some variables to use them later.
This is the first loop. It will keep blinking until the user(motorbike driver!) presses the A or B button. After pressing the button controller will start running.
This is the main loop. We will start by getting required values and calculating the motion.
Then we will use the information we get before to control the game. Keyboard keys will trigger when the microbit turns right or left higher than a specific value.
We will use same information for changing the direction on the microbit.
That’s all! Thanks for reading :)