Lesson 6.2: Using for Loops to Repeat Robot Actions Cleanly
Technical Context
Repetitive mechanical tasks — such as pulsing an intake or cycling a scoring mechanism — are prone to "logic bloat" if written line-by-line. Using a for loop ensures code efficiency and allows the programmer to adjust the number of iterations (e.g., 3 gear-clearing pulses) in a single parameter change rather than rewriting blocks of code.
When a for Loop Is Better Than Repeating Code
The for loop follows the syntax for(initialization; condition; update). It instantiates a local int counter that increments after each pass. In FTC robotics, this is frequently used to cycle mechanisms for a fixed number of iterations rather than relying on inconsistent time-based logic.
The three parts of the for header do distinct jobs:
- Initialization — runs once before the loop starts, creating the counter (e.g.,
int i = 0) - Condition — checked before every iteration; the loop stops when this is
false(e.g.,i < 5) - Update — runs at the end of every iteration, advancing the counter (e.g.,
i++)
Annotated Code
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.hardware.DcMotor;
@Autonomous(name="Pulse_Demo")
public class PulseDemo extends LinearOpMode {
@Override
public void runOpMode() {
DcMotor scoringMotor = hardwareMap.get(DcMotor.class, "scoring");
waitForStart();
// Example: Pulsing a scoring motor 5 times
for (int i = 0; i < 5; i++) {
scoringMotor.setPower(1.0);
sleep(100); // 100ms on-pulse
scoringMotor.setPower(0.0);
sleep(100); // 100ms pause between pulses
telemetry.addData("Pulse", i + 1);
telemetry.update();
}
}
}
Fill-in-the-Blank Practice
- In the
forloop header, the__________section is executed exactly once before the loop begins. - To repeat a block of code exactly 10 times starting at
i = 0, the condition should be written asi < __________. - The
__________statement is executed at the end of every iteration, typically used to increase the counter.
Show answers
- initialization (e.g.,
int i = 0) 10- update (e.g.,
i++)
Template Challenge
Robot Scenario: Your robot needs to "flick" a servo named "delivery" three times between position 0.0 and 1.0 to ensure a game element has been released.
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.hardware.Servo;
@Autonomous(name="Triple_Flick")
public class TripleFlick extends LinearOpMode {
@Override
public void runOpMode() {
Servo delivery = hardwareMap.get(Servo.class, "delivery");
waitForStart();
// INSERT CODE HERE: Create a for loop that runs exactly 3 times
{
// INSERT CODE HERE: Set position to 1.0, sleep 200ms, then set to 0.0
}
}
}
Show answer
for (int i = 0; i < 3; i++) {
delivery.setPosition(1.0);
sleep(200);
delivery.setPosition(0.0);
sleep(200);
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.