Table of Contents
Arduino Variables
In programming, values that are stored for further processing are named with a single word called a variable. The value of a variable can change continuously or be changed by the program. The variable name should be chosen in such a way that it is easily readable and understandable within the program. In addition, a variable also has a data type that defines the value range.
The Variable must be declared before being used. So the data type, the variable name and the value of the variable are set. If no value is specified, the variable value is set to 0.
datatype Variable_Name =Value;
Example:
int motorSpeed = 255;
Comprehensible variable names such as motorSpeed or temperatureSensor improve the readability of th program
Arduino Datatypes
A data type describes the value range and the possible operations of a variable. Since the data types of the variables require different amounts of memory, you must be aware of exactly which possible values a variable defined can have when creating the program: Is it only a digital state with the values yes and no or can the variable hold an entire text? The data type must be selected accordingly. The most important data types and their value ranges for creating Arduino programs are listed below:
Data type | Value range | Memory requirement | Explanations/Notes | Examples |
boolean | true/false | 1 byte | Value for Yes/No or On/Off, | boolean machineStatus = true; |
int | -32,768 to 32,767 | 2 bytes | Integer values for values without decimal places | int potiValue = 1023; |
unsigned int | 0 to 4,294,967,295 | 2 bytes | Integer for positive values without decimal places | int tempValue = -5; |
byte | 0 to 255 | 1 byte | Low values (and correspondingly small memory requirements) | byte brightnessLED = 126; |
word | Unsigned 16-bit or 32-bit | 16-bit on Atmega boards. 32-bit on Due and Zero | Be aware of processor used | word value = 1500; |
long | -2,147,483,648 to 2,147,483,647 | 4 bytes | Integer numbers with big values | long offset = -15362; |
unsigned long | 0 to 4,294,967,295 | 4 bytes | Unsigned Integer numbers with big values | unsigned long secondsPassed = 45379; |
float | -3.4028235E+38 to 3.4028235E+38 | 4 bytes | Values with decimal places | float voltageValue = 3.3; |
double | -3.4028235E+38 to 3.4028235E+38 | 4 bytes | -3.4028235E+38 to 3.4028235E+38 | double distanceGPS= 722.54; |
char | -128 to 127 | 1 byte | Value for storing one character | char charR = ‘R’; |
unsigned char | 0 to 255 | 1 byte | Usually used with numbers having range 0 to 255 | unsigned char charNumber = 255; |
string | length depends on definition | depends on need | Strings like texts or several characters | string brand[ ] = “ROBOSANS”; |
array | size depends to definition | Depends on array size | store values in variables with table form like e.g. lists of ports or configuration values | int portsPWM[ ] = {3,5,6,9,10,11}; |