Java While Loops → Java Break/Continue
Complete the W3Schools lessons from Java While Loop through Java Break/Continue.
Make sure to paste all code challenges into your section of the doc.
Robotics Programming Challenge: Intake Control
Objective
You are programming the intake mechanism of an FRC robot.
Your job is to write the code that controls two motors:
- Intake motor — pulls game pieces into the robot
- Indexer motor — moves game pieces farther into the robot
The robot operator has several controls, and the intake has a sensor that detects when a game piece has been collected.
You will be given some starter code, but you are responsible for writing the majority of the control logic.
1. Starter Code
Begin with:
public class IntakeChallenge {
public static void main(String[] args) {
// Operator controls
boolean intakeButton = true;
boolean outtakeButton = false;
// Intake sensor
boolean hasPiece = false;
// Motor speed from 0.0 to 1.0
double motorSpeed = 0.75;
// Motor outputs
double intakeMotor = 0;
double indexerMotor = 0;
// Your code goes here
// Print final motor outputs
System.out.println("Intake Motor: " + intakeMotor);
System.out.println("Indexer Motor: " + indexerMotor);
}
}The motor outputs work like this:
| Output | Meaning |
|---|---|
1.0 | 100% forward |
0.5 | 50% forward |
0.0 | Stopped |
-0.5 | 50% reverse |
-1.0 | 100% reverse |
2. Program the Intake
When the operator presses the intake button, the intake should run forward.
If:
intakeButton == truethen:
intakeMotorshould equalmotorSpeedindexerMotorshould equalmotorSpeed
If the button isn’t pressed, the motors should not run unless another command tells them to.
You’ll need to decide what conditional structure makes sense here.
Display the Motor Speed
After setting the motor outputs, print the motor speed as a percentage.
Use an explicit cast to convert the calculated percentage to an integer:
System.out.println("Motor Speed: " + (int)(motorSpeed * 100) + "%");For example, if motorSpeed is 0.75, the output should be:
Motor Speed: 75%Print this line whenever the intake or outtake button is pressed.
3. Program the Outtake
The robot also needs to be able to spit a game piece back out.
When:
outtakeButton == truethe motors should run backward.
For example:
intakeMotor = -motorSpeed;The indexer should also run backward.
Important
What happens if both intakeButton and outtakeButton are pressed?
Your program should choose one behavior rather than trying to run the motors in both directions.
Decide which command should have priority and implement it using if, else if, and else.
3.5. Use a Switch Statement
In addition to your if, else if, and else statements, use a switch statement to determine the robot’s current operating mode.
Create an integer variable:
int operatingMode = 0;Use the following modes:
| Mode | Behavior |
|---|---|
| 0 | Idle |
| 1 | Intake |
| 2 | Outtake |
Use a switch statement to print the current operating mode:
Mode: Idle
Mode: Intake
Mode: OuttakeYou may determine the operating mode using the operator buttons.
This statement should be used somewhere in your completed program.
4. Add the Game-Piece Sensor
The intake has a sensor called:
boolean hasPiece = false;This tells you whether the robot currently has a game piece.
If the robot detects a game piece while intaking:
- The indexer should stop
- The intake should continue running
- Print:
GAME PIECE DETECTED
INDEXER STOPPEDFor example:
Intake Motor: 0.75
Indexer Motor: 0.0Think carefully about the order of your conditions.
The sensor should be able to override the normal indexer behavior.
## 5. Add a Motor Safety Limit
Real robot code shouldn’t blindly trust its inputs.
Add a safety check so that motorSpeed can never cause the motor to run above 100%.
For example, if:
double motorSpeed = 1.25;your program should prevent the motor from running at 125%.
Print:
WARNING: MOTOR SPEED LIMITEDand adjust the speed to the maximum allowed value.
Important: Motor speed must always be between 0.0 and 1.0.
- If
motorSpeedis greater than1.0, set it to1.0. - If
motorSpeedis less than0.0, set it to0.0. - Otherwise, leave it unchanged.
This safety check should happen before calculating motor outputs.
6. Simulate Collecting a Game Piece
Now use a loop to simulate the intake collecting a game piece.
Only run this simulation when the intake button is pressed and the robot does not already have a game piece.
Start with:
int distanceToPiece = 5;The distance represents how many arbitrary units away the game piece is.
While the robot is intaking, decrease the distance:
Distance to piece: 5
Distance to piece: 4
Distance to piece: 3
Distance to piece: 2
Distance to piece: 1
Distance to piece: 0
GAME PIECE COLLECTEDYou will need to:
- Create a loop
- Use a comparison
- Change the distance each iteration
- Stop the loop when the piece reaches the robot
- Print the distance each iteration
Test A should include the distance simulation output.
7. Test Your Code
Change the starter variables to test different situations.
Test A — Intake
boolean intakeButton = true;
boolean outtakeButton = false;
boolean hasPiece = false;
double motorSpeed = 0.75;Expected:
Distance to piece: 5
Distance to piece: 4
Distance to piece: 3
Distance to piece: 2
Distance to piece: 1
Distance to piece: 0
GAME PIECE COLLECTED
Intake Motor: 0.75
Indexer Motor: 0.75
Motor Speed: 75%Test B — Already Have a Piece
boolean intakeButton = true;
boolean outtakeButton = false;
boolean hasPiece = true;
double motorSpeed = 0.75;Expected:
GAME PIECE DETECTED
INDEXER STOPPED
Intake Motor: 0.75
Indexer Motor: 0.0
Motor Speed: 75%Test C — Outtake
boolean intakeButton = false;
boolean outtakeButton = true;
boolean hasPiece = true;
double motorSpeed = 0.50;Expected:
Intake Motor: -0.5
Indexer Motor: -0.5
Motor Speed: 50%Test D — Nothing Happening
boolean intakeButton = false;
boolean outtakeButton = false;
boolean hasPiece = false;
double motorSpeed = 0.75;Expected:
Intake Motor: 0.0
Indexer Motor: 0.0Test E — Motor Speed Limit
double motorSpeed = 1.25;Expected:
WARNING: MOTOR SPEED LIMITED
Intake Motor: 1.0
Indexer Motor: 1.0
Motor Speed: 100%Final Requirements
Your completed program must use:
booleanintdoubleifelse ifelseswitch- A
fororwhileloop - Arithmetic operators
- Comparison operators
- An explicit cast
- Intake control
- Outtake control
- Game-piece detection
- Motor speed limiting
CONGRATULATIONS!
You have completed what I consider starter Java.
You know how to do a lot of cool things in a main method, and you have become good at taking apart challenges and completing them on your own.
Up next is more basic Java, but it is going to take you much further than anything you’re able to do right now.
Keep it up!
To complete this lesson
Paste all your code challenges, including the Intake Control challenge, into this lesson’s tab in your training doc. Code goes in as text, no screenshots.