Instructional Material: Exercise: Drive in a Square Pattern

Now lets expand on the previous example. This OpMode shows changing direction, use of a for loop and time delays to drive in a square pattern by programming only two of the moves needed (drive straight then turn).

Note that the robot steers or changes direction by varying the motor power being applied to each side. If the motors are not turning at the same power level in the same direction, the robot will turn. This is called skid steering.
Copy and paste this code into a new class called DriveSquare and demonstrate your robot driving in a square. You may need to adjust the sleep times to get accurate 90 degree turns to complete the square. Note that because of the use of the for loop and the code design that moves forward then turns for each of the four parts of the square, the robot will turn one extra time. An exercise for you is to modify the program to eliminate the extra turn.
Material Type: 
Lab Activity
Education Level: 
Middle School
High School
Focus Subject: 
Computing / Computer Science
Robotics Software
HW Platform: 
SW Platform: 
Interactivity Style: