We already know how to store data in variables. But storing data is of little use if we don’t do anything with it.
The power of a computer (and your Arduino is a tiny one) lies in its ability to calculate things very, very fast.
Calculating isn’t just adding numbers; it’s also comparing things (“Is this greater than that?”) and making logical decisions (“If this happens AND that”).
For this we use operators. Let’s teach our Arduino to add, subtract, and think.
Assignment (=)
In programming, the = sign is called the Assignment Operator. It means “put this value inside this variable”.
int numero = 5;
It means: Take the value 5 and store it inside the box called numero.
Basic Arithmetic
Arduino knows how to do basic math with int and float variables. The symbols are almost what you’d expect, with a couple of changes:
| Operation | Symbol | Example | Result if a=10, b=2 |
|---|---|---|---|
| Addition | + | int suma = a + b; | 12 |
| Subtraction | - | int resta = a - b; | 8 |
| Multiplication | * | int mult = a * b; | 20 |
| Division | / | int div = a / b; | 5 |
| Remainder (Modulo) | % | int resto = a % b; | 0 |
The Danger of Division
Be careful with division. If you are using integers (int), Arduino cannot use decimals (because you are using integers, of course).
int a = 5;
int b = 2;
int resultado = a / b; // What's the result?
In real life it’s 2.5. In Arduino, since the resultado box is an integer, it cuts off and discards the decimals. The result will be 2.
If you need decimals, remember to use variables of type float for everything.
The Shortcuts ++, +=
There are some “shortcuts” for the most common operations. For example, imagine you want to count laps. The normal way would be: vueltas = vueltas + 1; (Take what vueltas was worth, add 1, and store it again).
In Arduino you’ll see this written like this:
vueltas++;Adds 1 to the variable. (Increment).vueltas--;Subtracts 1 from the variable. (Decrement).vueltas += 5;Adds 5 to what was already there. (It’s the same asvueltas = vueltas + 5).
You’ll see these symbols everywhere, especially in for loops.
Comparison Operators
Now that we know how to calculate, we want to ask. These operations don’t return a number, they return an answer of True (true) or False (false). They are the basis of the if statement.
| Question | Symbol | Example | Meaning |
|---|---|---|---|
| Equality | == | a == 5 | Is ‘a’ exactly 5? |
| Inequality | != | a != 5 | Is ‘a’ different from 5? |
| Less than | < | a < 10 | Is ‘a’ smaller than 10? |
| Greater than | > | a > 10 | Is ‘a’ larger than 10? |
| Less than or equal | <= | a <= 10 | Is it 10 or less? |
| Greater than or equal | >= | a >= 10 | Is it 10 or more? |
Look at the first one. To ask if it’s equal we use DOUBLE EQUALS (==).
if (a = 5)WRONG. You are storing a 5 in ‘a’ and Arduino gets confused.if (a == 5)CORRECT. You are comparing.
Logical Operators
Sometimes one question isn’t enough. We want to ask things like
Is it daytime AND am I sleepy?
For that, we combine comparisons with logic.
&& means “AND”. For it to be true, both things must be true.
(A && B)
Example: “Turn on alarm if the sensor detects motion
&&the alarm is armed”.
|| means “OR”. For it to be true, it’s enough for one of the two to be true. (The vertical bar | is usually on the 1 key or below the Esc key).
(A || B)
Example: “Turn on light if I press switch 1
||I press switch 2”.
! means “NOT”. It inverts the result. What was true becomes false, and vice versa.
(!A)
Example:
!trueisfalse. Very useful for switches:estado = !estado;(Inverts the light: if it was ON it becomes OFF).
Practical Example: A Counter with a Limit
Let’s put it all together in a real example. We want a variable that counts from 0 to 10. When it reaches 10, it should go back to 0.
int contador = 0; // We use Assignment (=)
void setup() {
Serial.begin(9600);
}
void loop() {
// 1. Arithmetic: Add 1
contador++;
// 2. Comparison: Have we gone too far?
if (contador > 10) {
// 3. Assignment: Reset
contador = 0;
Serial.println("¡Vuelta a empezar!");
}
// 4. Compound Logic: We only print if it's even AND greater than 5
// (The operator % 2 == 0 tells us if it's even)
if (contador > 5 && (contador % 2 == 0)) {
Serial.print("Numero par alto: ");
Serial.println(contador);
}
delay(500);
}
