3.0 Programming Environment and Language
The Arduino programming experience is centered around the Arduino IDE and its simplified C++ language.
3.1 The Arduino IDE
The Integrated Development Environment is a simple application for managing, writing, and uploading code. The setup process involves:
- Downloading and Installing: Obtaining the correct IDE version for the operating system (Windows, macOS, Linux).
- Powering the Board: Connecting the Arduino board to the computer via a USB cable.
- Board Selection: Selecting the specific Arduino board model being used from the Tools > Board menu.
- Port Selection: Selecting the correct serial port that the board is connected to from the Tools > Serial Port menu.
The IDE toolbar provides essential functions:
- Verify: Checks the code for compilation errors.
- Upload: Compiles the code and sends it to the Arduino board.
- New, Open, Save: Standard file management for sketches.
- Serial Monitor: A tool to send and receive serial data between the board and the computer.
3.2 Program Structure: The “Sketch”
An Arduino program is referred to as a sketch. Every sketch has a mandatory two-part structure:
- void setup(): This function is called once when the sketch starts (after power-up or reset). It is used to initialize variables, set pin modes, and start using libraries.
- void loop(): After setup() completes, the loop() function runs repeatedly. This is where the main logic of the program resides, allowing the program to change and respond to inputs actively.
3.3 Core Language Concepts
The Arduino language is based on C/C++ and includes fundamental programming constructs.
Data Types
| Data Type | Description | Storage Size (AVR Boards) | Range (AVR Boards) |
| boolean | Holds true or false. | 1 byte | N/A |
| byte | Stores an 8-bit unsigned number. | 1 byte | 0 to 255 |
| char | Stores a character value or small number. | 1 byte | -128 to 127 |
| unsigned char | Unsigned version of char. | 1 byte | 0 to 255 |
| int | Primary data type for numbers. | 2 bytes | -32,768 to 32,767 |
| unsigned int | Unsigned version of int. | 2 bytes | 0 to 65,535 |
| word | A 16-bit unsigned number. | 2 bytes | 0 to 65,535 |
| long | Extended size variable for integers. | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| unsigned long | Unsigned version of long. | 4 bytes | 0 to 4,294,967,295 |
| float | For floating-point numbers (with decimals). | 4 bytes | -3.4028235E+38 to 3.4028235E+38 |
| double | Double-precision floating point. (Same as float on AVR boards). | 4 bytes | Same as float |
| String | An object for manipulating text. | Variable | N/A |
Variables and Scope
- Local Variables: Declared inside a function or block, only accessible within that scope.
- Global Variables: Declared outside of all functions, accessible from anywhere in the program, and persist for the program’s lifetime.
Operators
The language supports a rich set of operators, including:
- Arithmetic: +, -, *, /, % (modulo)
- Comparison: ==, !=, <, >, <=, >=
- Boolean: && (and), || (or), ! (not)
- Bitwise: &, |, ^, ~, <<, >>
- Compound: ++, –, +=, -=, *=
Control Structures and Loops
- Conditional Statements: if, if-else, switch-case allow the program to make decisions and execute different code blocks based on specified conditions.
- Loops: for, while, and do-while loops allow for the repeated execution of a block of statements.
Strings
Text can be handled in two ways:
- Character Arrays (C-style strings): An array of type char, ending in a null character (\0). This method is memory-efficient but requires manual manipulation.
- String Object: A more user-friendly object that provides methods for text manipulation (concat(), replace(), substring(), etc.). This approach is easier to use but consumes more memory, which can be a critical consideration on resource-constrained microcontrollers.