Customer Notice (New Bluetooth App):
We have recently produced a new Google Play Store app called Kitronik Bluetooth Control. This Android app allows you to control a Kitronik Kitronik :MOVE mini buggy over Bluetooth. The new Kitronik Bluetooth Control app replaces our previous Android app that features in this resource. We suggest that you switch to this new app for a better and more consistent experience. See the links below for more details.
This Resource:To explain coding with the Microsoft MakeCode Editor we'll hand over to Martin Woolley. Martin not only wrote the code that we use for the remote controlled buggies, he also designed the Bluetooth profile for the BBC micro:bit. Over to you Martin...
From a software point of view, the Kitronik Robot Wars remote controlled buggy consists of some code running on a BBC micro:bit and an application called ‘micro:bit Blue’ running on an Android smartphone or tablet. The two devices communicate using special messages called ‘events’ which are transmitted from the smartphone application to the BBC micro:bit using Bluetooth, a wireless communications technology which uses very little power and which both the smartphone and BBC micro:bit are equipped with. The smartphone application sends different types of event, each with its own unique event code depending on which buttons on the touchscreen gamepads are touched or released. An event code is just a simple number and the code on the micro:bit decides what it should do, based on the event codes it is receiving.
Controlling the Buggy:The BBC micro:bit is plugged into the Kitronik buggy using its edge connector, in this case via the Kitronik Motor Driver Board that the microbit is plugged into. The edge connector is divided into several separate electrical ‘pins’. The micro:bit software can control whether a pin has a high electrical value or a low value. In digital terms, we can think of this as meaning on or off. The motors which control the wheels of the buggy can each, separately, be made to move forwards or backwards or to stop moving, just by setting the value of the right edge connector pins to the appropriate values. For more information on the Kitronik Motor Driver Board, download the Datasheet here. Pins all have a pin number which acts as an identifier so that we can indicate in our code, which pin’s value it is that we want to change. To control the buggy, we use pins 0, 8, 12 and 16. The table below shows how each are used and the effect that they have in different combinations.
|PIN 0||PIN 8||PIN 12||PIN 16||EFFECT|
|OFF||OFF||OFF||OFF||The buggy is stationary|
|OFF||OFF||ON||ON||Buggy drives forwards in a straight line|
|ON||ON||OFF||OFF||Buggy drives backwards in a straight line|
|OFF||OFF||ON||OFF||Buggy turns left in the forwards direction|
|OFF||OFF||OFF||ON||Buggy turns right in the forwards direction|
|OFF||ON||OFF||OFF||Buggy turns left in the backwards direction|
|ON||OFF||OFF||OFF||Buggy turns right in the backwards direction|
The Microsoft MakeCode Editor:To write the code which responds to Bluetooth events sent from the smartphone application and controls the motors which drive the buggy’s wheels, we’ll use Microsoft’s MakeCode code editor. You only need a web browser to use this tool and can access it at https://makecode.microbit.org/. MakeCode is similar to the old Blocks editors but has a few more tricks up its sleeve, especially where Bluetooth is concerned. It provides a series of function blocks, organised into collections called ‘packages’. For example, there’s a Logic package, a Loops package and a Bluetooth package.
Coding With MakeCode:
NOTE:Due to cosmetic changes to the MakeCode Editor, the screenshots below vary slightly in look to the blocks you will find in the editor. They still function in the same way. There is an embedded editor towards the foot of this page which shows all of the code as it looks currently. To open the code in the online MakeCode Editor, click the Edit icon in the top right. So, we make use of the ‘on bluetooth connected’ and ‘on bluetooth disconnected’ blocks from within the Bluetooth package. When a bluetooth connection is established by the smartphone, we also initialise a variable called ‘drive’ which we’ll use to keep track of whether or not the buggy is moving (0 = not moving) and if so, whether it’s going forwards (drive = 1) or backwards (drive = 2). When micro:bit Blue disconnects from the micro:bit, as you can see below, we write a value of zero to all the buggy control pins so that if it is moving, it stops. We’ll also display “C” when connected to and “D” when the smartphone disconnects.
The event values we need to use are as follows:
|MES_DPAD_BUTTON_1_DOWN||=||right hand pad, top button, pressed|
|MES_DPAD_BUTTON_1_UP||=||right hand pad, top button, unpressed|
|MES_DPAD_BUTTON_2_DOWN||=||right hand pad, bottom button, pressed|
|MES_DPAD_BUTTON_2_UP||=||right hand pad, bottom button, unpressed|
|MES_DPAD_BUTTON_C_DOWN||=||left hand pad, left button, pressed|
|MES_DPAD_BUTTON_C_UP||=||left hand pad, left button, unpressed|
|MES_DPAD_BUTTON_D_DOWN||=||left hand pad, right button, pressed|
|MES_DPAD_BUTTON_D_UP||=||left hand pad, right button, unpressed|
Get The Code:
- You can download the code for the microbit buggy here.
- Alternatively, you can also grab the code from Bitty Software here.
|Build A Robot Wars Buggy Learning Resources.|
|Part 1 - Intro.||What we did, why we did it and what we used.|
|Part 2 - The Buggy.||It's all about the line following buggy, We used the buggy as is and attached custom parts.|
|Part 3 - The Perspex Top Plate.||How we designed and cut the top plate, with alternative methods for those without laser cutters.|
|Part 4 - The 3D printed Add-ons.||How we designed and 3D printed the add-ons, with alternative methods for those without 3D printers.|
|Part 5 - Making The Flag.||Our design process for making the self righting flag.|
|Part 6 - Coding with the Microsoft MakeCode Editor.||Beyond line following. Martin Woolley gives a very thorough breakdown of how he wrote the code for the Robot Buggies. Learn how!|
|Part 7 - Bluetooth Buggy Control.||How to turn your Android device into a remote control for the buggy, including pairing instructions, Again, over to Martin Wooley.|
©Kitronik Ltd – You may print this page & link to it, but must not copy the page or part thereof without Kitronik's prior written consent.