Table of Contents
What is the Modulo Operator?
The modulo operator is a mathematical operation that performs the division of two values and in return gives us their remainder. Let’s say we have one value of 9 and the other one is 2 when we divide both the remainder will be 1. To get the remainder of the number in Arduino the percent sign (%) is used which is a symbol for the modulo operator.
This modulo operator is commonly used in Arduino programming to maintain a variable within a specific range, such as the size of an array.
Syntax
The syntax for the remainder operation is as follows:
Parameter
The modulo operator takes the following parameters:
- remainder: It is a variable that will store the remainder value. The allowed data types are int, float and double.
- dividend: It is variable or constant. It is the integer value that is divided by another integer. Allowed data type is int.
- divisor: It is the non-zero variable with a value that divides another integer. The allowed data type for the divisor is int.
Return
This function returns the remainder of two numbers when divided.
How To Use the Modulo Operator in Arduino
To use the modulo operator in Arduino, you simply use the percent sign between two numbers. Here’s an example:
In this example, we are using the modulo operator to find the remainder of 10 divided by 3. The result is 1, which is stored in the variable remainder.
Example Program Using the Modulo Operator in Arduino
Let’s look at a complete program that uses the modulo operator to find the remainder of two numbers:
int num2 = 3;
int remainder = num1 % num2;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Remainder of ");
Serial.print(num1);
Serial.print(" and ");
Serial.print(num2);
Serial.print(" is: ");
Serial.println(remainder);
delay(10000);
}
The code begins by defining variables, num1 and num2. Their value is set to 10 and 3, respectively. We then use the modulo operator that will output the remainder after dividing both these numbers. The remainder value will be saved inside a variable name remainder. Finally, we use the Serial.println() function to print the value of the remainder to the Serial Monitor.
How To Use the Modulo Operator to Check for Even or Odd Numbers in Arduino
The modulo operator is also useful for checking if a number is even or odd. When we divide a certain number with 2 and if the remainder is zero it means the input value is even number while if we get some value of the remainder, it shows the number is odd. Here’s an example:
Serial.begin(9600);
while (!Serial); // wait for Serial Monitor to open
Serial.println("Enter a number:");
}
void loop() {
if (Serial.available()) {
int num = Serial.parseInt();
if (num % 2 == 0) {
Serial.print(num);
Serial.println(" is even");
} else {
Serial.print(num);
Serial.println(" is odd");
}
// wait for next input
Serial.println("Enter another number:");
}
}
The code started by checking the user input. It then enters the loop() function, which checks whether the user has entered a number by calling the Serial.available() function. If there is new data available, the code uses the Serial.parseInt() function to read the user’s input as an integer.
The code then uses the if statement to check if the user input is even or odd. It uses the Serial.print() function to print the result to the Serial Monitor.
Finally, the code prints a message asking the user to enter another number, and the process repeats.
Limitations of the Modulo Operator
While the modulo operator is a useful tool for finding the remainder of two numbers, it does have its limitations. The Modulo operator can only be used with integer values. If you try to use the modulo operator with floating-point numbers, you will encounter an error.
Conclusion
In Arduino programming, the modulo operator finds the remainder of two numbers. It is represented by the percent sign (%) and can be used with integer values. The modulo operator can also check for a certain input number if that input number is even or odd. However, it cannot be used with floating-point numbers. For details on modulo operators in Arduino read the article.