Transferring code from MakeCode to microbit.python.org
Hello everyone first time poster here. Im currently taking a robotics class at my university and am trying to figure why when I use MakeCode to run a simulation for the microbit board it works but when I transfer my code to microbit.python.org it comes up with the last line being an error. My project is to use the micobit board in any way to measure an increase in water elevation so. I decided to use the built in accelerometer as my measuring device.
I plan on replacing the show LED part of the code with some code that will light up an actual green, yellow, and red LED on the cyberbot board (most likely the ports being 0,2,4). I also plan on replacing the mealody part of the code with activating port 22 on the board as well.
Anyone know why when I run the code in the microbit.python.org it says the last line there is an error?
I like using MakeCode due to the visual block code building style it has. Here is a link to the MakeCode, https://makecode.microbit.org/_MtchesHz6C1x.
x = 0
def on_forever():
global x
x = input.acceleration(Dimension.X)
if x <= 0:
serial.write_line("Water Level Good")
basic.show_leds("""
# # # # .
# . . . .
# . # # #
# . . # .
# # # # .
""")
elif x == Math.constrain(x, 5, 512):
basic.show_leds("""
# . . . #
. # . # .
. . # . .
. . # . .
. . # . .
""")
serial.write_line("Water Level Rising")
music.play_melody("C5 - - C5 - - C5 - ", 120)
else:
basic.show_leds("""
# # # . .
# . # . .
# # # . .
# . # . .
# . . # .
""")
serial.write_line("Water Level Critical")
music.play_melody("C5 - C5 - C5 - C5 - ", 120)
basic.forever(on_forever)
Comments
This is the problem the python that make code generates isn't micropython, Microsoft have invented there own thing with a python syntax.
To get started with micropython on the microbit see https://microbit-micropython.readthedocs.io
You may want to use the mu-editor, it has built in file transfer, repl and plot. Download it from https://codewith.mu/en/download
You may want to continue with Maker code, it is certainly easy to create working programs.
If you are interested in Python or micropython I agree with rhubarbdog and that is to use the mu editor to develop your programs.
Python is fairly interesting all round and if you load Python 3 to your desktop it allows you to create multi platform desktop apps that function on Windows Mac and Linux and you can even create GUI's to interact with your microbit.
If you have any problems with Python code just post the full sketch here and we will see if we can help.