Operators in Arduino

During the development of our sketches for Arduino, we will often find ourselves dealing with the manipulation of variables (see previous article), on which we will perform arithmetic operations, comparison between values and implementations of logical conditions. To this end, the Arduino platform provides us with a certain number of operators, in particular arithmetic, relational and logical operators.

Arithmetic operators

The arithmetic operators, also called binary because they allow you to perform operations on two operands, are those of the four fundamental operations, as shown in the following table:

OperationOperator
Addition+
Subtraction
Multiplication*
Division/
Module%
table of arithmetic operators in Arduino

Using these operators we can add, subtract, multiply, divide and get the rest of the content of two or more variables through expressions as shown in the following code example:

int operand1, operand2, result; // declaration of variables variables

// assignment
operand1 = 10;
operand2 = 3;

//addition
result = operand1 + operand2;

//subtraction
result = operand1 - operand2;

//multiplication
result = operand1 * operand2;

//division
result = operand1 / operand2;

// module, get the remainder of the division
result = operand1% operand2;

As an exercise for the reader, before moving on to the test of the code reported later in the article, you can try to imagine what the value of the variable result is from time to time after each reassignment and, above all, in the last two operations of division and module.

Compound arithmetic operators

Often it is necessary to perform an arithmetic operation by applying a fixed quantity to a variable as in the following example:

int number = 3;
number = number + 5;

We first declared the variable number, assigning it the value 3 at the same time and, subsequently, we added 5 to it. However, there is a contracted form, with which we can obtain the same result in a faster and more elegant way, by applying the compound operator + =:

number + = 5; // unary addition operator

The unary operator can be applied to all arithmetic operators, as shown in the table below:

OperatorDefault expressionContracted expression
+=number = number + 5;number += 5;
-=number = number- 5;number -= 5;
*=number = number* 5;number *= 5;
/=number = number/ 5;number /= 5;
%=number = number% 5;number %= 5;
Table of compound arithmetic operators in Arduino

Unary operators

A unary operator corresponds to subtraction and addition, a denomination that indicates that they apply to a single operand. Unary operators are of two types:

  • Increment, adds a unit to the value of a variable: ++
  • Decrement, subtracts one unit from the value of a variable –

They can be either prefixed to the variable they apply to, or postfixes. In the first case, inside an expression with several operands, the value of the variable is first increased / decreased by one unit and then the next operation is performed; in the second case, the increase / decrease occurs after the next operation. The following code shows some examples of using the unary operator:

int variable = 7;

++ variable; // increment of one unit with prefix operator
variable ++; // increment of one unit with postfix operator

--variable; // decrement of one unit with prefix operator
variable--; // decrement of a unit with postfix operator

int result = ++ variable - 4;
result = variable ++ + 4;

Exercise for the reader: what values do the variables variable and result assume at each operation?

Relational operators

Some control operations that compare two operands are very useful for managing the flow of execution of an Arduino program. In particular, we might want to establish whether the value of a variable is greater, less, equal, greater or equal, less or equal, different from that of another. To do this we will use the relational operators schematized as follows:

Comparison operationOperator
Equal to==
Greater than>
Less than<
Different from!=
Greater than or equal to>=
Less than or equal to<=
table of relational operators in Arduino

By assigning the result of a comparison expression to a Boolean variable, we will be able to determine if the operation returns true or false, as in the following code:

int operand1, operand2;
bool result;

operand1 = 65;
operand2 = 34;

result = operand1 == operand2; // operand1 is the same as operand2?
result = operand1! = operand2; // operand1 is different from operand2?
result = operand1> = operand2; // operand1 is greater than or equal to operand2?
result = operand1 <= operand2; // operand1 is less than or equal to operand2?
result = operand1> operand2; // operand1 is greater than operand2?
result = operand1 <operand2; // operand1 is less than operand2?

The reader can practice by trying to answer the questions in the code comments. What is the value of the result variable from time to time?

Logical operators

The logical operators allow us to compare two or more Boolean expressions, obtained with the comparison operators. They can be very useful in an Arduino project, for example to trigger an alert if the values of two different sensors are below a certain threshold. The following table shows the syntax of the logical operators that can be used in sketches for Arduino:

OperatorDescription
&&AND
||OR
!NOT

The AND operator returns true if and only if both operands are true.

The OR operator returns true if one of the two operands is true, otherwise it returns false.

The NOT operator inverts the value of its operand, not by chance it is called a negation operator.

Let’s see some examples of logical operations in Arduino:

bool op1 = true;
bool op2 = true;
bool op3 = false;

bool result;

result = op1 && op2;
result = op1 && op3;
result = op1 || op2;
result = op1 || op3;
result =! op1;
result =! op3;
result = op1 &&! op3;
result = op1 || ! op3;

Sketch to test operators in Arduino

We are ready to bring together all the examples of use of operators in Arduino seen so far in a single summary program, which will also help us understand their behavior (and check the results if we have done the exercises suggested above!)

void setup () {
  Serial.begin (9600); // we set the serial port baudrate
  
  int operand1, operand2, result; // declaration of variables variables

  // assignment
  operand1 = 10;
  operand2 = 3;
  
  //addition
  result = operand1 + operand2;
  Serial.print ("Addition:");
  Serial.println (result);
  
  //subtraction
  result = operand1 - operand2;
  Serial.print ("subtraction:");
  Serial.println (result);
  
  //multiplication
  result = operand1 * operand2;
  Serial.print ("multiplication:");
  Serial.println (result);
  
  //division
  result = operand1 / operand2;
  Serial.print ("division:");
  Serial.println (result);
  
  // module, get the remainder of the division
  result = operand1% operand2;
  Serial.print ("module:");
  Serial.println (result);
  
  int variable = 7;

  ++ variable; // increment of one unit with prefix operator
  Serial.print ("increment with prefix operator:");
  Serial.println (variable);
  
  variable ++; // increment of one unit with postfix operator
  Serial.print ("increment with postfix operator:");
  Serial.println (variable);
  
  --variable; // decrement of one unit with prefix operator
  Serial.print ("decrement with prefix operator:");
  Serial.println (variable);
  
  variable--; // decrement of a unit with postfix operator
  Serial.print ("decrement with prefix operator:");
  Serial.println (variable);
  
  result = ++ variable - 4;
  Serial.print ("increment with prefix operator in an expression:");
  Serial.println (result);
  
  result = variable ++ + 4;
  Serial.print ("increment with postfix operator in an expression:");
  Serial.println (result);
    
  operand1 = 65;
  operand2 = 34;

  bool resultBool; // declaration of a bool variable for the outputs of logic and relational operations
  
  resultBool = operand1 == operand2; // operand1 is the same as operand2?
  Serial.print ("operand1 is equal to operand2:");
  Serial.println (resultBool);
  
  resultBool = operand1! = operand2; // operand1 is different from operand2?
  Serial.print ("Is operand1 different to operand2?");
  Serial.println (resultBool);
  
  resultBool = operand1> = operand2; // operand1 is greater than or equal to operand2?
  Serial.print ("Is operand1 greater than or equal to operand2?");
  Serial.println (resultBool);
  
  resultBool = operand1 <= operand2; // operand1 is less than or equal to operand2?
  Serial.print ("Is operand1 less than or equal to operand2?");
  Serial.println (resultBool);
  
  resultBool = operand1> operand2; // operand1 is greater than operand2?
  Serial.print ("Is operand1 greater than operand2?");
  Serial.println (resultBool);
  
  resultBool = operand1 <operand2; // operand1 is less than operand2?
  Serial.print ("Is operand1 less than operand2?");
  Serial.println (resultBool);

  bool op1 = true;
  bool op2 = true;
  bool op3 = false;
  

  Serial.println ("Logical operators:");
  resultBool = op1 && op2;
  Serial.println (resultBool);
  resultBool = op1 && op3;
  Serial.println (resultBool);
  resultBool = op1 || op2;
  Serial.println (resultBool);
  resultBool = op1 || op3;
  Serial.println (resultBool);
  resultBool =! op1;
  Serial.println (resultBool);
  resultBool =! op3;
  Serial.println (resultBool);
  resultBool = op1 &&! op3;
  Serial.println (resultBool);
  resultBool = op1 || ! op3;
  Serial.println (resultBool);

}

void loop () {
  // put your main code here, to run repeatedly:

}

Here is the output of the sketch on the serial monitor (as usual, for these first introductory examples, we don’t put any code in the loop function):

Addition: 13
subtraction: 7
multiplication: 30
division: 3
module: 1
increment with operator prefix: 8
increment with postfix operator: 9
decrement with prefix operator: 8
decrement with prefix operator: 7
increment with prefix operator in an expression: 4
increment with postfix operator in an expression: 12
operand1 is equal to operand2: 0
Is operand1 different to operand2? 1
Is operand1 greater than or equal to operand2? 1
Is operand1 less than or equal to operand2? 0
Is operand1 greater than operand2? 1
Is operand1 less than operand2? 0
Logical operators:
1
0
1
1
0
1
1
1

Note that the Serial.println () function prints an integer when passed a bool value. This is because Serial.println () tries to print the data byte of the ResultBool variable in ASCII format.

Conclusion

We have seen how Arduino allows us to manipulate the contents of the variables of our sketches through the use of some operators and we have tested their functioning through an example program. Now that we are able to use variables, types and operators to build more or less complex operations, we can approach the creation of something more interesting, which perhaps has to do with some sensor and actuator. But not before having had a taste of electronics, just enough to build some simple circuit, for example to turn on a LED or to acquire the temperature from a special sensor.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.