gavmac00/arduino-docs huggingface dataset:
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Compound operators
            > Compoundaddition
          
+=
          [Compound Operators]
          
Description
This is a convenient shorthand to perform addition on a variable with another constant or variable.
Syntax
x += y; // equivalent to the expression x = x + y;
Parameters
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Arithmetic operators
            > Addition
          
+
          [Arithmetic Operators]
          
Description
Addition is one of the four primary arithmetic operations. The operator + (plus) operates on two operands to produce the sum.
Syntax
sum = operand1 + operand2;
Parameters
Syntax
sum = operand1 + operand2;
Parameters
sum: variable. Allowed data types: int, float, double, byte, short, long.
operand1: variable or constant. Allowed data types: int, float, double, byte, short, long.
operand2: variable or constant. Allowed data types: int, float, double, byte, short, long.
Example Code
int a = 5;
int b = 10;
int c = 0;
c = a + b;  // the variable 'c' gets a value of 15 after this statement is executed
Notes and Warnings
The addition operation can overflow if the result is larger than that which can be stored in the data type (e.g. adding 1 to an integer with the value 32,767 gives -32,768).
If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
If the operands are of float / double data type and the variable that stores the sum is an integer, then only the integral part is stored and the fractional part of the number is lost.
float a = 5.5;
float b = 6.6;
int c = 0;
c = a + b;  // the variable 'c' stores a value of 12 only as opposed to the expected sum of 12.1
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Arithmetic operators
            > Assignment
          
=
          [Arithmetic Operators]
          
Description
> Assignment
          
=
          [Arithmetic Operators]
          
Description
The single equal sign = in the C++ programming language is called the assignment operator. It has a different meaning than in algebra class where it indicated an equation or equality. The assignment operator tells the microcontroller to evaluate whatever value or expression is on the right side of the equal sign, and store it in the variable to the left of the equal sign.
Example Code
int sensVal;              // declare an integer variable named sensVal
sensVal = analogRead(0);  // store the (digitized) input voltage at analog pin 0 in SensVal
Notes and Warnings
The variable on the left side of the assignment operator ( = sign ) needs to be able to hold the value stored in it. If it is not large enough to hold a value, the value stored in the variable will be incorrect.
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Arithmetic operators
            > Division
          
/
          [Arithmetic Operators]
          
Description
Division is one of the four primary arithmetic operations. The operator / (slash) operates on two operands to produce the result.
Syntax
result = numerator / denominator;
Parameters
Syntax
result = numerator / denominator;
Parameters
result: variable. Allowed data types: int, float, double, byte, short, long.
numerator: variable or constant. Allowed data types: int, float, double, byte, short, long.
denominator: non zero variable or constant. Allowed data types: int, float, double, byte, short, long.
Example Code
int a = 50;
int b = 10;
int c = 0;
c = a / b;  // the variable 'c' gets a value of 5 after this statement is executed
Notes and Warnings
If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
If the operands are of float / double data type and the variable that stores the result is an integer, then only the integral part is stored and the fractional part of the number is lost.
float a = 55.5;
float b = 6.6;
int c = 0;
c = a / b;  // the variable 'c' stores a value of 8 only as opposed to the expected result of 8.409
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Arithmetic operators
            > Multiplication
          
*
          [Arithmetic Operators]
          
Description
Multiplication is one of the four primary arithmetic operations. The operator * (asterisk) operates on two operands to produce the product.
Syntax
product = operand1 * operand2;
Parameters
Syntax
product = operand1 * operand2;
Parameters
product: variable. Allowed data types: int, float, double, byte, short, long.
operand1: variable or constant. Allowed data types: int, float, double, byte, short, long.
operand2: variable or constant. Allowed data types: int, float, double, byte, short, long.
Example Code
int a = 5;
int b = 10;
int c = 0;
c = a * b;  // the variable 'c' gets a value of 50 after this statement is executed
Notes and Warnings
The multiplication operation can overflow if the result is bigger than that which can be stored in the data type.
If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
If the operands are of float / double data type and the variable that stores the product is an integer, then only the integral part is stored and the fractional part of the number is lost.
float a = 5.5;
float b = 6.6;
int c = 0;
c = a * b;  // the variable 'c' stores a value of 36 only as opposed to the expected product of 36.3
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Arithmetic operators
            > Remainder
          
%
          [Arithmetic Operators]
          
Description
Remainder operation calculates the remainder when one integer is divided by another. It is useful for keeping a variable within a particular range (e.g. the size of an array). The % (percent) symbol is used to carry out remainder operation.
Syntax
Syntax
remainder = dividend % divisor;
Parameters
remainder: variable. Allowed data types: int, float, double.
dividend: variable or constant. Allowed data types: int.
divisor: non zero variable or constant. Allowed data types: int.
Example Code
int x = 0;
x = 7 % 5;  // x now contains 2
x = 9 % 5;  // x now contains 4
x = 5 % 5;  // x now contains 0
x = 4 % 5;  // x now contains 4
x = -4 % 5; // x now contains -4
x = 4 % -5; // x now contains 4
/* update one value in an array each time through a loop */
int values[10];
int i = 0;
void setup() {}
void loop() {
int i = 0;
void setup() {}
void loop() {
  values[i] = analogRead(0);
  i = (i + 1) % 10; // remainder operator rolls over variable
}
Notes and Warnings
The remainder operator does not work on floats.
If the first operand is negative, the result is negative (or zero).
Therefore, the result of x % 10 will not always be between 0 and 9 if x can be negative.
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Arithmetic operators
            > Subtraction
          
-
          [Arithmetic Operators]
          
Description
Subtraction is one of the four primary arithmetic operations. The operator - (minus) operates on two operands to produce the difference of the second from the first.
Syntax
difference = operand1 - operand2;
Parameters
Syntax
difference = operand1 - operand2;
Parameters
difference: variable. Allowed data types: int, float, double, byte, short, long.
operand1: variable or constant. Allowed data types: int, float, double, byte, short, long.
operand2: variable or constant. Allowed data types: int, float, double, byte, short, long.
Example Code
int a = 5;
int b = 10;
int c = 0;
c = a - b;  // the variable 'c' gets a value of -5 after this statement is executed
Notes and Warnings
The subtraction operation can overflow if the result is smaller than that which can be stored in the data type (e.g. subtracting 1 from an integer with the value -32,768 gives 32,767).
If one of the numbers (operands) are of the type float or of type double, floating point math will be used for the calculation.
If the operands are of float / double data type and the variable that stores the difference is an integer, then only the integral part is stored and the fractional part of the number is lost.
float a = 5.5;
float b = 6.6;
int c = 0;
c = a - b;  // the variable 'c' stores a value of -1 only as opposed to the expected difference of -1.1
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Sketch
            > Setup
          
setup()
          [Sketch]
          
Description
The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup() function will only run once, after each powerup or reset of the Arduino board.
Example Code
int buttonPin = 3;
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Sketch
            > Loop
          
loop()
          [Sketch]
          
Description
After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.
Example Code
Example Code
int buttonPin = 3;
// setup initializes serial and the button pin
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}
// loop checks the button pin each time,
// and will send serial if it is pressed
void loop() {
  if (digitalRead(buttonPin) == HIGH) {
    Serial.write('H');
  }
  else {
    Serial.write('L');
  }
  delay(1000);
}
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Boolean operators
            > Logicalnot
          
!
          [Boolean Operators]
          
Description
Logical NOT results in a true if the operand is false and vice versa.
Example Code
This operator can be used inside the condition of an if statement.
if (!x) { // if x is not true
  // statements
}
if (!x) { // if x is not true
  // statements
}
It can be used to invert the boolean value.
x = !y; // the inverted value of y is stored in x
Notes and Warnings
The bitwise not ~ (tilde) looks much different than the boolean not ! (exclamation point or "bang" as the programmers say) but you still have to be sure which one you want where.
See also
LANGUAGE ~ Bitwise NOT
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Boolean operators
            > Logicaland
          
&&
          [Boolean Operators]
          
Description
Logical AND results in true only if both operands are true.
Example Code
This operator can be used inside the condition of an if statement.
if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) { // if BOTH the switches read HIGH
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Boolean operators
            > Logicalor
          
||
          [Boolean Operators]
          
Description
Logical OR results in a true if either of the two operands is true.
Example Code
This operator can be used inside the condition of an if statement.
if (x > 0 || y > 0) { // if either x or y is greater than zero
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Bitwise operators
            > Bitwisenot
          
~
          [Bitwise Operators]
          
Description
~
          [Bitwise Operators]
          
Description
The bitwise NOT operator in C++ is the tilde character ~. Unlike & and |, the bitwise NOT operator is applied to a single operand to its right. Bitwise NOT changes each bit to its opposite: 0 becomes 1, and 1 becomes 0.
In other words:
0  1    operand1
-----
1  0   ~operand1
Example Code
int a = 103;  // binary:  0000000001100111
int b = ~a;   // binary:  1111111110011000 = -104
Notes and Warnings
Notes and Warnings
You might be surprised to see a negative number like -104 as the result of this operation. This is because the highest bit in an int variable is the so-called sign bit. If the highest bit is 1, the number is interpreted as negative. This encoding of positive and negative numbers is referred to as two’s complement. For more information, see the Wikipedia article on two’s complement.
As an aside, it is interesting to note that for any integer x, ~x is the same as -x - 1.
At times, the sign bit in a signed integer expression can cause some unwanted surprises.
See also
EXAMPLE BitMath Tutorial
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Bitwise operators
            > Bitshiftright
          
>>
          [Bitwise Operators]
          
Description
The right shift operator >> causes the bits of the left operand to be shifted right by the number of positions specified by the right operand.
Syntax
variable >> number_of_bits;
Parameters
variable: Allowed data types: byte, int, long.
Parameters
variable: Allowed data types: byte, int, long.
number_of_bits: a number that is < = 32. Allowed data types: int.
Example Code
int a = 40;     // binary: 0000000000101000
int b = a >> 3; // binary: 0000000000000101, or 5 in decimal
Notes and Warnings
When you shift x right by y bits (x >> y), and the highest bit in x is a 1, the behavior depends on the exact data type of x. If x is of type int, the highest bit is the sign bit, determining whether x is negative or not, as we have discussed above. In that case, the sign bit is copied into lower bits, for esoteric historical reasons:
int x = -16;  // binary: 1111111111110000
int y = 3;
int y = 3;
int result = x >> y;  // binary: 1111111111111110
This behavior, called sign extension, is often not the behavior you want. Instead, you may wish zeros to be shifted in from the left. It turns out that the right shift rules are different for unsigned int expressions, so you can use a typecast to suppress ones being copied from the left:
int x = -16;  // binary: 1111111111110000
int y = 3;
int result = (unsigned int)x >> y;  // binary: 0001111111111110
If you are careful to avoid sign extension, you can use the right-shift operator >> as a way to divide by powers of 2. For example:
int x = 1000;
int y = x >> 3; // integer division of 1000 by 8, causing y = 125.
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Bitwise operators
            > Bitshiftleft
          
<<
          [Bitwise Operators]
          
Description
The left shift operator << causes the bits of the left operand to be shifted left by the number of positions specified by the right operand.
Syntax
variable << number_of_bits;
Parameters
variable: Allowed data types: byte, int, long.
Parameters
variable: Allowed data types: byte, int, long.
number_of_bits: a number that is < = 32. Allowed data types: int.
Example Code
int a = 5;      // binary: 0000000000000101
int b = a << 3; // binary: 0000000000101000, or 40 in decimal
Notes and Warnings
When you shift a value x by y bits (x << y), the leftmost y bits in x are lost, literally shifted out of existence:
int x = 5;  // binary: 0000000000000101
int y = 14;
int result = x << y;  // binary: 0100000000000000 - the first 1 in 101 was discarded
If you are certain that none of the ones in a value are being shifted into oblivion, a simple way to think of the left-shift operator is that it multiplies the left operand by 2 raised to the right operand power. For example, to generate powers of 2, the following expressions can be employed:
   Operation  Result
   ---------  ------
    1 <<  0      1
    1 <<  1      2
    1 <<  2      4
    1 <<  3      8
    ...
    1 <<  8    256
    1 <<  9    512
    1 << 10   1024
    ...
1 << 10   1024
    ...
The following example can be used to print out the value of a received byte to the serial monitor, using the left shift operator to move along the byte from bottom(LSB) to top (MSB), and print out its Binary value:
// Prints out Binary value (1 or 0) of byte
void printOut1(int c) {
  for (int bits = 7; bits > -1; bits--) {
    // Compare bits 7-0 in byte
    if (c & (1 << bits)) {
      Serial.print("1");
    }
    else {
      Serial.print("0");
    }
  }
}
See also
EXAMPLE BitMath Tutorial
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Bitwise operators
            > Bitwiseor
          
|
          [Bitwise Operators]
          
Description
|
          [Bitwise Operators]
          
Description
The bitwise OR operator in C++ is the vertical bar symbol, |. Like the & operator, | operates independently each bit in its two surrounding integer expressions, but what it does is different (of course). The bitwise OR of two bits is 1 if either or both of the input bits is 1, otherwise it is 0.
In other words:
0  0  1  1    operand1
0  1  0  1    operand2
----------
0  1  1  1    (operand1 | operand2) - returned result
Example Code
int a =  92;    // in binary: 0000000001011100
int b = 101;    // in binary: 0000000001100101
int b = 101;    // in binary: 0000000001100101
int c = a | b;  // result:    0000000001111101, or 125 in decimal.
One of the most common uses of the Bitwise OR is to set multiple bits in a bit-packed number.
// Note: This code is AVR architecture specific
// set direction bits for pins 2 to 7, leave PD0 and PD1 untouched (xx | 00 == xx)
// same as pinMode(pin, OUTPUT) for pins 2 to 7 on Uno or Nano
DDRD = DDRD | 0b11111100;
See also
LANGUAGE || Logical OR
EXAMPLE BitMath Tutorial
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Bitwise operators
            > Bitwisexor
          
^
          [Bitwise Operators]
          
Description
^
          [Bitwise Operators]
          
Description
There is a somewhat unusual operator in C++ called bitwise EXCLUSIVE OR, also known as bitwise XOR. (In English this is usually pronounced "eks-or".) The bitwise XOR operator is written using the caret symbol ^. A bitwise XOR operation results in a 1 only if the input bits are different, else it results in a 0.
Precisely,
0  0  1  1    operand1
0  1  0  1    operand2
----------
0  1  1  0    (operand1 ^ operand2) - returned result
Example Code
int x = 12;     // binary: 1100
int y = 10;     // binary: 1010
int z = x ^ y;  // binary: 0110, or decimal 6
int z = x ^ y;  // binary: 0110, or decimal 6
The ^ operator is often used to toggle (i.e. change from 0 to 1, or 1 to 0) some of the bits in an integer expression. In a bitwise XOR operation if there is a 1 in the mask bit, that bit is inverted; if there is a 0, the bit is not inverted and stays the same.
// Note: This code uses registers specific to AVR microcontrollers (Uno, Nano, Leonardo, Mega, etc.)
// it will not compile for other architectures
void setup() {
  DDRB = DDRB | 0b00100000;  // set PB5 (pin 13 on Uno/Nano, pin 9 on Leonardo/Micro, pin 11 on Mega) as OUTPUT
  Serial.begin(9600);
}
void loop() {
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Bitwise operators
            > Bitwiseand
          
&
          [Bitwise Operators]
          
Description
&
          [Bitwise Operators]
          
Description
The bitwise AND operator in C++ is a single ampersand &, used between two other integer expressions. Bitwise AND operates on each bit position of the surrounding expressions independently, according to this rule: if both input bits are 1, the resulting output is 1, otherwise the output is 0.
Another way of expressing this is:
0  0  1  1    operand1
0  1  0  1    operand2
----------
0  0  0  1    (operand1 & operand2) - returned result
In Arduino, the type int is a 16-bit value, so using & between two int expressions causes 16 simultaneous AND operations to occur.
Example Code
In a code fragment like:
int a =  92;    // in binary: 0000000001011100
int a =  92;    // in binary: 0000000001011100
int b = 101;    // in binary: 0000000001100101
int c = a & b;  // result:    0000000001000100, or 68 in decimal.
Each of the 16 bits in a and b are processed by using the bitwise AND, and all 16 resulting bits are stored in c, resulting in the value 01000100 in binary, which is 68 in decimal.
One of the most common uses of bitwise AND is to select a particular bit (or bits) from an integer value, often called masking. See below for an example (AVR architecture specific).
PORTD = PORTD & 0b00000011;  // clear out bits 2 - 7, leave pins PD0 and PD1 untouched (xx & 11 == xx)
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > Break
          
break
          [Control Structure]
          
Description
break is used to exit from a for, while or do…​while loop, bypassing the normal loop condition. It is also used to exit from a switch case statement.
Example Code
Example Code
In the following code, the control exits the for loop when the sensor value exceeds the threshold.
int threshold = 40;
for (int x = 0; x < 255; x++) {
  analogWrite(PWMpin, x);
  sens = analogRead(sensorPin);
  if (sens > threshold) {     // bail out on sensor detect
    x = 0;
    break;
  }
  delay(50);
}
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > If
          
if
          [Control Structure]
          
Description
The if statement checks for a condition and executes the following statement or set of statements if the condition is 'true'.
Syntax
if (condition) {
  //statement(s)
}
Parameters
condition: a boolean expression (i.e., can be true or false).
}
Parameters
condition: a boolean expression (i.e., can be true or false).
Example Code
The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement.
if (x > 120) digitalWrite(LEDpin, HIGH);
if (x > 120)
digitalWrite(LEDpin, HIGH);
if (x > 120) {digitalWrite(LEDpin, HIGH);}
if (x > 120) {
  digitalWrite(LEDpin1, HIGH);
  digitalWrite(LEDpin2, HIGH);
}
// all are correct
Notes and Warnings
The statements being evaluated inside the parentheses require the use of one or more operators shown below.
Comparison Operators:
x == y (x is equal to y)
x != y (x is not equal to y)
x <  y (x is less than y)
x <  y (x is less than y)
x >  y (x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)
Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.
This is because C++ evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the (assignment operator)), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.
See also
LANGUAGE else
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > Else
          
else
          [Control Structure]
          
Description
> Else
          
else
          [Control Structure]
          
Description
The if…​else allows greater control over the flow of code than the basic if statement, by allowing multiple tests to be grouped. An else clause (if at all exists) will be executed if the condition in the if statement results in false. The else can proceed another if test, so that multiple, mutually exclusive tests can be run at the same time.
Each test will proceed to the next one until a true test is encountered. When a true test is found, its associated block of code is run, and the program then skips to the line following the entire if/else construction. If no test proves to be true, the default else block is executed, if one is present, and sets the default behavior.
Note that an else if block may be used with or without a terminating else block and vice versa. An unlimited number of such else if branches are allowed.
Syntax
Syntax
if (condition1) {
  // do Thing A
}
else if (condition2) {
  // do Thing B
}
else {
  // do Thing C
}
Example Code
Below is an extract from a code for temperature sensor system
if (temperature >= 70) {
  // Danger! Shut down the system.
}
else if (temperature >= 60) { // 60 <= temperature < 70
  // Warning! User attention required.
}
else { // temperature < 60
  // Safe! Continue usual tasks.
}
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > For
          
for
          [Control Structure]
          
Description
The for statement is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. The for statement is useful for any repetitive operation, and is often used in combination with arrays to operate on collections of data/pins.
Syntax
for (initialization; condition; increment) {
  // statement(s);
}
Parameters
initialization: happens first and exactly once.
condition: each time through the loop, condition is tested; if it’s true, the statement block, and the increment is executed, then the condition is tested again. When the condition becomes false, the loop ends.
increment: executed each time through the loop when condition is true.
Example Code
// Dim an LED using a PWM pin
int PWMpin = 10;  // LED in series with 470 ohm resistor on pin 10
void setup() {
  // no setup needed
}
void loop() {
  for (int i = 0; i <= 255; i++) {
    analogWrite(PWMpin, i);
    delay(10);
  }
}
Notes and Warnings
delay(10);
  }
}
Notes and Warnings
The C++ for loop is much more flexible than for loops found in some other computer languages, including BASIC. Any or all of the three header elements may be omitted, although the semicolons are required. Also the statements for initialization, condition, and increment can be any valid C++ statements with unrelated variables, and use any C++ datatypes including floats. These types of unusual for statements may provide solutions to some rare programming problems.
For example, using a multiplication in the increment line will generate a logarithmic progression:
for (int x = 2; x < 100; x = x * 1.5) {
  println(x);
}
Generates: 2,3,4,6,9,13,19,28,42,63,94
Another example, fade an LED up and down with one for loop:
void loop() {
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > Goto
          
goto
          [Control Structure]
          
Description
Transfers program flow to a labeled point in the program
Syntax
label:
goto label; // sends program flow to the label
Example Code
for (byte r = 0; r < 255; r++) {
Example Code
for (byte r = 0; r < 255; r++) {
  for (byte g = 255; g > 0; g--) {
    for (byte b = 0; b < 255; b++) {
      if (analogRead(0) > 250) {
        goto bailout;
      }
      // more statements ...
    }
  }
}
bailout:
// more statements ...
Notes and Warnings
The use of goto is discouraged in C programming, and some authors of C programming books claim that the goto statement is never necessary, but used judiciously, it can simplify certain programs. The reason that many programmers frown upon the use of goto is that with the unrestrained use of goto statements, it is easy to create a program with undefined program flow, which can never be debugged.
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > Return
          
return
          [Control Structure]
          
Description
Terminate a function and return a value from a function to the calling function, if desired.
Syntax
return;
return value;
Parameters
value: Allowed data types: any variable or constant type.
Example Code
A function to compare a sensor input to a threshold
Example Code
A function to compare a sensor input to a threshold
int checkSensor() {
  if (analogRead(0) > 400) {
    return 1;
  }
  else {
    return 0;
  }
}
The return keyword is handy to test a section of code without having to "comment out" large sections of possibly buggy code.
void loop() {
  // brilliant code idea to test here
  return;
  // the rest of a dysfunctional sketch here
  // this code will never be executed
}
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > Dowhile
          
do...while
          [Control Structure]
          
Description
The do…​while loop works in the same manner as the while loop, with the exception that the condition is tested at the end of the loop, so the do loop will always run at least once.
Syntax
do {
  // statement block
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > Continue
          
continue
          [Control Structure]
          
Description
The continue statement skips the rest of the current iteration of a loop (for, while, or do…​while). It continues by checking the conditional expression of the loop, and proceeding with any subsequent iterations.
Example Code
Example Code
The following code writes the value of 0 to 255 to the PWMpin, but skips the values in the range of 41 to 119.
for (int x = 0; x <= 255; x ++) {
  if (x > 40 && x < 120) {  // create jump in values
    continue;
  }
  analogWrite(PWMpin, x);
  delay(50);
}
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > Switchcase
          
switch...case
          [Control Structure]
          
Description
switch...case
          [Control Structure]
          
Description
Like if statements, switch case controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions. In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run.
The break keyword exits the switch statement, and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions ("falling-through") until a break, or the end of the switch statement is reached.
Syntax
switch (var) {
  case label1:
    // statements
    break;
  case label2:
    // statements
    break;
  default:
    // statements
    break;
}
Parameters
break;
  default:
    // statements
    break;
}
Parameters
var: a variable whose value to compare with various cases. Allowed data types: int, char.
label1, label2: constants. Allowed data types: int, char.
Returns
Nothing
Example Code
switch (var) {
  case 1:
    //do something when var equals 1
    break;
  case 2:
    //do something when var equals 2
    break;
  default:
    // if nothing else matches, do the default
    // default is optional
    break;
}
See also
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Structure
            >  Control structure
            > While
          
while
          [Control Structure]
          
Description
> While
          
while
          [Control Structure]
          
Description
A while loop will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.
Syntax
while (condition) {
  // statement(s)
}
Parameters
condition: a boolean expression that evaluates to true or false.
Example Code
var = 0;
while (var < 200) {
  // do something repetitive 200 times
  var++;
}
See also
EXAMPLE While Loop
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Variables
            >  Variable scope qualifiers
            > Volatile
          
volatile
          [Variable Scope & Qualifiers]
          
Description
volatile is a keyword known as a variable qualifier, it is usually used before the datatype of a variable, to modify the way in which the compiler and subsequent program treat the variable.
Declaring a variable volatile is a directive to the compiler. The compiler is software which translates your C/C++ code into the machine code, which are the real instructions for the Atmega chip in the Arduino.
Specifically, it directs the compiler to load the variable from RAM and not from a storage register, which is a temporary memory location where program variables are stored and manipulated. Under certain conditions, the value for a variable stored in registers can be inaccurate.
A variable should be declared volatile whenever its value can be changed by something beyond the control of the code section in which it appears, such as a concurrently executing thread. In the Arduino, the only place that this is likely to occur is in sections of code associated with interrupts, called an interrupt service routine.
int or long volatiles
int or long volatiles
If the volatile variable is bigger than a byte (e.g. a 16 bit int or a 32 bit long), then the microcontroller can not read it in one step, because it is an 8 bit microcontroller. This means that while your main code section (e.g. your loop) reads the first 8 bits of the variable, the interrupt might already change the second 8 bits. This will produce random values for the variable.
Remedy:
While the variable is read, interrupts need to be disabled, so they can’t mess with the bits, while they are read.
There are several ways to do this:
LANGUAGE noInterrupts
use the ATOMIC_BLOCK macro. Atomic operations are single MCU operations - the smallest possible unit.
Example Code
Example Code
The volatile modifier ensures that changes to the state variable are immediately visible in loop(). Without the volatile modifier, the state variable may be loaded into a register when entering the function and would not be updated anymore until the function ends.
// Flashes the LED for 1 s if the input has changed
// in the previous second.
volatile byte changed = 0;
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(2), toggle, CHANGE);
}
void loop() {
  if (changed == 1) {
    // toggle() has been called from interrupts!
    // Reset changed to 0
    changed = 0;
    // Blink LED for 200 ms
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    digitalWrite(LED_BUILTIN, LOW);
  }
}
void toggle() {
digitalWrite(LED_BUILTIN, LOW);
  }
}
void toggle() {
  changed = 1;
}
To access a variable with size greater than the microcontroller’s 8-bit data bus, use the ATOMIC_BLOCK macro. The macro ensures that the variable is read in an atomic operation, i.e. its contents cannot be altered while it is being read.
#include <util/atomic.h> // this library includes the ATOMIC_BLOCK macro.
volatile int input_from_interrupt;
// Somewhere in the code, e.g. inside loop()
  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
    // code with interrupts blocked (consecutive atomic operations will not get interrupted)
    int result = input_from_interrupt;
  }
See also
LANGUAGE attachInterrupt
Language
functions
variables
structure
Libraries
IoT Cloud API
Glossary
        The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    
    Find anything that can be improved? Suggest corrections and new documentation via GitHub.
    Doubts on how to use Github? Learn everything you need to know in this tutorial.
    
Last Revision: Searching...
Last Build: 2023/06/30 
Edit This Page
Reference
            >  Language
            >  Variables
            >  Variable scope qualifiers
            > Const
          
const
          [Variable Scope & Qualifiers]
          
Description