Building Smart LEGO MINDSTORMS EV3 Robots
上QQ阅读APP看书,第一时间看更新

Using the compare switch

We will program a turret fire case directly after the aiming code we just wrote. Drag out a switch block and set its mode to Infrared Sensor | Compare | Beacon Heading. This mode allows us to set a desired beacon heading value. The EV3 will check the current sensor reading and compare it to the value we set here. The compare switch block is great because it combines three steps into a single block: it reads the sensor, compares its value to the desired target, then decides which case to execute based on whether the sensor value matches the target. In our specific example, if IR 2 measures a beacon heading of exactly zero, the EV3 will run the true case. Otherwise, it will run the false case.

We will set zero as our desired heading value; enter 0 into the threshold value input of the switch (the third input). We only want the turret to fire when the heading equals exactly zero, so the next step is to change the compare type (the second input on the switch block) to =. Finally, make sure that this switch block is using the sensor in port 2.