2.0 The Arduino Programming Language: Fundamentals
Every Arduino program, known as a sketch, is built upon a foundation of core programming concepts derived from C/C++. Mastering the fundamental building blocks—program structure, data types, variables, and operators—is essential for writing functional and efficient code for any project, from the simplest to the most complex.
2.1 Program Structure: and
An Arduino program is referred to as a sketch. Every sketch is built upon two mandatory functions that form its core structure: setup() and loop().
The setup() function is called exactly once when a sketch starts. It runs after each power-up or reset of the Arduino board. Its primary purpose is to initialize settings that are required for the program to run, such as:
- Initializing variables.
- Setting pin modes (INPUT or OUTPUT).
- Starting communication libraries (e.g., Serial.begin()).
After the setup() function completes, the loop() function begins. As its name suggests, this function runs consecutively and repeatedly, allowing your program to change, respond, and actively control the Arduino board. The code inside loop() is the main body of your program, where you will read sensor data, control actuators, and perform the primary tasks of your project.
2.2 Data Types
Data types define the nature of variables, dictating how much memory they occupy and how the bit patterns stored within that memory are interpreted. Selecting the correct data type is crucial for memory efficiency and program correctness.
| Data Type | Description |
| void | Used only in function declarations to indicate that the function returns no value. |
| boolean | Holds one of two values: true or false. Occupies 1 byte of memory. |
| char | Stores a single character value (e.g., ‘A’). Occupies 1 byte. Characters are stored as numbers according to the ASCII chart, allowing for arithmetic operations. |
| unsigned char | An unsigned data type that occupies 1 byte and stores numbers from 0 to 255. |
| byte | Stores an 8-bit unsigned number, from 0 to 255. |
| int | The primary data type for number storage. On ATmega-based boards (like Uno), it stores a 16-bit (2-byte) signed value from -32,768 to 32,767. On 32-bit boards (like Due), it stores a 32-bit (4-byte) value from -2,147,483,648 to 2,147,483,647. |
| unsigned int | A 2-byte data type that only stores positive values, ranging from 0 to 65,535. On the Due, it is a 4-byte value ranging from 0 to 4,294,967,295. |
| word | Stores a 16-bit unsigned number on ATmega boards (0 to 65,535) and a 32-bit unsigned number on Due and Zero boards. |
| long | An extended size variable for number storage. It is a 32-bit (4-byte) signed value, ranging from -2,147,483,648 to 2,147,483,647. |
| unsigned long | A 32-bit (4-byte) unsigned value, ranging from 0 to 4,294,967,295. |
| short | A 16-bit (2-byte) signed data type with a range of -32,768 to 32,767. |
| float | A data type for floating-point numbers (numbers with a decimal point). It is stored as 32 bits (4 bytes) of information. |
| double | On ATmega-based boards (Uno), this is identical to float (4 bytes). On the Arduino Due, it has 8-byte (64-bit) precision. |
2.3 Variables and Scope
Variable scope defines the region of a program where a variable can be accessed. A variable can be declared in three places, which determines its scope.
- Local Variables Variables declared inside a function or a block of code (e.g., inside a for loop) are considered local. They are only accessible within that specific function or block. Once the function or block is exited, the local variable is destroyed and its memory is reclaimed.
- Formal Parameters These are variables used in the definition of a function’s parameters. They are treated as local variables within that function.
- Global Variables Global variables are defined outside of all functions, typically at the top of the program. They are accessible by any function throughout the entire program and hold their value for the program’s lifetime. They are useful for sharing data between different parts of your sketch, such as between the loop() function and an interrupt service routine.
2.4 Operators
An operator is a symbol that instructs the compiler to perform a specific mathematical, relational, or logical function.
Arithmetic Operators
| Operator Name | Symbol | Description | Example (A=10, B=20) |
| Assignment | = | Stores the value on the right in the variable on the left. | A = B |
| Addition | + | Adds two operands. | A + B gives 30 |
| Subtraction | – | Subtracts the second operand from the first. | A – B gives -10 |
| Multiplication | * | Multiplies both operands. | A * B gives 200 |
| Division | / | Divides the numerator by the denominator. | B / A gives 2 |
| Modulo | % | Returns the remainder of an integer division. | B % A gives 0 |
Comparison Operators
| Operator Name | Symbol | Description | Example (A=10, B=20) |
| Equal to | == | Checks if the values of two operands are equal. | (A == B) is false |
| Not equal to | != | Checks if the values of two operands are not equal. | (A != B) is true |
| Less than | < | Checks if the left operand is less than the right. | (A < B) is true |
| Greater than | > | Checks if the left operand is greater than the right. | (A > B) is false |
| Less than or equal to | <= | Checks if the left operand is less than or equal to the right. | (A <= B) is true |
| Greater than or equal to | >= | Checks if the left operand is greater than or equal to the right. | (A >= B) is false |
Boolean Operators
| Operator Name | Symbol | Description | Example (A is true, B is false) |
| AND | && | Returns true only if both operands are true. | (A && B) is false |
| OR | ` | ` | |
| NOT | ! | Reverses the logical state of its operand. | !A is false |
Bitwise Operators
| Operator Name | Symbol | Description | Example (A=60, B=13) |
| AND | & | Copies a bit to the result if it exists in both operands. | (A & B) gives 12 |
| OR | ` | ` | Copies a bit to the result if it exists in either operand. |
| XOR | ^ | Copies a bit if it is set in one operand but not both. | (A ^ B) gives 49 |
| NOT | ~ | Flips the bits of its operand. | ~A gives -61 |
| Shift Left | << | Moves the bits of the left operand to the left. | A << 2 gives 240 |
| Shift Right | >> | Moves the bits of the left operand to the right. | A >> 2 gives 15 |
Compound Operators
| Operator Name | Symbol | Description | Example (A=10) |
| Increment | ++ | Increases an integer value by one. | A++ gives 11 |
| Decrement | — | Decreases an integer value by one. | A– gives 9 |
| Compound Addition | += | Adds the right operand to the left and assigns the result. | A += 5 is A = A + 5 |
| Compound Subtraction | -= | Subtracts the right operand from the left and assigns. | A -= 5 is A = A – 5 |
| Compound Multiplication | *= | Multiplies the left operand by the right and assigns. | A *= 2 is A = A * 2 |
| Compound Division | /= | Divides the left operand by the right and assigns. | A /= 2 is A = A / 2 |
| Compound Modulo | %= | Takes the modulus and assigns the result. | A %= 3 is A = A % 3 |
| Compound Bitwise AND | &= | Performs bitwise AND and assigns the result. | A &= 2 is A = A & 2 |
| Compound Bitwise OR | ` | =` | Performs bitwise OR and assigns the result. |
With these fundamental building blocks understood, the next step is to learn how to control the order in which they are executed.