Introduction
Arithmetic on an Arduino looks like the arithmetic you already know. Five operators, and four of them behave exactly as expected.
The fifth is %, the remainder — sometimes called modulo. It answers "what is left over after dividing". It looks like an oddity and turns out to be one of the most useful operators in the language: it is how you make something happen every third time, how you keep a number inside a range, and how you split 197 seconds into 3 minutes and 17.
The trap is not any of the five. It is that whole numbers divide into whole numbers. 7 / 2 is 3, and the remainder is thrown away silently. You met that with types; here it starts to bite, because arithmetic is where it shows up.
Order matters too. 2 + 3 * 4 is 14, not 20, for the same reason it is in maths class. Brackets settle any argument, and cost nothing.
What you will be able to do
By the end of this lesson you can:
- Use
+,-,*,/and%on an Arduino. - Predict the result of integer division without running it.
- Use
%to wrap a number back into a range. - Use
%to do something every n-th time. - Apply the order of operations, and use brackets when it is unclear.
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. Read everything in the Serial Monitor at 9600 baud.
The five
| Operator | Name | a = 7, b = 2 |
|---|---|---|
+ | add | 9 |
- | subtract | 5 |
* | multiply | 14 |
/ | divide | 3 |
% | remainder | 1 |
int a = 7;
int b = 2;
Serial.println(a + b); // 9
Serial.println(a - b); // 5
Serial.println(a * b); // 14
Serial.println(a / b); // 3 not 3.5
Serial.println(a % b); // 1
7 / 2 is 3 with 1 left over. / gives you the 3, % gives you the 1.
% — the useful one
Wrapping a number back into a range. Any number % 6 is always 0 to 5:
Serial.println(17 % 6); // 5
Serial.println(18 % 6); // 0
Serial.println(19 % 6); // 1
The count climbs for ever and the answer keeps cycling 0,1,2,3,4,5. That is how a menu wraps from the last item back to the first.
Every n-th time. When a number divides exactly, the remainder is 0:
count % 10 // 0 every tenth time
count % 2 // 0 on even numbers, 1 on odd
Splitting a number up. 197 seconds is 3 minutes and 17 seconds:
int total = 197;
Serial.println(total / 60); // 3 minutes
Serial.println(total % 60); // 17 seconds
/ gives how many whole ones fit; % gives what is left. Together they take a number apart.
Order of operations
Multiply and divide happen before add and subtract, exactly as in maths.
Serial.println(2 + 3 * 4); // 14
Serial.println((2 + 3) * 4); // 20
% ranks with * and /.
Brackets cost nothing and remove all doubt. Use them whenever a line makes a reader pause.
Mixing whole and decimal
int count = 7;
float average = count / 2; // 3.0 — divided as whole numbers first
float better = count / 2.0; // 3.5
One decimal anywhere makes the whole calculation decimal. % does not work on floats at all — it is a whole-number operator.
Watch out for
7 / 2is 3. Every time.- Divide by zero does not crash an Arduino, but the result is meaningless. Check first.
%on a negative number keeps the sign:-7 % 3is-1, not2.%will not compile on afloat.- Overflow still applies.
30000 * 2in anintwraps negative.
Challenges
Challenge 1
Take a number apart.
A race lasted 3725 seconds. Print it as hours, minutes and seconds:
1 h 2 m 5 s
You need / and % only. Work out the hours first, then what is left over, then the minutes from that.
Then try it with 59 seconds and with 3600 seconds. Does your sketch still read correctly?
Log in to ask for the answer.
Challenge 2
Odd, even, and every fifth.
Count from 1 to 20, printing each number with a label:
1 odd
2 even
3 odd
...
You do not have loops yet, so use a counter variable and a delay(300) in loop().
Then add a second label so every fifth number also says TICK:
5 odd TICK
Hint: two different % questions about the same number.
Log in to ask for the answer.
Challenge 3
Predict, then check.
Write down your answer for each of these before running anything:
2 + 3 * 4
(2 + 3) * 4
10 / 3
10 % 3
10.0 / 3
7 % 7
0 % 5
-7 % 3
30000 * 2 // stored in an int
Now print them all. Mark yourself. For every one you got wrong, write one sentence on why.
The last two are the interesting ones.
Extra challenge
A till that gives change.
A customer pays RM 50.00 for something costing RM 13.70. Work out the change in notes and coins, and print it:
Change: RM 36.30
2 x RM10
1 x RM5
1 x RM1
1 x 20 sen
1 x 10 sen
Do the whole thing in sen, as whole numbers — 5000 and 1370 — and only divide by 100 for display. Decimals will let you down here: 0.1 + 0.2 is not exactly 0.3, and money must be exact.
Work down from the largest: 1000, 500, 100, 50, 20, 10, 5 sen. For each, / tells you how many, and % tells you what is left for the next one.
Think about it: this is the same / and % pair as the hours-and-minutes challenge, applied seven times over. Notice you wrote nearly the same three lines seven times. Hold that thought until arrays and loops arrive.
Log in to ask for the answer.