id
stringlengths 14
16
| text
stringlengths 10
1.45k
| source
stringlengths 46
118
|
---|---|---|
a4c1897dfa8e-2
|
myByte ^ 0b11111111 = ~myByte;
Notes and Warnings
Because we are dealing with bits in a bitwise operator - it is convenient to use the binary formatter with constants. The numbers are still the same value in other representations, they are just not as easy to understand. Also, 0b00000000 is shown for clarity, but zero in any number format is zero.
Consequently - to toggle bits 0 & 1 of a variable, while leaving the rest of the variable unchanged, use the compound bitwise XOR operator (^=) with the constant 0b00000011
1 0 1 0 1 0 1 0 variable
0 0 0 0 0 0 1 1 mask
----------------------
|
https://www.arduino.cc/reference/en/language/structure/compound-operators/compoundbitwisexor/index.html
|
a4c1897dfa8e-3
|
----------------------
1 0 1 0 1 0 0 1
bits unchanged
bits toggled
Here is the same representation with the variables bits replaced with the symbol x. ~x represents the complement of x.
x x x x x x x x variable
0 0 0 0 0 0 1 1 mask
----------------------
x x x x x x ~x ~x
bits unchanged
bits set
So if:
myByte = 0b10101010;
myByte ^= 0b00000011 == 0b10101001;
See also
LANGUAGE ^ Bitwise XOR
|
https://www.arduino.cc/reference/en/language/structure/compound-operators/compoundbitwisexor/index.html
|
33fd50f3ac07-0
|
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
> Decrement
--
[Compound Operators]
Description
Decrements the value of a variable by 1.
Syntax
x--; // decrement x by one and returns the old value of x
--x; // decrement x by one and returns the new value of x
Parameters
|
https://www.arduino.cc/reference/en/language/structure/compound-operators/decrement/index.html
|
33fd50f3ac07-1
|
--x; // decrement x by one and returns the new value of x
Parameters
x: variable. Allowed data types: int, long (possibly unsigned).
Returns
The original or newly decremented value of the variable.
Example Code
x = 2;
y = --x; // x now contains 1, y contains 1
y = x--; // x contains 0, but y still contains 1
See also
|
https://www.arduino.cc/reference/en/language/structure/compound-operators/decrement/index.html
|
a52741b5fea2-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/compound-operators/compoundaddition/index.html
|
a52741b5fea2-1
|
Syntax
x += y; // equivalent to the expression x = x + y;
Parameters
x: variable. Allowed data types: int, float, double, byte, short, long.
y: variable or constant. Allowed data types: int, float, double, byte, short, long.
Example Code
x = 2;
x += 4; // x now contains 6
See also
LANGUAGE Normal Addition
|
https://www.arduino.cc/reference/en/language/structure/compound-operators/compoundaddition/index.html
|
27ecaee7256f-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/addition/index.html
|
27ecaee7256f-1
|
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).
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/addition/index.html
|
27ecaee7256f-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/addition/index.html
|
ca587d7dea01-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/assignment/index.html
|
ca587d7dea01-1
|
> 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.
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/assignment/index.html
|
ca587d7dea01-2
|
Don’t confuse the assignment operator [ = ] (single equal sign) with the comparison operator [ == ] (double equal signs), which evaluates whether two expressions are equal.
See also
LANGUAGE if (conditional operators)
LANGUAGE char
LANGUAGE int
LANGUAGE long
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/assignment/index.html
|
5643e1543b96-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/division/index.html
|
5643e1543b96-1
|
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.
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/division/index.html
|
5643e1543b96-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/division/index.html
|
d5f076f26813-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/multiplication/index.html
|
d5f076f26813-1
|
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.
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/multiplication/index.html
|
d5f076f26813-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/multiplication/index.html
|
75a4dcefb713-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/remainder/index.html
|
75a4dcefb713-1
|
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() {
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/remainder/index.html
|
75a4dcefb713-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/remainder/index.html
|
c429dce92225-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/subtraction/index.html
|
c429dce92225-1
|
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).
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/subtraction/index.html
|
c429dce92225-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/subtraction/index.html
|
c79462aad412-0
|
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;
|
https://www.arduino.cc/reference/en/language/structure/sketch/setup/index.html
|
c79462aad412-1
|
Example Code
int buttonPin = 3;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
// ...
}
See also
|
https://www.arduino.cc/reference/en/language/structure/sketch/setup/index.html
|
b8f49bd197fe-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/sketch/loop/index.html
|
b8f49bd197fe-1
|
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
|
https://www.arduino.cc/reference/en/language/structure/sketch/loop/index.html
|
7b86fa654c3d-0
|
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
}
|
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicalnot/index.html
|
7b86fa654c3d-1
|
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
|
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicalnot/index.html
|
a0374045488b-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicaland/index.html
|
a0374045488b-1
|
// statements
}
Notes and Warnings
Make sure you don’t mistake the boolean AND operator, && (double ampersand) for the bitwise AND operator & (single ampersand). They are entirely different beasts.
See also
LANGUAGE & (Bitwise AND)
|
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicaland/index.html
|
21fa8971f569-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicalor/index.html
|
21fa8971f569-1
|
// statements
}
Notes and Warnings
Do not confuse the boolean || (double pipe) operator with the bitwise OR operator | (single pipe).
See also
LANGUAGE | Bitwise OR
|
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicalor/index.html
|
124fb469c43e-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwisenot/index.html
|
124fb469c43e-1
|
~
[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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwisenot/index.html
|
124fb469c43e-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwisenot/index.html
|
892b586ccd8c-0
|
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.
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitshiftright/index.html
|
892b586ccd8c-1
|
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;
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitshiftright/index.html
|
892b586ccd8c-2
|
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.
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitshiftright/index.html
|
892b586ccd8c-3
|
See also
EXAMPLE BitMath Tutorial
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitshiftright/index.html
|
15b564c41fb2-0
|
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.
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitshiftleft/index.html
|
15b564c41fb2-1
|
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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitshiftleft/index.html
|
15b564c41fb2-2
|
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
...
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitshiftleft/index.html
|
15b564c41fb2-3
|
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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitshiftleft/index.html
|
584732599718-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwiseor/index.html
|
584732599718-1
|
|
[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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwiseor/index.html
|
584732599718-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwiseor/index.html
|
b4e5164274a7-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwisexor/index.html
|
b4e5164274a7-1
|
^
[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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwisexor/index.html
|
b4e5164274a7-2
|
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() {
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwisexor/index.html
|
b4e5164274a7-3
|
Serial.begin(9600);
}
void loop() {
PORTB = PORTB ^ 0b00100000; // invert PB5, leave others untouched
delay(100);
}
See also
EXAMPLE BitMath Tutorial
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwisexor/index.html
|
fc3de75cc991-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwiseand/index.html
|
fc3de75cc991-1
|
&
[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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwiseand/index.html
|
fc3de75cc991-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwiseand/index.html
|
fc3de75cc991-3
|
See also
LANGUAGE && Logical AND
EXAMPLE BitMath Tutorial
|
https://www.arduino.cc/reference/en/language/structure/bitwise-operators/bitwiseand/index.html
|
9f28086dea63-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/break/index.html
|
9f28086dea63-1
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/break/index.html
|
a477e733c82a-0
|
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).
|
https://www.arduino.cc/reference/en/language/structure/control-structure/if/index.html
|
a477e733c82a-1
|
}
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)
|
https://www.arduino.cc/reference/en/language/structure/control-structure/if/index.html
|
a477e733c82a-2
|
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.
|
https://www.arduino.cc/reference/en/language/structure/control-structure/if/index.html
|
a477e733c82a-3
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/if/index.html
|
96983251a6c1-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/else/index.html
|
96983251a6c1-1
|
> 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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/else/index.html
|
96983251a6c1-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/else/index.html
|
140b72581cde-0
|
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.
|
https://www.arduino.cc/reference/en/language/structure/control-structure/for/index.html
|
140b72581cde-1
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/for/index.html
|
140b72581cde-2
|
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() {
|
https://www.arduino.cc/reference/en/language/structure/control-structure/for/index.html
|
140b72581cde-3
|
Another example, fade an LED up and down with one for loop:
void loop() {
int x = 1;
for (int i = 0; i > -1; i = i + x) {
analogWrite(PWMpin, i);
if (i == 255) {
x = -1; // switch direction at peak
}
delay(10);
}
}
See also
|
https://www.arduino.cc/reference/en/language/structure/control-structure/for/index.html
|
1b51216eafb9-0
|
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++) {
|
https://www.arduino.cc/reference/en/language/structure/control-structure/goto/index.html
|
1b51216eafb9-1
|
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.
|
https://www.arduino.cc/reference/en/language/structure/control-structure/goto/index.html
|
1b51216eafb9-2
|
With that said, there are instances where a goto statement can come in handy, and simplify coding. One of these situations is to break out of deeply nested for loops, or if logic blocks, on a certain condition.
See also
|
https://www.arduino.cc/reference/en/language/structure/control-structure/goto/index.html
|
bf9a1a8f5fa1-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/return/index.html
|
bf9a1a8f5fa1-1
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/return/index.html
|
3229ecda51b5-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/dowhile/index.html
|
3229ecda51b5-1
|
Syntax
do {
// statement block
} while (condition);
Parameters
condition: a boolean expression that evaluates to true or false.
Example Code
int x = 0;
do {
delay(50); // wait for sensors to stabilize
x = readSensors(); // check the sensors
} while (x < 100);
See also
|
https://www.arduino.cc/reference/en/language/structure/control-structure/dowhile/index.html
|
e693c451c9f3-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/continue/index.html
|
e693c451c9f3-1
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/continue/index.html
|
914129355149-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/index.html
|
914129355149-1
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/index.html
|
914129355149-2
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/index.html
|
2458741dce0c-0
|
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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/while/index.html
|
2458741dce0c-1
|
> 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
|
https://www.arduino.cc/reference/en/language/structure/control-structure/while/index.html
|
e4a1765d2b2a-0
|
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.
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/index.html
|
e4a1765d2b2a-1
|
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
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/index.html
|
e4a1765d2b2a-2
|
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
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/index.html
|
e4a1765d2b2a-3
|
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() {
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/index.html
|
e4a1765d2b2a-4
|
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
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/volatile/index.html
|
70ccb4aa3a30-0
|
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
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/const/index.html
|
70ccb4aa3a30-1
|
const
[Variable Scope & Qualifiers]
Description
The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only". This means that the variable can be used just as any other variable of its type, but its value cannot be changed. You will get a compiler error if you try to assign a value to a const variable.
Constants defined with the const keyword obey the rules of variable scoping that govern other variables. This, and the pitfalls of using #define, makes the const keyword a superior method for defining constants and is preferred over using #define.
Example Code
const float pi = 3.14;
float x;
// ....
x = pi * 2; // it's fine to use consts in math
pi = 7; // illegal - you can't write to (modify) a constant
Notes and Warnings
#define or const
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/const/index.html
|
70ccb4aa3a30-2
|
Notes and Warnings
#define or const
You can use either const or #define for creating numeric or string constants. For arrays, you will need to use const. In general const is preferred over #define for defining constants.
See also
LANGUAGE #define
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/const/index.html
|
ff9bc86b2973-0
|
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
> Scope
scope
[Variable Scope & Qualifiers]
Description
Variables in the C++ programming language, which Arduino uses, have a property called scope. This is in contrast to early versions of languages such as BASIC where every variable is a global variable.
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/scope/index.html
|
ff9bc86b2973-1
|
A global variable is one that can be seen by every function in a program. Local variables are only visible to the function in which they are declared. In the Arduino environment, any variable declared outside of a function (e.g. setup(), loop(), etc. ), is a global variable.
When programs start to get larger and more complex, local variables are a useful way to insure that only one function has access to its own variables. This prevents programming errors when one function inadvertently modifies variables used by another function.
It is also sometimes handy to declare and initialize a variable inside a for loop. This creates a variable that can only be accessed from inside the for-loop brackets.
Example Code
int gPWMval; // any function will see this variable
void setup() {
// ...
}
void loop() {
int i; // "i" is only "visible" inside of "loop"
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/scope/index.html
|
ff9bc86b2973-2
|
int i; // "i" is only "visible" inside of "loop"
float f; // "f" is only "visible" inside of "loop"
// ...
for (int j = 0; j < 100; j++) {
// variable j can only be accessed inside the for-loop brackets
}
}
See also
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/scope/index.html
|
78883f17f2bb-0
|
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
> Static
static
[Variable Scope & Qualifiers]
Description
The static keyword is used to create variables that are visible to only one function. However unlike local variables that get created and destroyed every time a function is called, static variables persist beyond the function call, preserving their data between function calls.
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/static/index.html
|
78883f17f2bb-1
|
Variables declared as static will only be created and initialized the first time a function is called.
Example Code
/* RandomWalk
Paul Badger 2007
RandomWalk wanders up and down randomly between two
endpoints. The maximum move in one loop is governed by
the parameter "stepsize".
A static variable is moved up and down a random amount.
This technique is also known as "pink noise" and "drunken walk".
*/
#define randomWalkLowRange -20
#define randomWalkHighRange 20
int stepsize;
int thisTime;
void setup() {
Serial.begin(9600);
}
void loop() {
// test randomWalk function
stepsize = 5;
thisTime = randomWalk(stepsize);
Serial.println(thisTime);
delay(10);
}
int randomWalk(int moveSize) {
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/static/index.html
|
78883f17f2bb-2
|
delay(10);
}
int randomWalk(int moveSize) {
static int place; // variable to store value in random walk - declared static so that it stores
// values in between function calls, but no other functions can change its value
place = place + (random(-moveSize, moveSize + 1));
if (place < randomWalkLowRange) { // check lower and upper limits
place = randomWalkLowRange + (randomWalkLowRange - place); // reflect number back in positive direction
}
else if (place > randomWalkHighRange) {
place = randomWalkHighRange - (place - randomWalkHighRange); // reflect number back in negative direction
}
return place;
}
See also
|
https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/static/index.html
|
c93dbb12e734-0
|
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
> Conversion
> Bytecast
byte()
[Conversion]
Description
Converts a value to the byte data type.
Syntax
byte(x)
(byte)x (C-style type conversion)
Parameters
x: a value. Allowed data types: any type.
Returns
Data type: byte.
See also
LANGUAGE byte
|
https://www.arduino.cc/reference/en/language/variables/conversion/bytecast/index.html
|
310facda0edb-0
|
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
> Conversion
> Wordcast
word()
[Conversion]
Description
Converts a value to the word data type.
Syntax
word(x)
word(h, l)
(word)x (C-style type conversion)
Parameters
x: a value. Allowed data types: any type.
|
https://www.arduino.cc/reference/en/language/variables/conversion/wordcast/index.html
|
310facda0edb-1
|
Parameters
x: a value. Allowed data types: any type.
h: the high-order (leftmost) byte of the word.
l: the low-order (rightmost) byte of the word.
Returns
Data type: word.
See also
LANGUAGE word
|
https://www.arduino.cc/reference/en/language/variables/conversion/wordcast/index.html
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.