Base Mission
7 points Live judgedCreate one stack consisting of a Green Cube a Yellow Cube, or a Yellow Cube a Green Cube.
Discovery · Coding Project 13
Until now your robot has followed orders. Today it starts choosing.
Student PIN:
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.
| Question | What 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 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 |
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.
while — Keeps askingwhile (digital(bump) == 0)
{
motor(left, 50);
motor(right, 50);
}Checks, runs the block, checks again, runs again… until the answer changes.
if — Asks onceif (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
Swap one for the other by accident and your robot either freezes in place or blows straight past a check it needed to make.
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 / elseif (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 / elseif (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.
Order Changes the Answer
In an else if chain the robot stops at the first true condition. So put the most specific check first and the catch-all last — or your specific case will never get a look in.
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.
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.
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.
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.
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?
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?
Mission 3 Create one stack consisting of a Green Cube a Yellow Cube, or a Yellow Cube a Green Cube. Create a second stack consisting of a Green Cube and a Yellow Cube. 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).Mixed Freight
Watch Mission 3 video
Base Mission
7 points
Live judgedBonus Mission
9 points
Live judgedAdvanced Bonus
13 points
Live judgedScores
Does Not Score
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.
| Step | Cube | Goes where | Check after? |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 |
| Run | Two stacks? | Opposite arrangement? | Did a check catch a miss? | Points |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 |
| Mission part | Scored? | Points |
|---|---|---|
| Mission 3 — Bonus (a second mixed stack) | 9 | |
| Mission 3 — Advanced (opposite arrangements) | 13 | |
| My total this project | 22 |
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 |
| Code | What 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 |
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?
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