diff --git a/data/lessons.js b/data/lessons.js index 153411b..cca8f76 100644 --- a/data/lessons.js +++ b/data/lessons.js @@ -1101,25 +1101,39 @@ robot.turn(0) level: 'robot', map: 'Level 3', content: ` -
Turning is very similar to moving, we use the robot.turn(amount) function.
The amount parameter is a number between -1 and 1, where -1 is full left, 0 is no turn, and 1 is full right.
We can't do much just by moving a robot using delays, so lets start using its sensors.
+The robot.get_distance_left() function returns the length of the left sensor beam.
When there is something blocking it, it gets smaller, and you can use this to detect obstacles.
+Try this code, and watch the console output:
import robot
import time
-robot.turn(1)
-time.sleep(2)
-robot.turn(0)
+robot.move(0.5)
+while True:
+ distance = robot.get_distance_left() # Get the distance from the left sensor
+ print(distance) # Print the distance
+ time.sleep(0.1) # Wait for on tenth of a second
-This code causes the robot to turn right at max speed for 2 seconds, then stop.
+Note how the distance changes as the robot moves.
+We can use this to automatically steer around obstacles.
+Add the following code AFTER the print(distance) line:
+if distance < 50: # If the distance is less than 0.5 meters
+ robot.turn(1) # Turn right at max speed
+else:
+ robot.turn(0) # Stop turning
+
+
+If you want to get the length of the right sensor, it's robot.get_distance_right().
You'll need to combine moving, turning, and waiting to reach all the checkpoints.
-Note: The values for move, turn, and sleep can all be decimal numbers (floats). ie time.sleep(0.5) or robot.move(0.8)
Note: Having that short time.sleep(0.1) is very important, if the loop runs too fast it overwhelms the system and nothing will happen.