KIPR · Botball Explorer
Activity Sections

Discovery · Coding Project 11

Counting Wheel Ticks

Same loop as last time — but now it watches a number instead of a switch.

Project
Coding Project 11
Strand
Coding
Phase
Make It Reliable
Time
One class period
What You Are Doing
Reading the counter built into every motor, driving exact distances instead of guessing with time, and delivering freight to the loading dock.
Mission Anchor
Mission 9 bonus · Mission 8 · Mission 18 base — 40 points
Before You Start
Project 10 — you must be comfortable writing a while loop and know where ao() goes.
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 — The Counter Was Always There

Back in Project 3 you dragged the slider on the Motors screen and noticed a number changing next to each . You were told to remember it. Here it is.

Open the Motors screen on the Wombat. Do not run anything — just turn a wheel with your hand and watch.

The motor widget on the Wombat showing the motor counter.
QuestionWhat I saw
What happens to the number when I roll the wheel forward?
What happens when I roll it backward?
What happens if I roll it backward past where I started?

Every Motor Has Been Counting This Whole Time

Since Project 3, your motors have been keeping track of exactly how far they turned. You just didn’t know how to ask them.

Find your ticks per inch

are the motor’s own unit. To use them you need to know how many make an inch on your robot.

  • Put the robot on the floor and mark where the front edge sits.
  • Clear the counter on the Motors screen.
  • Push the robot straight forward exactly 24 inches. Slowly, and in a straight line.
  • Read the counter.

TryTicks for 24 inchesTicks per inch (divide by 24)
1
2
3
Average

⚠ Measure Long, Not Short

Use 24 inches, not 3. A small error in a short push turns into a big error per inch. Anything under about a foot is not worth measuring.

Your three numbers are not identical. Why not — and which one should you use?

Learn It — A Number Instead of a Switch

A tick is one small step of the motor shaft. There are about 1820 ticks in one full revolution of the shaft.

Two commands

Code / partWhat it means
clear_motor_position_counter(0);Sets the counter for port 0 back to zero. Short name: cmpc(0).
get_motor_position_counter(0);Gives you the number of ticks that motor has turned. Short name: gmpc(0).

Both names do the same thing. The short ones are quicker to type and you will see both in other people’s code.

The same loop you already know

Look at these two side by side. Only the changed.

Project 10 — a switch

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

Project 11 — a number

while (gmpc(left) < 4000)
{
	motor(left, 50);
	motor(right, 50);
}
motor(0, 0);
motor(3, 0);
msleep(30);

Read the new one out loud: “While the left motor has turned fewer than 4000 ticks, keep driving. The moment it reaches 4000, stop.”

Why This Is Better Than a Wall

A touch only helps where there is something to touch. A tick counter works anywhere on the field — open floor, mid-turn, anywhere. Your robot finally has a way to know how far it has gone without hitting something.

Always clear before you count:

// Start counting from zero
cmpc(left);

while (gmpc(left) < 4000)
{
	motor(left, 50);
	motor(right, 50);
}

motor(0, 0);
motor(3, 0);
msleep(30);
// Let it settle
msleep(500);

It will go too far. Every time.

Run that program with a target of 4000 and then check the Motors screen. You will not see 4000. You will see something like 4310.

Nothing is broken. Your robot has — it is moving, and when the loop shuts the motors off it keeps coasting.

Going backward makes the number go down

Driving in reverse decreases the counter, and it will go negative. That gives you two ways to come home.

Do not clear — count back to 0

cmpc(left);
// Drive Out
while (gmpc(left) < 4000)
{
	motor(0, 0);
	motor(3, 0);
	msleep(500);
}

// Drive Home
while (gmpc(left) > 0)
{
	motor(0, 0);
	motor(3, 0);
	msleep(30);
}

Clear again — count to −4000

cmpc(left);
// Drive Out
while (gmpc(left) < 4000)
{
	motor(0, 0);
	motor(3, 0);
	msleep(500);
}

cmpc(left);
// Drive Home
while (gmpc(left) > -4000)
{
	motor(0, 0);
	motor(3, 0);
	msleep(30);
}

Both work. The first one always returns to where it started, no matter how far out it went. The second one always travels the same distance back. Those are not the same thing, and one day the difference will matter.

⚠ Get the Direction Right or the Loop Never Ends

Driving backward with while (gmpc(left) < 4000) is a loop that can never finish — the number is going down, away from 4000, forever.

Going forward, count up with <. Going backward, count down with >. Check this before every run.

Do It — Drive by the Numbers

Step 1 — Measure your overshoot

Create a new project called Ticks. Clear, drive to 4000 ticks, stop. Then check the Motors screen for what you actually got.

PowerAsked forActually gotOvershoot
304000
504000
1004000

What is the relationship between power and overshoot?

Which power will you use for the rest of this project, and why?

Step 2 — Correct for it

Subtract your overshoot from your target and run again. Keep adjusting until the robot lands on 4000.

TryTarget in my codeWhere it actually stopped
1
2
3

Step 3 — Drive a real distance

Use your ticks-per-inch from Try It. Work out the ticks for each distance, then test it with a ruler.

I wantTicks (inches × TPI)How far it really went
6 inches
12 inches
24 inches

Which distance was least accurate? Why do you think that is?

Step 4 — Out and back

Drive out 4000 ticks, stop and settle, then return to exactly where you started. Use whichever of the two methods from Learn It you prefer.

Which method did you use, and why that one?

Compare this to Project 4, where you did the same thing with a stopwatch. Notice how much less tuning it took.

Step 5 — Turn by ticks

Create a new project called Right Turn. — one wheel forward, one back, same speed — but this time stop it by counting ticks instead of by time.

cmpc(left);

while (gmpc(left) < turnTicks)
{
	motor(left, 50);
	motor(right, -50);
}
motor(0, 0);
motor(3, 0);
msleep(30);
TryTicksToo far, not far enough, or right?
1
2
3
4

Step 6 — The square, again

In Project 5 you drove a square with four timed turns and it did not close. Do it again — same square, but every distance and every turn counted in ticks.

QuestionProject 5 (time)Now (ticks)
How close to the starting spot?
Facing the same way?

This Is the Whole Point of the Project

Same robot. Same square. The only thing that changed is what the robot was measuring — and suddenly it can come back to where it started.

Your timed square drifted a little more with every corner. Why does a tick-counted square not do that?

Step 7 — Mission 9 Bonus — get Botguy out for 9 points

Watch Mission 9 video

Bonus Mission

9 points Live judged

Botguy is outside the enclosure AND is touching the warehouse floor.

Scores

  • A robot is touching Botguy.
  • Botguy is completely outside the enclosure and touching the warehouse floor.
  • Botguy is touching the warehouse floor while being held by a robot.

Does Not Score

  • A robot is near Botguy but not touching him.
  • Botguy is touching the warehouse floor but still touching the enclosure.
  • Botguy is removed from the enclosure but not touching the warehouse floor.
Your claw can still be holding him. He just has to be clear of the enclosure and down on the floor at the same instant.

Step 8 — Mission 18 Base — take him to the loading zone for 11 points

Watch Mission 18 video

Base Mission

11 points Final judged

Botguy is the Loading Zone.

Bonus Mission

13 points Final judged

Botguy is the Loading Zone AND at least one Traffic Cone is the Loading Zone.

Advanced Bonus

15 points Final judged

Botguy is the Loading Zone AND both Traffic Cones are the Loading Zone.

Scores

  • Botguy is the Loading Zone.
  • Botguy and one Traffic Cone are the Loading Zone.
  • Botguy and both Traffic Cones are the Loading Zone.

Does Not Score

  • Botguy is outside the Loading Zone.
  • Botguy is touching only the exterior boundary of the Loading Zone.
  • A Traffic Cone is the Loading Zone but Botguy is not.
  • Botguy and only one Traffic Cone are the Loading Zone for the Advanced Bonus.

One Trip, Two Missions, Twenty Points

You just lifted Botguy out of the enclosure for Mission 9. Do not put him down — carry him to the Loading Zone and Mission 18’s base scores too.

This is exactly the kind of thing to look for when you plan a full match: one action, more than one mission.

Mission 18 is final position, so Botguy has to still be when the match ends. Set him down properly and back straight away.

Roughly how many ticks is it from the enclosure to the loading zone?

Step 9 — Mission 8 — deliver the red cube for 20 points

Watch Mission 8 video

Base Mission

11 points Final judged

The pallet with the Large Red Cube is the Loading Dock.

Bonus Mission

9 points Final judged

One or more Small Red Cubes are the Large Red Cube while the pallet is on the Loading Dock.

Scores

  • The pallet and Large Red Cube are the Loading Dock.
  • One Small Red Cube is the Large Red Cube while on the Loading Dock.
  • Multiple Small Red Cubes are the Large Red Cube on the Loading Dock.

Does Not Score

  • The pallet is on the Loading Dock but the Large Red Cube has fallen off.
  • A Small Red Cube is on the Loading Dock but not the Large Red Cube.
  • The pallet is not touching the Loading Dock.
  • The Large Red Cube is on the Loading Dock without the pallet.

⚠ The Pallet and the Cube Travel Together

Read the failures carefully. The Large Red Cube on the Loading Dock without the pallet does not score. Neither does a pallet on the dock with the cube fallen off.

They both have to end up there, cube on pallet, pallet on dock.

Which was harder — driving to the dock, or getting the pallet onto it?

Step 10 — Run it five times

RunM9 bonusM18 baseM8 baseM8 bonusPoints
1
2
3
4
5

Score It — Checkpoint

My score

Mission partScored?Points
Mission 9 — Bonus (Botguy out and on the floor)9
Mission 18 — Base (Botguy in the Loading Zone)11
Mission 8 — Base (pallet + cube on the dock)11
Mission 8 — Bonus (small red cube on top up there)9
My total40

My tick card

These replace every timing number you have been carrying since Project 4.

MeasurementValue
Ticks per inch
Driving power I use
My overshoot at that power
Ticks for a 90° turn
Ticks for a 180° turn
Ticks from my starting box to the loading dock

Write the condition

I want the robot to…The condition is
Drive forward until it has turned 2500 ticks
Drive backward until it is back at zero
Drive forward 8 inches, using my ticks per inch

Can you do it again?

Think about it

Your battery is half flat, so the robot drives slower. What happens to a run built on msleep()? What happens to a run built on ticks?

Ticks tell you how far the wheels turned — not how far the robot moved. Name a situation where those two are different.

You have now written the drive-a-set-distance code and the turn-90° code several times each, in several projects. What would you rather do than keep retyping them?

Next

You have a robot that can measure. You also have a program that says the same twenty lines over and over.

In Project 12 — Teaching Your Robot New Moves, you give those twenty lines a name — and then you just say the name.

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