Working with two micro:bits

Our current assignment required using two microbits (or a microbit and the microbit app on a smartphone) and a sensory input device. Since I’m a game design major, I decided to use a joystick for my sensory input. I had originally planned to create a two-player game where one player would use the device with the joystick and another would use the device with it, but I could not think of gameplay that seemed interesting. Instead I decided to make a very simple adventure game where one microbit would serve as the joystick controller, and the other would just act as a receiver to print messages using serial.

I started by just testing if I could get a simple “Hello” message to go from one microbit to another. I coded two separate projects for each, and I set the group to 1 for each on start. It worked pretty easily, so then I started working on getting the joystick to work.

1114191823

joystick code

Because the joystick is essentially two potentiometers, each pin will return a value between 1 and 1023. The default position for each is directly in the middle on both the x and y axis, so I picked values above or below 511 to register the current direction the joystick was pointing in. When the value was above this amount, the LED on the joystick would display the chosen direction. Pressing down on the joystick would send a number associated with the current direction to the other microbit. A check mark symbol would display on the joystick microbit to let the user know that the press had been registered. When I tested the joystick button last week, I was able print either a 1 or 0 using serial with a digital read of the pin. This did not work for me, and I had to use an analog read of the pin, which seemed a bit strange.

terminal

I ran into a problem where I couldn’t get the joystick microbit to send the command to the receiver when the joystick microbit was disconnected from the computer. In order to troubleshoot this issue, I changed out the code that sent the message after clicking the joystick with code that would send when the “A” button on the microbit itself was pushed. This worked, and I realized that the problem was that the joystick button required 5 volts. This was a simple fix by plugging the microbit’s USB into an outlet instead of the computer.

All in all, this project went pretty smoothly. I was able to figure out what was causing my problems, or at least I found workarounds for them.