Spis treści:
Arduino operators are a type of symbols used to program the board. Thanks to them the compiler knows exactly what action to perform.
Arduino programming operators
Arduino board programming is very similar to programming virtually any other development board. It relies heavily on creating logical relationships between variables. Sometimes these involve operations on values from external sources such as sensors, peripherals (for example, a keyboard) or from a clock. A full understanding of how each operator works is essential to using them correctly. Operators involve different types of operations – depending on the source of information they can be divided into different categories, but the most basic ones are logical operators, bitwise operators, arithmetic operators, assignment operators, comparison operators.
What are Arduino arithmetic operators?
Arithmetic operators are programming tools thanks to which you can perform basic arithmetic operations on variables. They are mainly concerned with addition, multiplication, subtraction, division, and thus are able to return the result of a sum, difference, product, quotient or remainder from an action on two arguments.
Operator “ + ”
The “+” operator is responsible for the sum operation, i.e. operations on two variables – it returns the value of the sum of expressions. When using this operation you must remember about coincidence of types of added variables, or about effects which result from lack of coincidence. For example, if the variable to which the result of the operation will be assigned stores only integers (for example “int”), then even in the case of a result with a number after the decimal point (for example the sum of two “float” variables) only the integer variable will be saved (for example 2 + 4,2 = 6), and the fraction part will be lost.
Operator ” – “
This operator is responsible for the difference operation, that is the operation of subtracting from the value of the variable before the “-” sign the value of the variable after this sign (the order is important!). When performing this operation, you should also be aware of the capacity of the variables due to their types (some are able to write decimal parts and some are not), so that you do not lose valuable data.
Operator ” * “
The ‘*’ operator is responsible for the arithmetic operation called multiplication. It is one of the main arithmetic operations that allows you to perform mathematical operations on variables, as an example.
Operator ” / “
The “/” operator is responsible for the quotient (division), another of the four most important arithmetic operations. The operator causes an operation between two variables and returns the result at the end. Again, special care must be taken with the type of variables on which operations are performed – inappropriate selection may result in loss of some data.
Operator ” = “
The operator “=” is an assignment function. For example, you can use it to assign the result of an action on another variable to a new variable. The value stored in the variable after the assignment operator is assigned to the variable before it. For example if:
Operator ” % “
The “%” character is an operator that returns the remainder of the division. If the result of dividing two integers is an integer, the “%” operator will return just that remainder, but if dividing variables that result in an integer, it will also return the remainder (0). So, for example:
10%5 = 0 (because 10/5 = 2), but
11%5 = 1 (because 10/5 = 2 and 1 is already indivisible by 2)
This type of operation often allows conversion between types of variables, for example.
Comparison operators
Comparison operators allow you to create conditional relationships, for example, if the variable a (to which temperature is assigned) is greater than a certain value, then turn on cooling. These operators are also very useful in conditional loops. Basic operators for Arduino are:
Operator ” > “
The “greater than” operator represented by the “>” sign, which is supposed to compare the value of the variable before the operator to the value placed after it. If the first variable is greater than the second – the result of the operation will be “TRUE” (with value 1), otherwise the value will be “FALSE” (with value 0). Remember to operate on variables of the same type when using dependencies.
Operator ” >= “
The operator “>=” consists of two characters – the so-called “greater than or equal to”. It works on the same principle as the operator shown above, but also assumes a positive result when the compared values are equal.
Operator ” < “
The “less than” operator. Works on the exact opposite principle of the “greater than” operator – in the case where the variable before the operator is greater than the one after it, the result will be “FALSE” (with a value of 0), i.e. “not true that the first variable is smaller than the second”. The value “TRUE” (with a value of 1), will be when the value of the first variable is less than the value of the second variable.
Operator ” <= “
The “less than or equal to” operator is analogous to the above operator consisting of two characters. The positive result will always be the case when there is a variable before the operator with value less than or equal to the value of the variable placed after the operator (the value “FALSE” will be the result in any other case).
Operator ” == “
The equality operator, written as “==”, is able to clearly determine if the values of two variables being compared are equal – only if so, the result of such operation will be “TRUE”. Otherwise the result will be “FALSE” (with value 0).
Operator ” != “
The inequality operator, or for some “negation” is written as “!=”. It is a tool by which you can explicitly state that the values of two variables are not equal. The result will always be “TRUE” until the values of the variables on both sides of the operator are the same. It is worth remembering to operate on the same types of variables to avoid false results.
Other operators
Programming largely boils down to manipulating operators with known functions. A programmer who intends to create a program on Arduino should know exactly what specific Arduino operators are used for. Presented above are only the most basic operators.
Nevertheless, the best way to learn how operators work is to combine theory with practice. If you can, start programming and using operators yourself, using available sources of knowledge.
How useful was this post?
Click on a star to rate it!
Average rating 0 / 5. Vote count: 0
No votes so far! Be the first to rate this post.