Write a Program in Python on mBlock 5

With the Python editor of mBlock 5, you can write programs in Python as in any other Python code editors.

Open the Python editor

1. Open mBlock 5 and click Python Editor.

mceclip0.png

2. Choose File > New Project to create a project.

mceclip1.png

Now you can start to write your code in the editing area.

mceclip2.png

Write your Python code

Option 1: Write your code without libraries

num1 = 100
num2 = 200
# Add the two numbers
sum = num1 + num2
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

mceclip4.png

After writing the code, click Run.

The terminal displays the execution result of the program.

mceclip5.png

Option 2: Write your code based on a library

In addition to the libraries developed by Makeblock, the mBlock Python editor provides some popular third-party libraries and you can write your code based on them.

The following uses the termcolor library as an example. 

#Print Constellations
from termcolor import *
constellation=['Aries','Taurus','Gemini','Cancer','Leo','Virgo','Libra','Scorpius','Sagittarius','Capricornus','Aquarius','Pisces']
for i in range(12):
pos=i
a=colored("{:^5}".format(chr(9800+i)),"magenta")
b=constellation[pos]
print(a,b)
print()

After writing the code, click Run.

If the termcolor library has not been installed, you are prompted to install it.

mceclip6.png

Click Yes and wait for the installation to be completed.

mceclip7.png

Click Run again. 

The terminal displays the execution result of the program.

mceclip8.png

You can also install the libraries you need before you write your code.

Install a library

The following describes how to install a library:

1. Choose Libraries.

mceclip9.png

2. Search for the library you want to install by category and click the Install button on the right.

install_library.gif

Option 3: Program a device in Python

Currently, the Python editor supports only CyberPi. You can write your code to control CyberPi.

1. Use a USB cable to connect CyberPi to your computer.

2. Click Connect in mBlock-Python Editor, and then click Connect in the dialog box that appears.

2.png

3. Set program execution mode to Live.

For device programming, mBlock-Python Editor provides two program execution modes: Live and Upload.

3.png

The Live mode supports Python 3 libraries and is used to control devices in real time.

In Live mode, you don't need to upload programs to devices and can click Run to execute a program in real time.

mceclip1.png

 

The Upload mode supports only MicroPython, and you can't use third-party Python 3 libraries to program devices.

In Upload mode, you need to click Upload to upload a program to a device. After being uploaded to a device, a program can be executed on the device even when the device is disconnected from mBlock-Python Editor.

mceclip2.png

 

4. Choose File > New project, and enter your code in the project file editing area.

mceclip3.png

5. Install the cyberpi library.

CyberPi is the latest main control board developed independently by Makeblock. To program CyberPi, you need to install the cyberpi library.

mceclip10.png

6. Enter the following code to implement the marquee effect of the LEDs on CyberPi:

import math 
import random
import cyberpi
from time import sleep

cyberpi.led.show("r g b y c")
count = 0
while True:
    cyberpi.led.set_bri(math.sin(count / 4) * 50 + 52) 
    cyberpi.led.move(1) 
    count += 1
    sleep(0.1)

 

mceclip4.png

3. Click Run.

The onboard LEDs of CyberPi are lit up in marquee mode.

More information

You can learn more about Python coding from Python Example Programs

To program CyberPi, refer to the Python API Documentation for CyberPi.

Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.