KIPR · Botball Explorer
Activity Sections

Discovery · Coding Project 17

Repeating Without Retyping

The last loop — and the last thirty-eight points on the field.

Project
Coding Project 17
Strand
Coding
Phase
Clean It Up
Time
One class period
What You Are Doing
Learning the loop that counts, then using it to collect every bonus that just means “now do that again.”
Mission Anchor
Mission 7 · 11 · 16 · 17 bonuses — 38 points
Before You Start
Project 16 — your should live in your by now.
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 — Thirty Small Hops

Back in Project 9 you moved a in three big jumps and were asked what it would take to move it in thirty small ones. Here is the answer you were promised.

#include <kipr/wombat.h>

int main ()
{
	int arm = 0;
	int position = 200;

	enable_servos();
	while (position < 1800)
	{
		position = position + 10;
		set_servo_position(arm, position);
		msleep(5);
	}
	disable_servos();
	return 0;
}

Read it with a partner before you run it, and answer these from the code alone.

QuestionMy answer
What kind of loop is this?
Where does position start?
What makes it stop?
How much does it change each time round?
So how many times does the loop run?
Now run it with your own arm positions and watch.

How is this different from set_servo_position(arm, 1800); on its own?

A Smooth Arm Is a Careful Arm

An arm that snaps to a position throws whatever it is holding. An arm that creeps there sets things down gently — which matters for every final-position mission you have.

Where the counting lives

Look at that loop again. The three things that control the counting are in three different places:

  • The start is on line 6, above the loop.
  • The stopping point is on line 9, in the .
  • The step is on line 11, buried inside the .
If a teammate asked you “how many times does that run?”, how long would it take you to work it out?

Learn It — All Three on One Line

A does exactly the same job, but it puts the start, the stop, and the step together where you can see all three at once.

for (position = 200; position < 1800; position++)
      ↑              ↑                  ↑
    start          stop               step
Code / partWhat it means
position = 200Where the counting begins. Runs once, before anything else.
position < 1800Checked before every trip through the block. The moment it is false, the loop ends.
position++Runs at the end of every trip. ++ means add one — the shorthand you met in Project 9.

Two inside the parentheses, and none after the closing one. Same rule as while and if — the braces finish the .

The same loop, both ways

while

int position = 200;

while (position < 1800)
{
	set_servo_position(arm, position);
	msleep(5);
	position++;
}

Three pieces in three places. Forget the last one and it never ends.

for

int position;

for (position = 200; position < 1800; position++)
{
	set_servo_position(arm, position);
	msleep(5);
}

All three in one place. Hard to forget the step when it is right there.

Which one, when?

Use while when…Use for when…
You do not know how many times — it depends on a .You know the count before you start. Four corners. Two poms.
There is more than one way out of the loop.You are counting up or down by a steady amount.
Nothing needs to change by a set step each time.You want the whole plan readable on one line.

Counting repeats, not values

The counter does not have to control anything. It can just count how many times you have gone round.

int count;

for (count = 0; count < 4; count++)
{
	drive_forward(4000);
	turn_right();
}

That is a square. Four sides, four corners, four lines of code — and to make it a pentagon you change one number.

⚠ Starting at 0 Means Stopping Before the Count

count = 0; count < 4 gives you 0, 1, 2, 3 — that is four trips, not three, and not five.

Counting from zero again, same as the motor back in Project 3. Write out the numbers if you are not sure.

Do It — Do It Again, Automatically

Step 1 — Convert the smoother

Take the while version from Try It and rewrite it as a for loop. The arm should move identically.

Change the step from position++ to position = position + 40. What happened to the movement?

Step 2 — A square in four lines

The square, for the last time. Use a for loop and your library .

VersionLines in mainChange it to a pentagon by…
Project 11 — ticks, written out
Project 12 — functions
Now — a for loop

Step 3 — Mission 11 Bonus — two orange poms for 7 points

Mission 11

Hazard Disposal

Watch Mission 11 video

Base Mission

7 points Final judged

One Orange Pom is a basket.

Bonus Mission

7 points Final judged

Two or more Orange Poms are the same basket.

Scores

  • One Orange Pom is a basket.
  • One Orange Pom partially extends into the interior space of a basket.
  • Two or more Orange Poms are the same basket.

Does Not Score

  • An Orange Pom is touching only the outside of a basket.
  • An Orange Pom is resting above a basket without extending into it.
  • One Orange Pom is in Basket A and one is in Basket B — does not satisfy Bonus.
Start here — it is the simplest “do it twice” on the field, and you already have the function.

Two Ways, Both Fine

Call your function twice, or wrap it in a two-iteration for loop. Try both and see which reads better to you.

The loop wins the moment the number might change — and at a tournament it always might.

The second pom is somewhere different, so your function needs an or a short drive between trips. Same problem you solved for Mission 15 in Project 12.

Step 4 — Missions 16 and 17 Bonuses — stack them higher for 22 points

⚠ Final Position --- Both Have to Survive

Delivering the second cube is where the first one gets knocked off. Approach the same way each trip, and back straight away rather than turning while you are still close.

Did the second delivery ever knock the first one off? What fixed it?

Step 5 — Mission 7 Bonus — a second enclosure · 9 pts

Watch Mission 7 video

Base Mission

11 points Final judged

One Blue Pom and one Orange Pom are the same PVC enclosure.

Bonus Mission

9 points Final judged

A second PVC enclosure contains at least one Blue Pom and at least one Orange Pom, each the enclosure. The Base and Bonus must use different enclosures.

Scores

  • One Blue Pom and one Orange Pom are the same PVC enclosure.
  • Multiple Blue Poms and one Orange Pom are the same PVC enclosure.
  • Two different enclosures each containing at least one Blue and one Orange Pom.

Does Not Score

  • A Blue Pom in one enclosure and an Orange Pom in a different enclosure.
  • Only one pom type in any enclosure.
  • The same enclosure used to satisfy both Base and Bonus Missions.
This is the same delivery as the base, aimed somewhere else — which is exactly the case for a function with an argument called twice, or a loop over two destinations.

Step 6 — Put the loops in your library

Anything you wrapped in a loop that worked — deliver_poms(int howMany), smooth_arm(int from, int to) — belongs in your library file. Add it, save, and note it in your block.

Step 7 — Run it five times

RunM11M16M17M7Points
1
2
3
4
5

Score It — Checkpoint

My score

Mission partScored?Points
Mission 11 — Bonus (two orange poms, same basket)7
Mission 16 — Bonus (two spilled cubes on the green)11
Mission 17 — Bonus (two unstraight cubes on the brown)11
Mission 7 — Bonus (a second enclosure)9
My total this project38

Which loop?

The robot needs to…while or for?
Drive until it touches a wall
Put three cubes on a
Follow a line until it has gone 5000 ticks
Lower the arm in small steps from 200 to 1800
Wait for someone to press a button

How many times?

The loopRuns how many times?
for (i = 0; i < 5; i++)
for (i = 1; i <= 5; i++)
for (i = 0; i < 10; i = i + 2)
for (i = 10; i > 0; i--)

Can you do it again?

Every point on the field

Add up what you scored across the whole strand. The right-hand column is what was available.

ProjectI scoredAvailable
4 — Out and Back4
6 — Bulldoze Run16
7 — Your Robot’s Arm7
8 — Arm and Claw Together37
9 — Names for Your Numbers20
10 — Feeling for Things13
11 — Counting Wheel Ticks40
12 — Teaching Your Robot New Moves18
13 — Deciding What to Do22
14 — Seeing Light and Dark35
15 — Following the Line51
17 — Repeating Without Retyping38
Total301

301 Is the Whole Game

Add up every base, bonus, and advanced bonus across all eighteen Stack Attack missions and the number is 301.

There is nothing on that field you have not been taught how to reach.

Think about it

You have written four kinds of loop condition now — a switch, a tick count, a brightness, and a plain count. Which was hardest to get right, and why that one?

In Project 1 you ran a program somebody else wrote and did not understand a line of it. Go back and read that program now. What does it say to you?

Your robot does exactly what it is told, every time, and has no idea what any of it means. After seventeen projects, does it seem intelligent to you? Say what makes you answer that way.

That Is the Coding Strand

Seventeen projects ago your robot could not be switched on. It now drives measured distances, turns to an angle, grabs and stacks and places, feels for walls, counts its own wheels, reads light and dark, follows a line, checks its own work, and decides what to do when something goes wrong.

All of it built from about a dozen commands and four ideas: do this, do it if, do it while, do it again.

Take your library with you. Next season the field will be different and most of what is in that file will still work.

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