Lesson 5.2: Using else if Chains to Pick One Robot Action at a Time
Technical Context
Using else if chaining prevents conflicting hardware commands within the same execution cycle. For example, if a programmer uses two independent if statements to control an arm (one for "up" and one for "down"), pressing both buttons simultaneously could send rapid, conflicting voltage signals to the Control Hub, causing jitter and mechanical strain on the motor gears.
Why else if Chains Prevent Conflicting Commands
else if and else clauses allow for the creation of a priority ladder. When Java encounters an if/else if chain, it evaluates conditions from top to bottom and executes only the first block that is true. The final else block acts as a catch-all fail-safe, which is critical for ensuring hardware returns to a "neutral" or "zero-power" state when no inputs are active. This structure ensures that the DcMotor's setPower() method receives exactly one unambiguous command per loop.
Annotated Code
@Override
public void loop() {
// Priority 1: Move Forward
if (gamepad1.dpad_up) {
driveMotor.setPower(0.5);
}
// Priority 2: Move Backward (Only checked if dpad_up is false)
else if (gamepad1.dpad_down) {
driveMotor.setPower(-0.5);
}
// Fail-safe: Stop (Executed only if both inputs are false)
else {
driveMotor.setPower(0.0);
}
}
Fill-in-the-Blank Practice
- To handle a condition that is only checked if the previous
ifstatement was false, we use the__________keywords. - The
__________block is used to execute code when all other preceding conditions in the chain have evaluated tofalse. - Chaining logical decisions is essential to prevent sending
__________commands to hardware in the same loop cycle.
Show answers
else ifelse- conflicting
Template Challenge
Robot Scenario: Control a Servo named "grabber". If Button A is pressed, set the position to 1.0 (closed). If Button B is pressed, set it to 0.0 (open). Otherwise, the servo should stay at 0.5 (ready position).
Show answer
@Override
public void loop() {
if (gamepad1.a) {
grabber.setPosition(1.0);
} else if (gamepad1.b) {
grabber.setPosition(0.0);
} else {
grabber.setPosition(0.5);
}
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.