|
Sometimes it is useful for us to be able to tell the robot to start driving and keep driving until we tell it to stop. For example, we can use a joystick connected to an Arduino board to control a Linkbot. If we push the joystick forward, we want the robot to keep moving forward until we move the joystick in a different direction. (See the Physical Computing with Arduino curriculum to learn how to write a program that does this.) We can also build a Linkbot+Arduino combination that acts like an autonomous vehicle. It uses an ultrasonic sensor to detect obstacles in front of the robot and the distance to the obstacle. The idea is to have the robot keep driving until it gets too close to an obstacle, at which point the program tells the robot to stop. To do so, we write a program that starts the robot driving forward. While it's driving, the program continually checks the distance readings from the ultrasonic sensor. If the distance reading gets too small, indicating that the robot is about to run into an obstacle, the program stops the robot. (See the Robotic Technologies curriculum to learn how to build the Linktbot+Arduino+ultrasonic sensor combination.) In this lesson we introduce the blocks that are used to have the robot start driving and keep driving until the program tells it to stop.
2. The driveForeverNB and stop Blocks To tell the robot to start driving and keep driving, we use the driveForeverNB block (found in the Robot menu at the end):
It is a non-blocking (NB) block because we want the program to be able to do other things while the robot is driving forward. (For more on how non-blocking blocks work, see this lesson.) When we want the robot to stop, we use the stop block (also found at the end of the Robot menu):
Example 1 shows a simple use of these blocks. It starts the robot using driveForeverNB, delays 5 seconds, and then stops the robot. (Note that a simpler way to write this program would be so use a driveTime(5) block. But the point of the example is to see how driveForeverNB works.) Example 2 expands on Example 1 by using a random number for the delay time. In addition, after the first stop, it turns the robot and repeats the process. Example 3 expands on Example 2 by adding a loop to the code so that the robot drives, stops, and turns 5 times.
|
| driveForeverNB, stop
|
| The code starts the robot using driveForeverNB, delays 5 seconds, and then stops the robot. |
| This code expands on Example 1 by using a random number for the delay time. In addition, after the first stop, it turns the robot and repeats the process. |
| This code expands on Example 2 by adding a loop to the code so that the robot drives, stops, and turns 5 times. |
| 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
|
|
|
|||||||||||
|
|||||||||||
