TigerJython4Kids | robotics |
Deutsch English |
YOU LEARN HERE... |
how a robot can repeat certain command sequences and how you can structure your programs better with named program blocks (functions). |
EXAMPLES |
from mbrobot import * #from mbrobot_plus import * repeat 4: forward() delay(2000) left() delay(550) stop() First run the program in simulation mode. In real mode, you may have to adjust the time when turning left. In contrast to the simulation, in which the robot can drive exactly through 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. |
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 cancel a running program: The easiest way is with switsch
|
Example 3: Structure programs 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 statements in the function body are indented. The function is called in the main program. In your example, you define a blink() function that causes the red LED to light up once. In the main program, the robot makes a similar movement to the previous example. Before it moves backwards, it stops, flashes twice and moves backwards to the starting point.
|
REMEMBER... |
To repeat a program block n times, use a repeat loop: To repeat a program block endlessly, use an endless loop: You can use functions to structure your programs better and avoid code duplication. You must differentiate between the function definition and the function call.
|
TO SOLVE YOURSELF |
|