Deutsch English |
YOU LEARN HERE... |
how a robot can repeat certain command sequences and how you can structure your programmes better with named programme blocks (functions). |
EXAMPLES |
Program: from mbrobot import * #from mbrobot_plus import * repeat 4: forward() delay(2000) left() delay(550) stop() Run the programme in simulation mode first. In real mode, you may need to adjust the time when turning left. In contrast to the turtlegraphics, in which the robot can drive exactly draw a square, it is difficult for the real robot to drive exactly straight ahead and turn exactly at a right angle. This corresponds to reality, because no car will ever drive exactly straight ahead if the wheel position is fixed, i.e. the steering is locked. This is why robots are equipped with sensors that help them to correct these inaccuracies. So don't spend too much time teaching the robot to drive exactly square. |
Program: from mbrobot import * #from mbrobot_plus import * repeat: forward() delay(2000) backward() delay(2000) left() delay(550) Here it is useful to know how to abort a running programme: The easiest way is to switch off the power supply. |
Example 3: Structuring programmes with your own functions A function definition always begins with the keyword def, followed by a function name, a parameter bracket and a colon. The instructions in the function body are indented. The function is called in the main programme. In your example, you define a blink(), function that causes the red LED to light up once. In the main programme, the robot makes a similar movement to the previous example. Before it reverses, it stops, flashes twice and reverses to the starting point.
|
REMEMBER YOU... |
To repeat a programme block n times, use a repeat loop:
To repeat a programme block endlessly, use an infinite loop:
The indented lines are repeated until the programme is aborted by closing the terminal window. You can use functions to structure your programmes better and avoid code duplication. You must differentiate between the function definition and the function call.
|
TO SOLVE BY YOURSELF |
|