← All articles

1 November 2026

Variables and Constants: How Does a Robot 'Remember' Numbers?

Illustration of variables and constants: labelled boxes holding a robot's numbers

Ask your child one simple question: can a robot remember anything? How does an obstacle-avoiding robot know it just turned left three times? How does a toy car remember how fast it is going? How does a plant watering system remember which moisture level means "dry soil"?

The answer sits in two of the most basic ideas in programming: variables and constants. Once your child understands these two ideas, they will understand how a robot "remembers" — and for the first time they will be able to write code in which the machine truly remembers something.

What is a variable? A labelled box

Imagine a variable as a small box with a label stuck on it. The label says a name, for example "speed". Inside the box is a number, for example 100. At any moment the code can open the box to look at the number inside, or throw the old number out and put a new one in.

That is the whole idea. A variable has a name and a value — and the value can change:

Children grasp variables quickly when you tie them to something familiar: a toy box with a name on it, a drawer for loose change, a scoreboard showing goals scored. The box stays the same box — only what is inside changes.

What is a constant? A number that never changes

A constant is also a labelled box — but once a number is put in, the box is locked, and the number can never be replaced.

For example: the ultrasonic sensor is plugged into pin 9 of the Arduino board. That pin does not change during the project — today it is 9, tomorrow it is still 9. The safe distance of 20 cm — under 20 cm means danger — is a rule that does not change. The speed of sound in air, used to work out distances, is also a fixed number.

Why lock these numbers away? Two reasons:

How do variables and constants differ?

In short: a variable is something that changes while the program runs; a constant is something fixed from start to finish.

One handy trick for parents explaining this to a child: ask "does this change while the robot is running?" If the answer is yes → use a variable. If the answer is no → use a constant. That one question is enough to tell them apart in almost every first project.

Try it together: counting button presses

The first project needs only one button and one LED — a browser simulation is enough, nothing to buy.

Step 1: Count with a variable. Create a variable called "press_count", starting at 0. Each time your child presses the button, add 1. Print the count on the screen. Your child will watch the number climb with every press — the first time a machine "remembers" something they just did.

Step 2: Use a variable to change speed. Create a "speed" variable and two buttons: one raises the speed, the other lowers it. The light blinks according to the speed variable — press more and the light blinks faster, press less and it blinks slower. Your child will see that a variable is not only for counting, but for controlling how a machine behaves.

Step 3: Set a limit with a constant. Add a constant "SAFE_DISTANCE = 20" and a distance sensor. When the distance is smaller than the limit, a red light turns on. Then try changing the constant to 30 and watch the robot become much more cautious — one number changed, and the whole behaviour changed with it.

Step 4: Combine variables and constants inside a loop. This step brings together three ideas already learned: the loop measures distance again and again, stores it in a variable, compares it with the constant limit, and decides what to do. Your child has just built the skeleton of every robot program — and they wrote it themselves.

Common mistakes — and how to fix them together

Using a variable before putting a number in it. Create a "speed" variable but forget to give it a starting value, and the machine will use some random leftover number from nowhere. The fix: teach your child the habit of always giving a value when creating a variable — "name the box and put a number in it straight away".

Two variables with the same name. Give two boxes the same "speed" name for two different jobs, and the machine will mix them up. The fix: use clear and different names, for example "car_speed" and "blink_speed" instead of calling both "speed".

Misspelling a variable name. Write "speed" in one place and "sped" in another, and the machine complains because it thinks they are two different variables. The fix: show your child this mistake is so common everyone makes it, and the way to avoid it is short names with no accents and no spaces.

Trying to change a constant. Your child tries to change a constant's value while the program runs, and the machine reports an error — but that is not a bug, it is the machine doing its job: a constant exists so it cannot change. The fix: explain that this error is the machine protecting your child from accidentally changing an important rule.

Forgetting to update a variable inside a loop. The loop measures the distance once and then uses that same old number forever, so the robot reacts to stale data. The fix: check whether each repeat measures again and stores the fresh number in the variable.

When something goes wrong, let your child say where they think the fault is before you suggest anything. The skill of guessing a fault and then checking it — called debugging — is the skill that follows your child through all of robotics.

Upgrades once the basic version works

Once your child can count button presses and use a safe-distance limit, there are several directions to grow:

Each upgrade adds only one new idea. Do not rush them all into one session — one session, one new idea is the pace that lasts.

From variables and constants to real robots

Do not dismiss two ideas that are "just boxes holding numbers". Inside every robot, variables and constants work together with the loop: the loop measures fresh data, a variable stores it, a constant says whether that data is safe or dangerous, and the code decides what to do. Four ideas — loops, variables, constants, conditions — join into the skeleton of every robot program.

Once your child understands variables and constants, they know how to give a robot a memory. The next time they see a robot stop exactly in time before an obstacle, they will know: a variable is holding the distance just measured, a constant is holding the safe limit, and one line of code is comparing the two numbers. A robot is not mysteriously clever — it just remembers numbers, compares numbers, and follows the rules your child set.

Start today, no kit needed

Your child can try variables and constants with a button and an LED today on ManuStem — a browser-based robotics learning platform, no kit required, for ages 8 and up, with 28 lessons including 8 Component Lab lessons and 20 Competition Robot lessons, 4 of them free to try. The built-in AI co-pilot helps your child wire the circuit, write the code and debug it step by step, and the 3D robot preview shows them their creation before it runs. Start at stem.manusignal.com — in Vietnamese or English, whichever your child prefers.

Read the Vietnamese version:

Biến và hằng số: robot 'nhớ' số liệu như thế nào?