KIPR · Botball Explorer
Activity Sections

Discovery · Coding Project 13

Deciding What to Do

Until now your robot has followed orders. Today it starts choosing.

Project
Coding Project 13
Strand
Coding
Phase
Make It Smart
Time
One class period
What You Are Doing
Writing code that asks a question once and picks a path based on the answer — then using it to build two stacks that have to come out different.
Mission Anchor
Mission 3 — Mixed Freight (advanced bonus) — 13 points
Before You Start
Projects 10 and 12 — you need while loops and your own .
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 — Sabotage Your Own Robot

Load your Project 12 program — the blue pom run. Before you press start, move the first pom about four inches to one side.

Now run it and watch closely.

QuestionWhat happened
Did the claw close on anything?
What did the robot do next?
Did it drive to the basket anyway?
Did it open the claw at the end?

⚠ Your Robot Has No Idea Anything Went Wrong

It drove to a spot, closed a claw on empty air, carried nothing across the field, and carefully placed nothing in the basket. Then it backed away pleased with itself.

Every program you have written does this. They all assume everything goes right.

Something goes slightly wrong in a real match — a cube got bumped, your robot drifted an inch. How much of your run is ruined?

You do this all day

You never follow a plan blindly. You check things and pick.

Fill in what you would do:

If this is true……I do this. Otherwise I do this.
It is raining outside
My shoes are already on
The milk smells bad
My bag feels lighter than it should
That last one is exactly what your robot needs — a way to notice that it is carrying nothing.

Learn It — Ask Once, Then Choose

An if asks a question and only runs its when the answer is true.

if (digital(bump) == 1)
{
	printf("I am touching something!\n");
}

The in the parentheses is written exactly the same way as a while condition — same symbols, same rules, same == trap.

The one difference that matters

while — Keeps asking

while (digital(bump) == 0)
{
	motor(left, 50);
	motor(right, 50);
}

Checks, runs the block, checks again, runs again… until the answer changes.

if — Asks once

if (digital(bump) == 0)
{
	motor(left, 50);
	motor(right, 50);
}

Checks once. Runs the block once if true, skips it if false. Either way the robot moves on immediately.

Similar Words, Completely Different Job

  • while means “keep doing this until something changes.”
  • if means “look at this once, then decide what happens next.”

Swap one for the other by accident and your robot either freezes in place or blows straight past a check it needed to make.

Multiple checks

Two separate if

if (shoes are on)
{
    go for a walk;
}

if (I am hungry)
{
    eat a snack;
}

Two questions about different things. Both can be true. Both can be false. They do not affect each other.

if / else

if (shoes are on)
{
    go for a walk;
}
else
{
    watch TV;
}

Exactly one of these always runs. else has no condition — it means “every other possibility.”

if / else if / else

if (shoes are on)
{
    go for a walk;
}
else if (shoes are by door)
{
    put them on;
}
else
{
    watch TV;
}

Checked top to bottom. The first true one runs and the rest are skipped, even if they are also true.

A decision inside a decision

You can put an if inside another if. This is called .

// Did I grab something?
if (digital(claw_switch) == 1)
{
	// Am I far enough along?
	if (gmpc(left) > 3000)
	{
		release();
	}
	else
	{
		drive_forward(1000);
		release();
	}
}
else
{
	printf("Grab failed - trying again\n");
	grab();
}

Nesting more than two deep gets hard to read fast. If you need three, that is usually a sign the code wants to be a function.

⚠ No Semicolon After the Condition

if (digital(bump) == 1); — that stray makes an empty if. The block after it then runs every single time, condition or not.

Same rule as while, and it is just as hard to spot.

Do It — Make It Choose

Step 1 — Your first decision

Create a new project called Choices. Write a program that checks the touch once and prints a different message either way.

if (digital(bump) == 1)
{
	printf("Something is there\n");
}
else
{
	printf("Nothing there\n");
}
msleep(3000);

Run it twice — once holding the sensor pressed, once not.

Step 2 — Swap if for while and watch what breaks

Change your if to a while. Leave everything else alone. Run it without the sensor.

What happened?

Change it back. You have just seen the difference from the inside.

Step 3 — Pick a side with a button

Real teams run one program from either starting box. The driver presses a button to say which side.

printf("Press A for left box, B for right\n");

while (a_button() == 0 && b_button() == 0)
{
	// Wait for either button
	msleep(10);
}

if (a_button() == 1)
{
	run_left_plan();
}
else
{
	run_right_plan();
}

Remember Waypoint Alpha and Bravo?

Back in Project 4 you wrote two nearly identical programs — one for Mission 1 on the left, one for Mission 10 on the right. Now they can be one program that asks which side it is on.

The && means “and” — keep waiting while neither button is pressed. You do not need to master it today; copy the pattern and it works.

Step 4 — Did I actually grab it?

This is the fix for what you saw in Try It. Put a touch sensor where a held cube presses it, or check whether the claw closed further than it should have.

grab();

if (digital(claw_switch) == 1)
{
	printf("Got it\n");
	carry_to_basket();
}
else
{
	printf("Missed - backing up to retry\n");
	drive_backward(300);
	grab();
}

Now sabotage it again — move the pom aside like you did in Try It.

What did the robot do differently this time?

Step 5 — A three-way choice

Use else if to pick between three actions based on how far along the robot is.

if (gmpc(left) < 1000)
{
	printf("Barely started\n");
}
else if (gmpc(left) < 3000)
{
	printf("About halfway\n");
}
else
{
	printf("Nearly there\n");
}

Swap the first two conditions around and run it again. What goes wrong, and why?

Step 6 — Mission 3 Advanced — two opposite stacks for 13 points

Mission 3

Mixed Freight

Watch Mission 3 video

Base Mission

7 points Live judged

Create one stack consisting of a Green Cube a Yellow Cube, or a Yellow Cube a Green Cube.

Bonus Mission

9 points Live judged

Create a second stack consisting of a Green Cube and a Yellow Cube.

Advanced Bonus

13 points Live judged

Create two stacks such that one contains a Green Cube a Yellow Cube, and one contains a Yellow Cube a Green Cube (opposite color arrangements).

Scores

  • Green on Yellow or Yellow on Green — both satisfy the Base Mission.
  • Two valid mixed-color stacks satisfy the Bonus Mission.
  • Two stacks with opposite color arrangements satisfy the Advanced Bonus.

Does Not Score

  • Green on Green or Yellow on Yellow — same-color stacks do not score.
  • A single three-cube stack does not count as two scoring stacks.
  • Any cube used in one stack may not be reused for another stack.

Going for Advanced Gets You the Bonus Too

Two opposite stacks are two stacks. Score the Advanced Bonus and the 9-point Bonus comes with it — 22 points from this one run.

⚠ Four Cubes, Four Jobs

Each cube may contribute to only one scoring stack. You need two greens and two yellows, and every one of them has a specific place to be. Get the second stack’s colours the wrong way round and you drop from 22 points to 9.

Where the decisions go. This run has four grabs and four placements in a row. If grab two fails and the robot does not notice, everything after it is wasted. Put a check after each grab — the pattern from step 4 — so a miss costs you one cube instead of the whole mission.

StepCubeGoes whereCheck after?
1
2
3
4

Step 7 — Run it five times

RunTwo stacks?Opposite arrangement?Did a check catch a miss?Points
1
2
3
4
5

Score It — Checkpoint

My score

Mission partScored?Points
Mission 3 — Bonus (a second mixed stack)9
Mission 3 — Advanced (opposite arrangements)13
My total this project22

if or while?

For each one, say which you would use and why.

I want the robot to…if or while?Why
Drive forward until it bumps a wall
Check whether the grab worked, then move on
Wait for someone to press a button
Pick which starting box plan to run

Spot the bug

CodeWhat goes wrong
if (digital(0) == 1);
if (digital(0) = 1)
else with a condition in parentheses
Catch-all else if placed first in the chain

Can you do it again?

Think about it

In Try It your robot carried nothing across the field and placed it carefully in a basket. Would you call that a mistake by the robot, or a mistake by the programmer?

A robot that checks its own work looks smarter than one that does not. Is it actually smarter, or is it something else?

Every decision you made today came from a switch — pressed or not pressed. What kinds of choices could your robot make if it could tell how much of something there is, instead of just yes or no?

Next

Everything your robot senses right now is on or off. The field is not like that — a black line and a white mat are not two states of a switch, they are two brightnesses.

In Project 14 — Seeing Light and Dark, your robot gets a sensor that answers with a number instead of a yes.

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