The document explains the process of initializing variables in programming, emphasizing the importance of assigning valid data and understanding variable scope, which distinguishes between global and local variables. It also covers constant variables, which maintain fixed values, and the significance of data types in determining the storage space required for variables in Arduino programming. Additionally, it mentions the ability to declare multiple variables of the same data type in a single line to optimize code efficiency.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
1 views
Initializing variables
The document explains the process of initializing variables in programming, emphasizing the importance of assigning valid data and understanding variable scope, which distinguishes between global and local variables. It also covers constant variables, which maintain fixed values, and the significance of data types in determining the storage space required for variables in Arduino programming. Additionally, it mentions the ability to declare multiple variables of the same data type in a single line to optimize code efficiency.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10
Initializing variables
• Initializing variables means assigning a starting value
to a variable during declaration or within the program. For purposes of good programming practice, you need to ensure that a variable has valid data in it. An assignment operator will be used to assign a specific value to be stored in that variable. This can be done using a single equal sign (=) after the name of the variable. Example: 1 int inputVariablel = 7; 2 float pi = 3.1416; 3 4 void setup ( ) { 5 // put your setup code here , to run 6 once 7 8 } Variable Scope • Another thing you need to consider when declaring a variable is the part where you will declare it in the program. The scope of the variable refers to the visibility or accessibility of the variable within the program. The part where you declare the variable may identify the scope of the variable. • If you noticed , the variable is declared on top of the setup ( ) function. This is because of the variables are intended to be accessed or visible with other functions within the entire program. This is referred to as global variables. In Arduino programming, once a variable is declared outside the function, it is considered as a Global variable. This differentiates it from the Local Variable , which is declared inside a specific function only. As your program becomes more complex , the scope of the variable will be very useful to ensure which specific function could acces certain variables , to avoid possible program of logical error. Constant variables • Variables that are declared as constant are those variables with a fixed value that do not change throughout the program. If the variable will represent where a device is connected to the Arduino Microcontroller code, it will be best if the variable ; is created 1 as constant. const int sensorPin = 2; 2 3 void setup( ) { 4 // put your setup code here, to run once; 5 6 } • If you noticed , the variable is declared on top of the setup ( ) function. This is because of the variables are intended to be accessed or visible with other functions within the entire program. This is referred to as global variables. In Arduino programming, once a variable is declared outside the function, it is considered as a Global variable. This differentiates it from the Local Variable , which is declared inside a specific function only. As your program becomes more complex , the scope of the variable will be very useful to ensure which specific function could access certain variables , to avoid possible program of logical error. Const int sensorPi = 2 ; - this creates a constant integer type of variable named as sensorPin with 2 as the assigned value. An error will be encountered if the programmer tries to change the value of this variable in any part of the Arduino sketch. Data Type • As started earlier , specifying the data type is needed in declaring a variable. Aside from identifying the type of data. A variable will store, it will also specify the amount of space to be allotted for it in the storage or RAM. Data type in Arduino programming is almost the same with other programming languages like C++ programming. Data Type • The table below introduces the common of data types used in studying basic Arduino Programming.
Data Type Description Arduino (RAM) Usage
boolean Holds true or false value. 1 byte
char Stores a character value. 1 byte int Primary data type for whole number storage 4 bytes commonly in 16 bits (2 bytes). float Stores number with floating or decimal point. 4 Example: We use int because 7 is a whole number integer. 1 int inputVariablel = 7; 2 float pi = 3.1416; 3 4 void setup ( ) { 5 // put your setup code here , to run 6 once 7 We use float since Pi 8 } is a decimal number Multiple Variable Declaration • Variables with the same data can be declared with a single line to save space on your final output file. Each variable declaration must be separated by a comma ( , ).
1 const int LED = 2, motor Al= 3, motorBl= 5;
2 3 void setup( ) { 4 // put your setup code here, to run once; 5 6 }