After learning about data types, variables, and constants in Arduino programming, the next step is understanding what arithmetic operators are and how they are used in Arduino code.
Arithmetic operators perform arithmetic operations between two or more numbers or operands. It plays a crucial role in Arduino programming as it defines the conditions based on which the program works.
The arithmetic operators are used to perform essential mathematical functions. Based on these arithmetic operators, the logic for the desired program can be devised. Eleven operators are used for the mathematical calculations explained in this write-up.
The operators include:
Addition operator
Subtraction operator
Multiplication operator
Division operator
Square operator
Square root operator
Modulo operator
Power operator
Absolute operator
Minimum and Maximum operator
The addition operator performs the addition operation when two or more numbers are supposed to be added. The operands on which the addition operation is performed are declared as an integer and are initialized to an integer value. For addition the +
operator is used.
int c;int a = 6;int b = 3 ;void setup ( ){Serial.begin( 9600 );c = a + b;Serial.println(c);}void loop ( ){//your code here}// output is 9
The subtraction operator performs the subtraction operation when two or more numbers are supposed to be subtracted. The operands on which the subtraction operation is performed are declared integers and initialized to an integer value. For subtraction the -
operator is used.
int c;int a = 6;int b = 3 ;void setup ( ){Serial.begin( 9600 );c = a - b;Serial.println(c);}void loop ( ){//your code here}// output is 3
The multiplication operator performs the multiplication operation when two or more numbers are supposed to be multiplied. The operands whose product is to be calculated or on which multiplication operation is performed are declared integers and initialized to an integer value. For multiplication the *
operator is used.
int c;int a = 6;int b = 3 ;void setup ( ){Serial.begin( 9600 );c = a * b;Serial.println(c);}void loop ( ){//your code here}// output is 18
The division operator is used to divide a number by another. The operands to be divided are declared integers and initialized to an integer value, whereas the variable that will store the division value is declared as a float value to store the non-integer value. For division the /
operator is used.
float c;int a = 6;int b = 3 ;void setup ( ){Serial.begin( 9600 );c = a / b;Serial.println(c);}void loop ( ){//your code here}// output is 2.00
The square operator is used to calculate the square of a number. The square is calculated of a variable or a constant value. For calculating square sq()
function is used. Similarly, the data types used for the operator square are float, int, and double.
float c;int a = 6;void setup ( ){Serial.begin( 9600 );c = sq(a);Serial.println(c);}void loop ( ){// your code here}// output is 36.00
The square root operator is used to calculate the square root of a number. For calculating square the sqrt()
function is used. Similarly, the data types used for the operator square are float, int, and double.
float c;int a = 6;void setup ( ){Serial.begin( 9600 );c = sqrt(a);Serial.println(c);}void loop ( ){// your code here}// output is 2.45
The modulo operator is the remainder when two values are divided, yet they are not divided completely. As a result, a remainder value is left to find that value remainder operator is used by using a percentage symbol %
. The remainder value will be returned if both numbers are not entirely divisible.
float c;int a = 7;int b = 3;void setup ( ){Serial.begin( 9600 );c = a % b;Serial.println(c);}void loop ( ){// your code here}// output is 1.00
The power operator is used to raise a number to the power of another. It calculates the value of variables and constants when they are in exponential form. For solving exponential forms, the pow()
function is used.
float c;int a = 7;int b = 3;void setup ( ){Serial.begin( 9600 );c = pow(a,b);Serial.println(c);}void loop ( ){// your code here}// output is 343.00
The absolute operator returns the absolute value of the given variable or constant. This operator is used to convert a negative value to a positive. For this operation abs()
is used.
float c;int a = -6;void setup ( ){Serial.begin( 9600 );c = abs(a);Serial.println(c);}void loop ( ){// your code here}// output is 6.00
Using the max()
and min()
functions, maximum and minimum values between two given variables or constants can be calculated.
float c;int a = 7;int b = 3 ;void setup ( ){Serial.begin( 9600 );c = max(a,b);Serial.println(c);c = min(a,b);Serial.println(c);}void loop ( ){// your code here}//output is 7.00 for max() and 3.00 for min()
Understanding of the concept
What will be the value of c? If c = pow(a,b), given that a = 9, b = 2.
What will be the value of c? If c = sqrt(a), given that a = 9.
Arithmetic operators are vital in Arduino programming, facilitating mathematical calculations and data manipulation. Handle data types and potential overflows carefully due to limited resources. Mastering these operators empowers developers to create efficient and powerful Arduino projects.
For a guide on boolean operators in Arduino, you can check this out.