Arduino - Operators
Arduino - Programming Operators
Section titled “Arduino - Programming Operators”Operators are special symbols that instruct the compiler to perform specific mathematical, relational, or logical operations. Since Arduino programming is based on C++, it uses the standard C++ operators. This guide provides practical, runnable examples for each type.
To run these examples: Copy the code into the Arduino IDE, upload it to your board, and open the Serial Monitor (Tools > Serial Monitor) with the baud rate set to 9600.
1. Arithmetic Operators
Section titled “1. Arithmetic Operators”These are used for performing mathematical calculations.
| Operator | Name | Description |
|---|---|---|
| = | Assignment | Assigns the value on the right to the variable on the left. |
| + | Addition | Adds two operands. |
| - | Subtraction | Subtracts the second operand from the first. |
| * | Multiplication | Multiplies two operands. |
| / | Division | Divides the first operand by the second. |
| % | Modulo | Returns the remainder of an integer division. |
void setup() { Serial.begin(9600);
int a = 10; int b = 3;
Serial.print("a = 10, b = 3\n"); Serial.print("a + b = "); Serial.println(a + b); // Result: 13 Serial.print("a - b = "); Serial.println(a - b); // Result: 7 Serial.print("a * b = "); Serial.println(a * b); // Result: 30 Serial.print("a / b = "); Serial.println(a / b); // Result: 3 (Integer division, remainder is discarded) Serial.print("a % b = "); Serial.println(a % b); // Result: 1 (Remainder of 10/3)
// For floating-point division: float x = 10.0; float y = 3.0; Serial.print("x / y = "); Serial.println(x / y); // Result: 3.33}
void loop() {}2. Comparison Operators
Section titled “2. Comparison Operators”These operators compare two values and return a boolean result (true or false). They are most often used in conditional statements like if.
| Operator | Name | Description |
|---|---|---|
| == | Equal to | Returns true if operands are equal. |
| != | Not equal to | Returns true if operands are not equal. |
| < | Less than | Returns true if the left operand is less than the right. |
| > | Greater than | Returns true if the left operand is greater than the right. |
| <= | Less than or equal to | Returns true if the left operand is less than or equal to the right. |
| >= | Greater than or equal to | Returns true if the left operand is greater than or equal to the right. |
void setup() { Serial.begin(9600);
int sensorValue = 750; int threshold = 500;
Serial.print("Sensor Value: "); Serial.println(sensorValue); Serial.print("Threshold: "); Serial.println(threshold);
if (sensorValue > threshold) { Serial.println("Result: Sensor value is greater than threshold."); }
if (sensorValue == 750) { Serial.println("Result: Sensor value is exactly 750."); }}
void loop() {}3. Boolean Operators
Section titled “3. Boolean Operators”These operators are used to combine multiple conditional expressions.
| Operator | Name | Description |
|---|---|---|
| && | Logical AND | Returns true only if both operands are true. |
| || | Logical OR | Returns true if at least one of the operands is true. |
| ! | Logical NOT | Inverts the boolean value of its operand (true becomes false, false becomes true). |
void setup() { Serial.begin(9600);
int temperature = 25; int humidity = 50;
// Example 1: AND if (temperature >= 20 && humidity < 60) { Serial.println("Condition 1: Temperature is pleasant AND humidity is normal."); }
// Example 2: OR if (temperature > 30 || humidity > 80) { Serial.println("Condition 2: It's either too hot OR too humid."); }
// Example 3: NOT bool isRaining = false; if (!isRaining) { Serial.println("Condition 3: It is NOT raining."); }}
void loop() {}4. Bitwise Operators
Section titled “4. Bitwise Operators”Bitwise operators work on the individual bits of integer data types. They are highly efficient and commonly used in low-level microcontroller programming to manipulate hardware registers.
| Operator | Name | Description |
|---|---|---|
| & | Bitwise AND | Sets each bit to 1 if both corresponding bits are 1. |
| | | Bitwise OR | Sets each bit to 1 if at least one of the corresponding bits is 1. |
| ^ | Bitwise XOR | Sets each bit to 1 if only one of the corresponding bits is 1. |
| ~ | Bitwise NOT | Inverts all the bits. |
| << | Left Shift | Shifts bits to the left, filling with zeros. (Fast multiplication by 2). |
| >> | Right Shift | Shifts bits to the right. (Fast integer division by 2). |
// A byte is 8 bits. B00000001 is the binary representation for the number 1.byte flags = B00000001;
void setup() { Serial.begin(9600);
// Use Case 1: Set a bit (turn a flag on) with OR // Let's set the 3rd bit (bit 2) flags = flags | B00000100; // Result: B00000101 Serial.print("After setting bit 2: "); Serial.println(flags, BIN);
// Use Case 2: Check a bit with AND // Is bit 2 still set? if ((flags & B00000100) > 0) { Serial.println("Bit 2 is ON."); }
// Use Case 3: Toggle a bit with XOR // Let's toggle the 1st bit (bit 0) flags = flags ^ B00000001; // Result: B00000100 Serial.print("After toggling bit 0: "); Serial.println(flags, BIN);
// Use Case 4: Shift Left byte val = B00001100; // 12 Serial.print("12 << 1 = "); Serial.println(val << 1); // 24}
void loop() {}5. Compound Operators
Section titled “5. Compound Operators”Compound operators are a shorthand for performing an operation on a variable and assigning the new value back to the same variable.
| Shorthand | Equivalent To | Name |
|---|---|---|
| x++ | x = x + 1 | Increment |
| x— | x = x - 1 | Decrement |
| x += y | x = x + y | Compound Addition |
| x -= y | x = x - y | Compound Subtraction |
| x *= y | x = x * y | Compound Multiplication |
| x /= y | x = x / y | Compound Division |
void setup() { Serial.begin(9600);
int counter = 0; counter++; // counter is now 1 Serial.print("Counter after ++: "); Serial.println(counter);
int score = 100; score -= 20; // score is now 80 Serial.print("Score after -= 20: "); Serial.println(score);}
void loop() {}