Skip to main content

Lesson 5.3: Using Comparison Operators to Protect Hardware Limits


Technical Context

Comparison operators allow the robot to interpret analog data — such as encoder ticks or sensor voltage — to enforce physical safety limits. Implementing "Soft Limits" via comparison logic prevents the robot from attempting to drive a mechanism past its physical hard-stop, which would otherwise result in stalled motors, stripped gears, or blown fuses on the Control Hub.


How Comparisons Create Safe Operating Zones

Hardware sensors return numerical values that must be compared against thresholds. Java provides several conditional operators for this purpose: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to). In the case of an arm mechanism, the programmer compares the current DcMotor encoder position against a maximum "Safe-Zone" integer. If the position exceeds the limit, the logic overrides driver input to force the power to zero.


Annotated Code

@Override
public void loop() {
double liftPower = -1 * gamepad1.left_stick_y;
int currentPos = liftMotor.getCurrentPosition();

// Hardware Protection Logic: Max Height is 2000 ticks
if (currentPos > 2000 && liftPower > 0) {
// Overriding power to prevent mechanical over-extension
liftPower = 0.0;
telemetry.addData("Safety", "MAX LIMIT REACHED");
}

liftMotor.setPower(liftPower);
}

Fill-in-the-Blank Practice

  1. To test if two numerical hardware values are exactly equal, the __________ operator must be used.
  2. The __________ operator is used to check if a sensor value is "not equal" to a specific constant.
  3. A common error is accidentally using the single = operator, which is for __________, instead of the double == operator.
Show answers
  1. ==
  2. !=
  3. assignment

Template Challenge

Robot Scenario: Your team is using a Distance Sensor to prevent the robot from hitting walls. If the distance reading is less than 10.0 cm, the motor power should be set to 0.0 immediately.

Unit 5 simulator
Supports gamepad-driven `if`, `else if`, and `else` logic with live branch visualization.
Includes truth-table and condition-inspector views, plus mock distance and color sensor inputs when needed.
Best for practicing decision logic, thresholds, and simple actuator commands.
Show answer
@Override
public void loop() {
double dist = distanceSensor.getDistance(DistanceUnit.CM);

if (dist < 10.0) {
driveMotor.setPower(0.0);
} else {
driveMotor.setPower(-1 * gamepad1.left_stick_y);
}
}

Ready to move on?

Sign in with Google to save your progress with Telemark, or continue without saving.