1 of 23

Basic

Programming Blocks

2 of 23

  1. The printing statements
  2. The conditional control statements
  3. The comparison operators
  4. The logical operators
  5. The loop control statements

CONTEN TS

VIDEO

Video

3 of 23

  • In the last class we learned Bittle’s main controller board and its on-board hardware
  • In this lesson, we will learn the basic structure of graphical programming, so that we can communicate with Bittle more smoothly

woof~

woof~

~

4 of 23

Block

category

Output area

Automatically generated Python script

Coding

area

Python Mode

Mind+ Environment Introduction

5 of 23

  • Can be used to print the value of a variable
  • Can also be used to print text

\CODE\2.01_PrintHelloWorld.mp

Print Statement

6 of 23

\CODE\2.02_GetUserInput.mp

Get User Input

7 of 23

Conditional Statement

Single branch

Double branch

Multiple Branch

8 of 23

Single Branch Structure

if judgment condition is True:

the number is larger than 10

after executing, continue to execute statement 2

if conditions is False:

the number is smaller or equal to 10

9 of 23

\CODE\2.03_SingleFlow.mp

Single Branch Structure

10 of 23

if…else… Branch Structure

If the expression condition is True:

statement 1 is executed:

the number is smaller than 10

after execution, else is skipped

statement 3 is executed:

done

If the condition is False:

statement 2 is executed:

the number is larger or equal to 10

statement 3 is executed:

done

11 of 23

if…else… Branch Structure

\CODE\2.04_DoubleFlow.mp

12 of 23

Multiple Branches�

\CODE\2.04_MultiFlow.mp

13 of 23

Task 1.1

  • Modify on the basis of this code, compare the size of the two numbers.

The random number is also displayed on the serial monitor

if the random number is large:

the serial port monitor displays YES;

else, the serial port monitor displays No;

\CODE\2.05_CompareRandomNum.mp

14 of 23

Task 1.2

  • Modify on the basis of this code, compare the size of the two numbers.

The random number is displayed on the serial port monitor

if the number is large, the serial port monitor displays YES;

If the number is small; Display NO on the serial monitor;

If the number equals the set value, OO is displayed.

\CODE\2.06_CompareRandomNum_MultiFlow.mp

15 of 23

Let's continue to study conditional statements

woof~

woof~

~

16 of 23

Comparison Operator

Comparison operator

Explanation

equal to

not equal to

less-than

less than or equal to

greater-than

greater than or equal to

17 of 23

Boolean Type

\CODE\2.07_BoolTyoe.mp

Boolean variables have only true/false

18 of 23

Logical Operator

Operator

Name

Explanation

and

A and B,Only if both conditions A and B are true is the whole condition true

or

A or B,If one of the two conditions A and B is true, the whole condition is true

not

if the condition is false, the "not" block can make it true

19 of 23

\CODE\2.08_LoopControl.mp

Loop Structure

20 of 23

Task 2

Please write the code to assess a student’s score.

Set a random score (1~100) and print it on the serial monitor. And comment:

If it’s equal to 100, print “Excellent”;

If it’s greater than 90 (inclusive) and less than 100, print “Well done”;

If it’s greater than 80 (inclusive) and less than 90, print “Good”;

If it’s greater than 70 (inclusive) and less than 80, print “Not bad”;

If it’s greater than 60 and less than 70, print “Nice try”;

If the random number equals 60, print “Interesting”;

If all the above conditions are not met, print “Smile”;

You may add some delay time in the loop to slow down for better observation.

\CODE\2.09_RandomNumInMultiFlow.mp

21 of 23

The previous comparison code can only be executed once per run. You can also write the code in the loop to let the program judge contiguously.

Pay attention to the delay time. Otherwise, the program will run very fast!

\CODE\2.10_LoopRandomNumCompare.mp

22 of 23

A comprehensive example of sound and light cycles

\CODE\2.11_LoopRandomNumOpenLEDBuzzer.mp

In the following example, we may not show the common starting blocks.

Remember to add them by your own!

On the basis of the previous code, please write the code, to achieve the following requirements:

print "Yes", the light LED lamps;

Print "OO", light LED lights, buzzer alarm at the same time;

Print "No", a buzzer alarm.

23 of 23

  • Now we are quite familiar with the branch structure and the loop structure of conditional control
  • Next time we will learn joint control in Mind+

woof~

woof~

Now we have learned programming basis.

Next time we'll help Bittle do various actions in Mind+