Setting up the loop and switches

As with the RC program, the first step is to add an infinite loop into which the rest of the code will be placed:

The EV3 will first check the status of the US sensor (port 1). Since this sensor is mounted low, it will see almost all of the obstacles that the Omnilander will encounter. If the US sensor sees an obstacle that is less than 20 centimeters away, the robot knows that a collision is imminent and it must decide whether to climb or steer away.

To program this step, add a switch block and set its mode to Ultrasonic Sensor | Compare | Distance Centimeters. Make sure that you select sensor port 1. Then, ...

Get Building Smart LEGO MINDSTORMS EV3 Robots now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.