KIPR · Botball Explorer
Activity Sections

Discovery · Coding Project 10

Feeling for Things

Project
Coding Project 10
Strand
Coding
Phase
Make It Reliable
Time
One class period
What You Are Doing
Mounting a touch , learning the loop that keeps checking it, and using contact to find a position your robot can trust.
Mission Anchor
Mission 13 — Straighten Up (advanced bonus) — 13 points
Before You Start
Project 9 — you need to be comfortable writing and using .
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 — Walking in the Dark

Imagine the power is out and you are trying to cross a room you know well. Your eyes are useless. So you put a hand out in front of you and start walking.

Actually do it. Stand a few steps from a wall, close your eyes, put one hand out, and walk slowly toward it until you touch it.

How many times did you check whether your hand had reached the wall?

You did not check once and hope. What were you doing the whole time you were walking?

That Is a Loop

Check. Step. Check. Step. Check — touched something — stop and do something different.

Repeating an action while you wait for something to become true is called looping, and it is the whole idea of this project.

Sort your sensors

Tip your kit’s sensors out on the table. Without looking anything up, sort them into two piles. You decide the rule.

My rule for splitting themWhat went in each pile
Pile 1
Pile 2
What rule did you use?

Learn It — Keep Checking Until

Digital sensors --- the long lever and large touch sensors.
A turns something physical — a touch, a brightness, a distance — into a number your program can read.

Digital — What you have today

A sensor is like a light switch. Two readings, nothing between:

  • 0: not touched
  • 1: touched

A touch sensor is a mechanical switch. Pressing it pushes two contacts together and completes a circuit.

Analog — Introduced in project 14

An sensor gives a whole range of numbers, from 0 to 4095. Light sensors and rangefinders work this way — “how bright,” not “bright or dark.”

You will meet these later. Today, everything is 0 or 1.

Why msleep cannot help you here

Every time you needed the robot to wait, you used msleep(). That will not work now.

The program cannot read a sensor while it is sleeping. If you tell it to sleep for two seconds and the sensor is pressed after half a second, the robot never notices.

You need it to keep checking — over and over, as fast as it can. That is what a loop is for.

Comparing two values

To check something, you compare two values. These are the comparisons you can use:

SymbolMeansExample
==is equal to5 == 4 is false
!=is not equal to5 != 4 is true
>is greater than5 > 4 is true
<is less than5 < 4 is false
>=is greater than or equal to5 >= 5 is true
<=is less than or equal to5 <= 5 is true

⚠ Two Equals Signs, Not One

One = means make this equal to that — it is what you used to set a in Project 9.

Two == means is this equal to that? — it is a question.

Use one where you meant two and your program may still and run, then do something wrong. This is one of the hardest bugs to find, so check it every time.

The while loop

Say it in plain English before you say it in C:

while it is raining
{
    my umbrella is open and over my head
}

close the umbrella

The umbrella stays up the whole time the is true. The moment it stops raining, you fall out of the and close it.

Code / partWhat it means
while (condition)Ask the question. No here — the braces do that job, same as int main ().
{ ... }Everything inside runs, over and over, as long as the answer is true.
after the }The moment the answer is false, the robot jumps down here.

Reading a touch sensor

digital(0) reads the digital sensor plugged into 0. It gives you a 0 or a 1.

// While not touched
while (digital(0) == 0)
{
	// Keep driving
	motor(0, 100);
	motor(3, 100);
	// Tiny pause to avoid overworking the controller
	msleep(10);
}

motor(0, 0);
motor(3, 0);
// Touched: Stop
msleep(30);

Read It Out Loud

“While the sensor in port 0 reads zero — while nothing is it — keep both motors running. As soon as it reads one, stop.”

Notice the loop condition is checking for 0, not 1. You keep going while it is not pressed. Students often mix this up.

⚠ No msleep, No ao Inside the Loop

Do not put a long msleep() in the loop — it stops the checking. Do not put ao() in the loop either, or the robot will start and stop over and over instead of driving.

The ao() goes after the closing brace. That is the whole point: the loop is the driving, and what comes after is what happens when the condition finally changes.

Do It — Drive Until Bump

Step 1 — Mount the sensor and read it live

Mount your touch sensor on the front of the robot so it hits things before anything else does. Plug it into a digital port and write down which one.

On the Wombat, open the sensor list. Press the sensor with your finger and watch the number.

The sensor list on the Wombat.
QuestionMy answer
Which digital port is my touch sensor in?
What does it read when nothing touches it?
What does it read when I press it?

Step 2 — Drive Until Bump — in your hands first

⚠ Hold the Robot in the Air for the First Run

Do not put this on a table or the field yet. Hold the robot up so the wheels spin freely, run the program, then press the sensor with your finger and watch the wheels stop.

A loop that never sees its sensor never stops. Find that out in your hands, not off the edge of a table.

Create a new project called Drive Until Bump. Plan it as first, then write it.

// 1. Print a message so I know it started
// 2. While the sensor is NOT pressed, drive forward
// 3. When it is pressed, fall out of the loop
// 4. Stop

Step 3 — Put your variables back in

You spent Project 9 learning why bare numbers are a bad idea. Do not abandon that now.

int left  = 0;
int right = 3;
// Front touch sensor
int bump  = 0;

while (digital(bump) == 0)
{
	motor(left, 100);
	motor(right, 100);
	msleep(10);
}
motor(0, 0);
motor(3, 0);
msleep(30);

Note that bump and left both hold 0 — but they mean completely different things. One is a digital port, one is a motor port. The names are what keep them straight.

Step 4 — Flip the condition and predict

Change == 0 to == 1. Before you run it, write down what you think will happen.

My prediction:

Now run it. Then run it again while holding the sensor pressed the whole time.

What actually happened, both times?

Explain why, using the words “true” and “false”.

Put it back to == 0.

Step 5 — Bump and Go Home

Here is what makes a touch sensor genuinely useful: a wall is a place your robot can always find.

New project. Drive backward until the sensor presses against the starting box wall, then drive forward a set distance into the box and stop.

// 1. Drive backward while the sensor is not pressed
// 2. Exit the loop when it touches the wall
// 3. Stop and settle
// 4. Drive forward a set distance
// 5. Stop

Why This Matters More Than It Looks

No matter where the robot wandered off to, it can come back and touch a wall — and now it knows exactly where it is. Every measurement after that starts from a spot it can trust.

Step 6 — Add a stop and settle

A robot that has been driving is still moving when the loop ends. Momentum does not care about your program.

Put a proper stop between the loop and whatever comes next:

motor(0, 0);
motor(3, 0);
msleep(30);
// Let the robot actually stop moving
msleep(500);

Run it with and without the pause and compare.

What difference did the pause make?

Step 7 — Wait for a button before starting

The Wombat has one physical push button and three soft buttons on screen, named a, b, and c. Each has a that returns 1 when pressed and 0 when not.

printf("Press A to start\n");

while (a_button() == 0)
{
	// Wait: do nothing at all
	msleep(10);
}

// Run the mission

Add this to the top of a program. Now your robot waits for you instead of driving off the second it compiles.

This loop has nothing inside it but a tiny pause. Is it still doing something? What?

Step 8 — Mission 13 Advanced — all three cubes for 13 points

Watch Mission 13 video

Base Mission

1 point Live judged

All three Unstraight Cubes satisfy the definition (cubes are the black line).

Bonus Mission

7 points Live judged

Two Unstraight Cubes form a scoring stack — one Unstraight Cube is another.

Advanced Bonus

13 points Final judged

All three Unstraight Cubes form a scoring stack. All cubes must originate from the Unstraight Cube area.

Scores

  • All three Unstraight Cubes are the black line.
  • One Unstraight Cube is another Unstraight Cube.
  • All three Unstraight Cubes participate in valid relationships (traditional stack or pyramid).

Does Not Score

  • One or more Unstraight Cubes are touching the black line.
  • Two Unstraight Cubes touching side-by-side without .
  • An Unstraight Cube stacked with a cube from another mission area.
  • Only two Unstraight Cubes participate in the Advanced Bonus stack.

A Pyramid Counts --- and It Is Easier

Read the scoring examples again. A pyramid scores exactly the same as a three-high tower. Two cubes on the bottom, one on top.

This is a final-position mission, so whatever you build has to still be standing when the match ends. A pyramid is far harder to knock over than a tower. Take the easy 13 points.

Use the touch sensor to make your approach repeatable. If your robot can find a wall or a cube by contact instead of by timing, every placement after that starts from a known spot.

Where in your run does the touch sensor help most — finding the cubes, or finding your way back?

Step 9 — Run it five times

RunAll three stacked?Still standing at the end?Points
1
2
3
4
5

Did the stack fall over on any run? What knocked it — the robot leaving, or the stack itself?

Score It — Checkpoint

My score

Mission partScored?Points
Mission 13 — Advanced Bonus (all three cubes stacked)13
My total13
With Project 6 and Project 8, Mission 13 is now worth 21 points across all three parts — the most any single mission has given you.

Finish the loop

Write the condition for each one.

I want the robot to…The condition is
Drive while the sensor in port 0 is not pressed
Drive while the sensor in port 1 is pressed
Wait until someone presses button A

Spot the bug

Each of these is wrong. Say what happens when you run it.

CodeWhat goes wrong
while (digital(0) = 0)
while (digital(0) == 0);
ao(); written inside the loop
msleep(2000); inside the loop

Can you do it again?

Think about it

Since Project 3, every distance your robot has driven was really a length of time. What does a touch sensor give you that a stopwatch never could?

A touch sensor only tells you something when you are already touching it. Name one thing on the field you would want to stop at, where bumping into it first would be a bad idea.

Your loop asks a question over and over. So far the question has always been about a sensor. What else could a robot count or measure that a loop could watch?

Next

A wall tells you where you are, but only if there is a wall. Most of the field has nothing to bump into.

In Project 11 — Counting Wheel , you point the same loop at a number instead of a switch — and your robot can finally measure how far it has actually gone.

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