What is a program?
Hook
Your first robot task: move an arm from 0° to 90° and hold it there.
You write what seems obvious:
void periodic() {
arm.moveToAngle(90);
}
The robot doesn’t move. There is no moveToAngle method — the arm doesn’t know what that means. It only knows “set motor power to this number.” You can’t tell the robot what to accomplish. You have to tell it exactly how, step by step.
Core concept
A program is a sequence of instructions, executed in order, by something that does exactly what you say — no more, no less.
That last part is the trap. A computer will do precisely what your program tells it to. If your program is wrong in a subtle way, the robot will be subtly wrong, confidently, fifty times a second.
Walk-through
The task: move an arm from 0° to 90°. The arm has a motor and an encoder (a sensor that measures the current angle).
Instead of “move to 90°” (a goal), the program has to describe exactly what to do:
- Read the current angle from the encoder.
- If the angle is less than 88°, run the motor forward.
- If the angle is greater than 92°, run the motor backward.
- If the angle is between 88° and 92°, stop the motor.
- Go back to step 1.
That’s not five goals — it’s five actions the hardware can actually do. The robot executes this loop fifty times per second. Each cycle it reads the sensor, makes a decision, and acts.
Interactive demo
Here’s those five steps as real Java code. The arm starts at 45°. Step through and watch what the program actually does:
void periodic() { double angle = encoder.getAngle(); if (angle < 88) { motor.set(0.5); } else if (angle > 92) { motor.set(-0.5); } else { motor.set(0.0); }}Step through to see values update.
The program never says “go to 90°.” It says: read the sensor, compare to a threshold, set a motor power. The robot reaches 90° as a consequence of running those four lines thousands of times.
Notice what happens if you remove the else branch: the motor never gets turned off. The arm would overshoot 90°, then the angle > 92 check would kick in and drive it back — past 88°, triggering the forward motor again. The arm would oscillate forever. Every branch matters.
Try it yourself
Trace this program by hand. Write the value of angle after each step before you check.
int angle = 0;
angle = angle + 30; // Step A
angle = angle + 30; // Step B
angle = angle - 10; // Step C
angle = angle + 30; // Step Dint angle = 0;angle = angle + 30;angle = angle + 30;angle = angle - 10;angle = angle + 30;Step through to see values update.
What is the value of angle after Step D?
Key takeaways
- A program is a sequence of instructions, executed in order.
- Computers do exactly what you say — including your mistakes.
- Robot programs loop continuously, reading sensors and acting on what they find.
- “Move to 90°” is a goal. “If angle < 88, run the motor forward” is a program.
Common confusions
“This seems too simple.” It is, on purpose. Programs don’t get conceptually harder — they get longer. The skill you’re building right now is being explicit, and it’s easier to learn on something small.
“Won’t the robot figure out what I mean?” No. A robot has no common sense — only instructions. Every 498 senior has spent at least one late night debugging because they assumed the robot knew something it didn’t.
Challenge
A robot arm starts at 0°. Your program runs four moves in sequence. Write them as Java statements and print the final angle.
Moves: add 45°, add 30°, subtract 20°, add 10°
Stuck? Show hint
Each move is one line: angle = angle + 45; etc. Execute them in order, then print.
What’s next
In Lesson 02, we’ll look at state and memory — how a program remembers values between loop cycles, and why where you declare a variable can quietly break everything.