Skip to main content

Lesson 2.4: Using start() and stop() for One-Time Robot Actions


Context

start() and stop() improve robot reliability by handling transitions. start() is often used to reset the getRuntime() timer exactly when the match begins, ensuring autonomous timing is accurate. stop() acts as a fail-safe, allowing you to force motors to zero power or close files when the match ends, preventing hardware damage or data corruption.


start() and stop() Functions

While init() and loop() are required, start() and stop() are optional lifecycle methods.

start() is called once the instant the "Play" button is pressed, making it more precise than init() for resetting counters.

stop() is called once when the "Stop" button is hit or the match timer expires. This is the professional way to handle cleanup logic, such as ensuring a high-speed flywheel is commanded to setPower(0) before the program terminates.


Annotated Code

@Autonomous(name="Transition_Demo")
public class TransitionDemo extends OpMode {

@Override
public void init() {
telemetry.addData("Hub", "Standby");
}

@Override
public void start() {
// Resetting the internal clock the moment the match begins
resetRuntime();
}

@Override
public void loop() {
telemetry.addData("Match Time", getRuntime());
}

@Override
public void stop() {
// Cleanup: Ensures no logic persists after the program ends
telemetry.addData("Match", "Complete");
}
}

Fill-in-the-Blank Practice

  1. The __________ method is called exactly once when the driver presses the "Play" button.
  2. To ensure all motors are deactivated at the end of a match, logic should be placed in the __________ method.
  3. The method that runs repeatedly between the "Init" and "Play" buttons being pressed is __________.
Show answers
  1. start()
  2. stop()
  3. init_loop()

Challenge

Your team needs to reset the robot's timer at the start of the match and display a "Robot Shutdown" message when the program is stopped.

Unit 2 simulator
Supports `init()`, `init_loop()`, `start()`, `loop()`, and `stop()`.
Reads `@TeleOp`, `@Autonomous`, `@Disabled`, telemetry, and `gamepad1`.
Handles local variables, simple math, `resetRuntime()`, and `getRuntime()`.
Does not execute hardware, `gamepad2`, `else` blocks, or loop structures.
Show answer
@Override
public void start() {
resetRuntime();
}

@Override
public void stop() {
telemetry.addData("Status", "Robot Shutdown");
}

Ready to move on?

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