When you program in MicroPython with Mu there are some things that you should be aware of (don’t worry if you don’t understand all of these right now, just be aware that if you start to have issues have a look at this page and see if it helps);
- Comments are used to make the code more readable. They start with a # character and can be written in plain English.
- When adding comments on the same line of code you will need 2 spaces before the # and one space after the # before the comment starts. If this is not done then Mu will flag up an error when the check button is pressed. All comments must start with a # character so the Micro:Bit knows that they are comments and doesn’t try to execute them.
- Each line of code can’t be greater than 79 characters including comments. If they are an error will be flagged when the check button is pressed. Code can be spread over multiple lines if required.
- Whitespace is not allowed when programming with Mu. For instance if you have spaces at the end of lines of code an error will be flagged when the check button is pressed.
- There must be 2 lines between methods and these lines must not contain any whitespace.
- The program loop will always start with while True: all code indented below this will be part of the program loop.
- indentation, program code is indented to make it more readable. All indents must be a multiple of 4 i.e. 4 spaces or a press of the tab button. When using conditional statements and loops indentation is extremely important because the code that is indented is part of the conditional statement or loop.
- The above code excerpt shows how indentation is important. immediate code under the def keyword is indented by 4 spaces/1 tab. This is to show that this code is part of the rightLights() method. The next line is the start of the for loop. The line under this is indented further (8 space/2 tabs) to show that it is part of the for loop. The next line (np.show())is not indented with reference to the above line (4 spaces/ 1 tab) because that line is not part of the for loop.
- ALWAYS use the check button at the top of Mu before uploading your code.
- If the screen on the Micro:Bit displays error text when it shouldn’t there’s an issue with your code. You will need to debug and re-upload.
- Batteries; get some rechargeable AA batteries as they won’t last very long.
Mu and PWM errors
When using Mu to control the speed of the motors and NeoPixels the Bit:Bot behaves erratically. This is due to an error in the DAL that is uploaded from Mu on to the Micro:Bit. When writing code that controls the motors I suggest that you use the online MicroPython editor instead of Mu (link on the resources page). I like to use Mu because uploading code is easier so I test it using Mu to make sure I don’t get any errors then I will compile using the online editor.
When you progress beyond the Mu tutorials, you will start using the Arduino IDE to unlock parts of the Micro:Bit that can’t be accessed using MicroPython. Arduino IDE will be difficult to use to start with and the syntax of the language is different to MicroPython. Be patient with it once you understand Arduino IDE you will find that it is easier to use than Mu and the online editors.
If your Arduino IDE sketches do not work on the Micro:Bit upload the softdevice hex file to the MicroBit, then re-upload your Arduino Sketch. If you put a MicroPython sketch onto the Micro:Bit you will need to put the softdevice hex file onto the Micro:Bit before uploading an Arduino Sketch otherwise it will not work as desired.