YOU LEARN HERE... |
|
SEQUENCE |
A computer program consists of a sequence of program lines, which are processed one after the other. In order to use the commands for the beetle, you have to import the module mbutils with from mbglow import *.
With forward() the beetle moves one step forward in the current direction of movement. At program start, the direction of movement points upwards. left(90) turns the beetle 90 degrees to the left and right(90) 90 degrees to the right. But this will not be noticeable until the next forward() command. With the following program, the beetle draws the track shown:
You can type the program in the editor window or copy/paste it from the template. To do this, click on Copy to clipboard and insert it with Ctrl+V into the TigerJython editor.
|
REPEAT LOOP |
Program: from mbglow import * makeGlow() repeat 4: forward() left(90) |
Pay attention to the correct indentation! Program: from mbglow import * makeGlow() setPos(-2, -2) repeat 4: repeat 4: forward() right(90) |
FUNCTIONS (NAMED PROGRAM BLOCKS) |
It is a good practice to group together commands that perform a common action. In Python such a program block is called a function, and you reference it by its name. After the function definition, the function may be used like an additional command: Each time you want the program block to execute, you just write the function name. You say that you invoke or call the function. The new function is available everywhere in your program. It's like extending the vocabulary of the Python language. The use of functions is very important, because you structure the program by breaking it into smaller pieces that have individual purposes. Each time you need to perform these actions, you do not duplicate the program lines, but just call the function.
As a function name, choose an identifier that expresses something about what the function does. You may not use special characters (spaces, umlauts, accents, punctuation, etc.). As a convention always start the name with a lowercase letter. Program: from mbglow import * makeGlow() def square(): repeat 4: forward() forward() left(90) square()
Program: from mbglow import * makeGlow() def square(): repeat 4: forward() forward() left(90) setSpeed(80) repeat 4: square() right(90) |
VARIABLES |
To increase the value of v by 20, you make the assignment v = v + 20, which at first glance looks like an mathematical equation, but is something completely different: It tells the computer to take the current value of v and add 20 to it, and then save the result in v again. The command showTrace(False) causes the beetle to leave no trace. Program: from mbglow import * makeGlow() def square(): repeat 4: forward() forward() right(90) v = 30 showTrace(False) setPos(-1, -1) repeat 4: setSpeed(v) square() v = v + 20 |
WHILE LOOP |
The while loop is one of the most important program structures. It can be used universally for any kind of repetition, and occurs in virtually all programming languages. A while loop is started with the keyword while followed by a condition and a colon. As long as the condition is fulfilled, the commands in the subsequent program block are repeated. Colloquially this would be expressed as follows: As long as condition is true, perform... To express the condition, the comparison operators < (less), <= (less-or-equal), > (greater), >= (greater or equal), == (equal), != (different) are used. Note the duplication of the equality sign in the equality test to make the difference to an assignment.
Program: from mbglow import * makeGlow() setSpeed(90) showTrace(False) a = 0 while a <= 360: forward() forward() back() back() left(45) a = a + 45
Program: from mbglow import * makeGlow() def square(): repeat 4: forward() forward() left(90) clear() setSpeed(90) showTrace(False) setPos(2, 0) left(45) while True: square() |
FOR IN RANGE LOOP |
With for x in range(-2, 3) x passes through the values -2, -1, 0, 1, 2. The LEDs in the top row are switched on one after the other. With sleep(500) you can pause the program execution for 500 milliseconds. Program: from mbglow import * makeGlow() clear() for x in range(-2, 3): setPos(x, 2) sleep(500)
Program: from mbglow import * makeGlow() clear() for y in range(-2, 3): for x in range(-2, 3): setPos(x, y) sleep(300) |
SELECTION IF - ELSE |
A selection is used when a program block should be executed only under a certain condition. The selection is initiated with the keyword if, followed by a condition and a colon. The following commands are part of the if block and are equally indented. They are executed only if the condition is true. The keyword else may follow, together with commands in an else block. These commands are executed, if the condition is false. In everyday language, one would express this as follows:: If condition true, then action1 else action2 In the following program the two buttons of the micro:bit are used: There is no else block.
Program: from mbglow import * makeGlow() right(90) x = 0 showTrace(False) while True: forward() x = x + 1 if x == 3: setPos(-2, 0) x = -2 In the next program, the beetle will randomly walk two steps forward or two steps backwards, then stop briefly, clear the track and return to the starting position. The action is repeated 10 times.
Program: from mbglow import * from random import randint makeGlow() setSpeed(80) repeat 10: r = randint(0, 1) if r == 1: forward() forward() else: back() back() sleep(300) clear() setPos(0, 0) |
MEMO |
The basic program structures are sequence, repetition and selection. With a sequence, the commands are executed sequentially one after the other. With a repetition, program blocks are executed several times. Python uses the keywords while and for, in TigerJython the additional keyword repeat is available . The selection with the keywords if and else causes certain statements to be executed only under certain conditions. The else part can be omitted. |
EXERCISES |
|
The concept of variables is fundamental, but a didactic challenge.
There are essentially three different methodological approaches. Since we only use variables in micro-bit robotics in simple contexts, we choose the pragmatic use.
Low-Level-Modell | Advantages | Disadvantage |
A variable, as in the machine code / assembler, is considered a reserved main memory location, which is addressed via its address |
Clear reference to the machine structure |
Although addresses are important in assembler, C / C ++ and other programming languages with pointers, in practice they only play a minor role in many other programming languages or are even frowned upon |
Boxmetapher | Advantages | Disadvantage |
A variable is understood to be a box (or drawer) that is written with a name and in which the value is located |
A variable is understood to be a box (or drawer) that is written with a name and in which the value is located |
The box can also be empty, but a variable always has a value. The box can contain several things, but not a variable. The box metaphor is wrong for Python, as a variable here is always a reference to an object (even for numbers) |
Pragmatischer Ansatz | Advantages | Disadvantage |
Variables are identifiers for a value that can be changed. So there is a reference or connection betwe |
The approach corresponds to the intuitive concept of names: "Hans is an identifier for a person". One does not "lose" time with problematic explanations |
It remains vague as far as the implementation of the variable (the memory model) is concerned |