In this lesson we introduce the concept of loops. Often in programming we need to do the same thing over and over again. For example, in robotics we might want to constantly check the reading from an ultrasonic distance sensor in order to detect potential obstacles in the robot's path and how far away they are. The simplest form of loop is sometimes called a "repeat" loop. In RoboBlockly it looks like this (found in the "Loops" tab menu): The repeat loop will execute whatever code is inside it for the specified number of times (or "iterations"). We start with a simple example of the robot repeatedly moving forward 20 units and turning 137 degrees.
|
Grid Size: S M L | Simple View: | MinView: |
|
Coord: x-axis y-axis | Grid Lines: x-axis y-axis | |||
Grid: | 12x12 inches | 24x24 inches | 36x36 inches | |
72x72 inches | 96x96 inches | 192x192 inches | ||
1x1 inches | 2x2 inches | Fraction: | ||
xy-Range: | ||||
|
||||
|
||||
Quad: | 1 Quadrant | 4 Quadrants | 1&4 Quadrants | |
Units: | US Customary | Metric | Ruler: |
Labels: |
|
|
|
Font
px
|
|
Tics Lines: |
|
Width
px
|
Hash Lines: |
|
Width
px
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
(
,
in
) in
|
|
degrees
|
|
|
Using the pre-placed code blocks, insert a driveDistance and a turnLeft command into the repeat loop, where the distance to be moved is 20 units and the turn angle is 137 degrees. Set the number of iterations to 12, with a speed of 8, so you can see how it works. After you get it running, experiment with different values for the number of iterations, the speed, the distance, and the angle. (The onscreen virtual robot can move very fast, up to about 200 inches/second. Hardware robots are limited to about 8 inches/second.) |
|
|
|||||||||||
|
|||||||||||