Program the return-to-center (right side)
If the turret has strayed too far to the right (motor D has > | 250 degrees), the tank will need to make a sharp right turn to center the beacon within its line of sight again and center the turret. We will add some programming in the true case of our new switch that will do just that.
First, add a medium motor block and set it to turn motor D off. This case executes when the turret starts to rotate too far, so we need to stop it to make sure it does not over-rotate.
Next, the tank will make its sharp right turn. We will use the move tank block, which allows us to individually control the power of both drive motors (port B + C). Set its mode to On for Seconds. Set the power of motor B, the left drive motor, to 75 percent. Set the power of motor C, the right drive motor, to -75 percent. Powering the motors in opposite directions allows the tank to make a quick spin turn. We want the tank to turn for 1.2 seconds, so enter that as the target time value.
Directly after that, we can place another move tank block, this time with the mode set to Off. This will halt the drive motors after the tank has made its turn.
The last part of this segment is some code that returns the turret to center. Add a loop block. We will set a special exit case for this loop. Change the mode on the loop to Motor Rotation | Compare | Degrees. This will work in a similar manner to the compare switches we programmed earlier. We will set a target degree value and the motor will rotate until it reaches this target. Change the compare type to < (type 4) and make the threshold value 3 degrees. Make sure the motor in port D is selected. Inside the loop, place a medium motor block. Set the block to simply turn the motor On at -50 percent power. This loop will power motor D in the negative direction until its degree value is less than three (which is very close to the center):