Type Here to Get Search Results !

How To Use Variable Declaration & Variable Declaration Data Type In Arduino Programming

In almost all programs, we will probably need to store values, do some calculation, try to access specific pins numbers in arduino and more. For that we can declare storage location that are called "variables" and we can access them using names.. Let me take for example the startbutton variable that is equal to 2. Assuming we know that in the physical circuit we connected some external button to the digital pin number 2 inside the arduino.

Now using this variable we can access the status of the button everywhere in the program. Let’s say in the same project or in another project we decided to physically change the connection of the button to pin #3. So instead of searching the all lines of code, we can just change it in one single line in the variable declaration.

Now before we can use a variable, we need to declare it so the arduino platform will be able to allocate the needed memory space. The way we declare a new variable is pretty straightforward, choosing a data type and a unique variable name. The data type define what kind of information we can store in the variable. Looking on the list of variables here, we can see different type of variables, one thing that is important to remember is that the arduino code is case sensitive, and this is relevant to variables names, functions names and more.

 Here some basic list of the most commonly used data types. The first one is boolean, where the size of the that variable is only 1 single bit, represent two states (true or false), very useful in the code that we are going to develop. Next one is byte, used to hold positive numbers from 0 to 255.

Char type is the same as byte but it can hold also negative value, the integer data type is one of the primary variable data type for storing numbers. It can be used to store numerical values between this long range. Other data types include the 32-bit long, which is the "big brother" of the integer and the last one here is floating-point numbers, expressed as the float data type, that  are capable of expressing fractions of a number using a decimal point.

Variables will behave in certain ways based on the properties of their data type. Ok, it can be integer, long, char and so on. In addition, by using variable qualifiers, it is possible to modify the behavior of certain variables. For example, numeric data types are available in two options: signed and unsigned

Unsigned variables will always contain positive values, while signed variables can express numbers with a negative value. Usually most data types default to signed data types, but can be changed using the unsigned variable qualifier. Meaning making them to store only positive values. Now this qualifier is useful for increasing the positive range of variables or for indicating to the reader that the value expected will never be negative

So in our example, by placing the unsigned keyword in front of the integer variable declaration the variable "maxcounter" may now only store positive values in the range of 0 to 65,535. The variable "maxcounter" may now only store
Positive values in the range of 0 to 65,535

Another type of variable qualifier is the constant variable qualifier, that makes the value of a variable a read-only that cannot be changed once the value has been assigned. We use the const keyword at the beginning of a variable declaration to define a variable as a constant value, to prevent us from changing by mistake the variable’s value later in the code.

For example, this is useful if we know that pin number of the blue led will never change. If we attempt to assign a new value to the constant variable "blueled", we will get a compiler error during the "verify" process. In addition to user defined constant variables, what we saw before,

The arduino development environment use its own predefined constants, so, the first of these constants defines the boolean logic states based on logical truth table using two states: true and false, the false condition is usually defined as being 0
Or sometimes off, on the other hand, true is, in many ways, anything other than false so it can be "true" or could be 1,

Unlike our other constants, true and false always appear in lowercase. Next one is pin level that defines the operational of a digital pin. As either input or output using the pinmode() function. So we are setting pin#13 as output using the constant "output". Or define the pin levels as being high or low,  equivalent to setting the pin states to +5v or 0v. Something we will use allot during our programs.


Post a Comment

0 Comments