Skip to main content

Lesson 3.5: Challenge: Build Dynamic Power Logic with Multiple Variables


Technical Context

A competitive robot rarely runs at a static speed. By combining multiple variables (inputs, multipliers, and offsets), you can create dynamic control systems. For instance, scaling joystick input by a "Battery Multiplier" variable ensures consistent robot performance even as the 12V battery voltage drops during a match.


How to Think Through Multi-Variable Power Logic

This challenge requires manipulating different variable types in a single expression. When an int and a double are used in the same math operation, Java "promotes" the result to a double to maintain precision. You must be careful with assignment operators; using = updates the variable's value, while using *= or += allows you to modify the current value relative to its previous state.


Annotated Code

@TeleOp(name="Dynamic_Control")
public class DynamicControl extends OpMode {

DcMotor drive;

// Multi-variable setup
double baseSpeed = 0.5;
double boostMultiplier = 2.0;
int gearSetting = 1;

@Override
public void init() {
drive = hardwareMap.get(DcMotor.class, "motor");
}

@Override
public void loop() {
double finalPower;

if (gamepad1.right_trigger > 0.5) {
// Math combining double inputs and multipliers
finalPower = baseSpeed * boostMultiplier;
} else {
finalPower = baseSpeed;
}

drive.setPower(finalPower);
telemetry.addData("Gear", gearSetting);
telemetry.addData("Output Power", finalPower);
}
}

Fill-in-the-Blank Practice

  1. The operator used to assign a new value to a variable is __________.
  2. If you multiply an int by a double, the resulting datatype will be a/an __________.
  3. To perform math and update a variable in one step (e.g., adding 5 to the current value), you use the __________ operator.
Show answers
  1. =
  2. double (Java promotes the result to the wider type)
  3. +=

Simulator Challenge

Robot Scenario: Create a "Precision Mode." If the driver holds the left bumper, the motor power should be exactly half of the joystick input. Use variables for input, scaleFactor, and finalPower.

Telemark Unit 3 Simulator
Tracks `String`, `double`, `boolean`, and `int` variables as you edit.
Supports telemetry, `gamepad1`, simple `if` blocks, and basic `setPower()` math.
Does not simulate full hardware behavior or complex Java control flow.
Show answer
@Override
public void loop() {
double input = -gamepad1.left_stick_y;

double finalPower;

if (gamepad1.left_bumper) {
finalPower = input * scaleFactor;
} else {
finalPower = input;
}

motor.setPower(finalPower);
}

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.