Lesson 3.3: Using boolean Values for On-or-Off Robot State
Technical Context
Sensors like Touch Sensors or Limit Switches only have two states: pressed or not pressed. Using boolean flags to store these states allows the robot to make binary decisions, such as stopping a lift before it hits a physical hard-stop and causes mechanical failure.
How boolean Values Simplify Robot Decisions
A boolean is a primitive datatype that can only hold one of two values: true or false. In the FTC environment, digital sensors and gamepad buttons are inherently boolean. When you call getState() on a DigitalChannel (like a touch sensor), the SDK returns a boolean. Storing this result in a variable allows you to "capture" the state at a specific moment in the loop() cycle and use it for logic comparisons.
Annotated Code
@TeleOp(name="Boolean_Logic")
public class BooleanLogic extends OpMode {
// Declaring a boolean to track if the safety limit is reached
boolean isAtLimit;
DcMotor lift;
DigitalChannel touch;
@Override
public void init() {
lift = hardwareMap.get(DcMotor.class, "lift");
touch = hardwareMap.get(DigitalChannel.class, "limit_switch");
}
@Override
public void loop() {
// Capturing the current hardware state into the boolean variable
isAtLimit = touch.getState();
telemetry.addData("Safe to move?", isAtLimit);
}
}
Fill-in-the-Blank Practice
- A variable type that can only represent two possible states is a/an
__________. - The default value of a
booleanvariable in Java, if not assigned, is__________. - To invert a
booleanvalue (changingtruetofalse), we use the__________operator.
Show answers
booleanfalse!(the logical NOT operator)
Simulator Challenge
Robot Scenario: Capture the state of the "A" button on gamepad1 into a boolean variable and send that variable to the Driver Station via telemetry.
Show answer
@Override
public void loop() {
boolean buttonState = gamepad1.a;
telemetry.addData("Button A Pressed", buttonState);
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.
Stuck on this lesson?
Ask about anything on this page. It can see which lesson you have open and which part you are reading.