KIPR · Botball Explorer
Activity Sections

Discovery · Coding Project 9

Names for Your Numbers

Your comments become real code. Change a number once, and the whole program changes with it.

Project
Coding Project 9
Strand
Coding
Phase
Make It Reliable
Time
One class period
What You Are Doing
Turning the numbers scattered through your programs into named , then using that cleaner code to stack the red cubes.
Mission Anchor
Mission 5 — Top Shelf Delivery (base + bonus) — 20 points
Before You Start
Project 8 — you need a working grab sequence and all seven of your numbers.
What You Need
Before you start: type your PIN in the box at the top of the page. Your teacher gave you this number. When you finish, press Submit & Download to turn in your work and save a copy.

Try It — Count the Damage

Open your Project 8 program — the big one with all four missions in it. Scroll through it slowly.

How many times does this appear as a bare number?Count
My arm’s servo
My arm’s “up” position
My arm’s “down” position
My claw’s “open” position
My claw’s “closed on a cube” position

Now break it

⚠ Your Teammate Rebuilt the Claw Last Night

It grips better now. But the closed position is different — it is 200 lower than it was.

Find every place that number appears in your program and change it. Time yourself.

QuestionAnswer
How long did it take?
How many places did you have to edit?
Are you certain you got them all?

That last question is the real problem. If you missed one, what would happen — and how would you find out?

This Gets Worse, Not Better

Your program is going to keep growing. By the time you are running a full match, that number could be in twenty places. There is a fix, it takes one line, and you are about to learn it.

Learn It — Give the Number a Name

A is a name that holds a value. You set it once at the top of your program, then use the name everywhere instead of the number.

Here is the whole idea in one line:

int arm = 0;
Code / partWhat it means
intShort for — a whole number. Ports and servo positions are always whole numbers.
armThe name. Pick something simple and descriptive.
= 0The value it holds.
;A , same as any other .

Comments to code: three small edits

You have been writing your positions as since Project 7, in the name = number format. There was a reason for that. Turning a comment into a variable takes three changes:

  • Delete the two slashes.
  • Add int at the front.
  • Add a semicolon at the end.

Before — comments

int main ()
{
	// arm  = 0
	// up   = 230
	// down = 1234

	...
}

After — variables

int main ()
{
	int arm  = 0;
	int up   = 230;
	int down = 1234;

	...
}

Then use the names

Before

set_servo_position(0, 1234);
msleep(500);
set_servo_position(0, 230);

motor(0, 50);
motor(3, 50);

After

set_servo_position(arm, down);
msleep(500);
set_servo_position(arm, up);

motor(left, 50);
motor(right, 50);

Read both versions out loud. One is a list of numbers. The other tells you what the robot is doing.

Five reasons this is worth the trouble

  1. You do not have to remember which port is the arm and which is the claw — the computer remembers for you.
  2. You do not have to remember which number is up, down, open, or closed.
  3. Your program is easier to read.
  4. Your program is easier to debug.
  5. If a port or a position changes, you change one line.

Name things properly

Short names are fine if they are obvious. If they are not obvious, explain them:

// L is my left wheel
int L = 0;
// R is my right wheel
int R = 3;

A name nobody can decode is barely better than a bare number.

A Yellow Banner Is Not an Error

If you declare a variable and never use it, you will get Compilation Succeeded with Warnings — a yellow banner instead of a green one. The program still runs.

But warnings exist for a reason. A variable you declared and never used usually means you forgot to swap out one of the bare numbers.

Variables can change

Most of your variables hold one value forever — a port is always the same port. But a variable is called a variable because it can vary.

int position = 250;

// Now 260
position = position + 10;
// Back to 250
position = position - 10;

Adding or subtracting one is so common it has its own shorthand:

Code / partWhat it means
position++;Exactly the same as position = position + 1;
position--;Exactly the same as position = position - 1;

You will meet the real use for these in Project 10, once you have a loop to put them in.

Do It — Clean It Up, Then Score

Step 1 — Convert one small program first

Open your Wave program from Project 7. Turn the three position comments into variables, then replace the numbers in the set_servo_position() lines with the names.

. Run. It should behave exactly as before.

If it behaves differently, you swapped two values. That is the most common mistake here.

Step 2 — Trigger the yellow banner on purpose

Add a variable you do not use anywhere:

int notused = 999;

Compile and look at the banner colour.

What did the warning say?

Delete the line. Get your green banner back.

Step 3 — Convert your big Project 8 program

This is the real job. Declare all your variables at the top, then work down the program replacing bare numbers with names.

VariableMy valueDone?
left
right
arm
claw
up
horizontal
down
open
closed

Convert One Mission Section at a Time

Same rule as building it in the first place. Convert one section, compile, run it, then move on. Converting all four at once and finding it broken tells you nothing about where.

Step 4 — Now do the claw change again

Same problem as Try It. Your claw’s closed position drops by 200. Change it.

QuestionAnswer
How long did it take this time?
How many places did you edit?

Compare those two numbers with the ones you wrote in Try It.

Step 5 — Try changing a variable while the program runs

Small experiment. Add this to a test program and watch the arm.

int arm = 0;
int position = 250;

set_servo_position(arm, position);
enable_servos();
msleep(500);

// Change the variable
position = position + 300;
set_servo_position(arm, position);
msleep(500);

// Change it again
position = position + 300;
set_servo_position(arm, position);
msleep(500);

disable_servos();

The arm moved in three hops instead of one jump. What would you have to do to make it move in thirty small hops?

Hold that thought. Project 10 gives you the tool that makes it one line instead of thirty.

Step 6 — Mission 5 — Top Shelf Delivery for 20 points

Watch Mission 5 video

Base Mission

9 points Live judged

A Small Red Cube is the Large Red Cube.

Bonus Mission

11 points Live judged

Both Small Red Cubes are the Large Red Cube.

Scores

  • One Small Red Cube is the Large Red Cube.
  • One Small Red Cube is touching the upper surface of the Large Red Cube while supported by a robot.
  • One Small Red Cube is the Large Red Cube and later falls off.
  • Both Small Red Cubes are the Large Red Cube at the same time.

Does Not Score

  • A Small Red Cube touching only the side of the Large Red Cube.
  • A Small Red Cube hovering above the Large Red Cube without touching it.
  • Only one Small Red Cube is the Large Red Cube for the Bonus Mission.
This one asks your arm to reach higher than anything so far. The large red cube is the platform; the small ones go it.

Live Judged Is On Your Side Here

Robot support is permitted, and the cubes do not have to stay up there. Once the judge has seen it, it counts — even if it falls a second later.

⚠ Check What Mission 2 Did to These Cubes

In Project 6 you pushed the Large Red Cube, its , and both Small Red Cubes off the black line. Those are the same cubes you need here. Where they ended up decides how hard this is — so plan the two missions together, not separately.

Where do your red cubes end up after your Mission 2 push? Does that help or hurt you here?

The bonus needs both cubes up there at the same time. Why does that make the second one harder than the first?

Step 7 — Run it five times

RunFirst cube on top?Both at once?Points
1
2
3
4
5

Score It — Checkpoint

My score

Mission partScored?Points
Mission 5 — Base (one small red cube on top)9
Mission 5 — Bonus (both small red cubes on top)11
My total20

Write the variable

Turn each comment into a proper variable declaration.

CommentVariable
// claw = 3
// open = 1246
// left = 0

Read the code

Given int arm = 0; and int down = 1234; — what does each line do?

LineWhat it does
set_servo_position(arm, down);
down = down - 50;
arm++;
That last one is legal code that does something you almost certainly do not want. Say what it actually does.

Can you do it again?

Think about it

Nothing your robot does changed in this project — it drives, grabs, and stacks exactly as it did before. So what actually got better?

Variables fixed the problem of one number appearing in many places. But your program still has the six-step grab sequence typed out four separate times. Would variables fix that too? Why or why not?

Your teammate opens your program for the first time. Which version would they be able to work on — the one from Project 8, or this one? What does that tell you about who you are really writing code for?

Next

Your robot still cannot tell when it has arrived anywhere. It drives for a length of time and hopes.

In Project 10 — Feeling for Things, it gets its first sense: a switch that knows when it has touched something — and the loop that keeps checking.

KIPR · Botball Explorer — Discovery Projects · © KISS Institute for Practical Robotics 1997–2026

When you are finished, press the button to turn in your work and save a copy.

KIPR · Botball Explorer · Discovery