Part 3 of the Arduino Programming Course
A variable is used in programming to store a value that may change during the life of the program (or sketch).
Memory is set aside for storing the variable and the variable is given a name which allows it to be accessed in the sketch.
One example of a variable is if you were to write a sketch that keeps the total of a teams score in a sports match. The value that the variable is holding (i.e. the teams score) would be displayed on a screen while the sports match is being played. As the score increases, the value that the variable holds will be increased and the display on the screen would be updated showing the new value that the score variable now holds.
In this example, the score value may change several times during the match and the time that the sketch is running. It is therefore variable as opposed to a fixed or constant value.
Using a Variable
The following sketch called variables demonstrates the use of a variable. Load this sketch to your Arduino and open the serial monitor window to see the output of the sketch.
void setup() { int count; Serial.begin(9600); count = 0; Serial.println(count); count = 1; Serial.println(count); count = 2; Serial.println(count); } void loop() { }
This video shows the sketch running.
Variable Definition
The following statement from the above sketch is a variable definition as it defines the variable type and name:int count;
Variable Type
In the above example, the variable type is int. This means that the variable can hold an integer value.An integer is a whole number – i.e. not a fraction. For example, the following numbers are integers 2, 5, 0, 100, 1024, -32, etc.Variable Name
The above variable has been given the name count. This variable can now be referenced or used in the sketch by using the name "count".By giving the variable a type and name, space is made available in memory for this variable.Using the Variable in a Sketch
After a variable has been defined, it can be assigned a value and the value of the variable can be displayed in the serial monitor window.In the following two statements, the count variable is first assigned a value of 0 and then the value that this variable is holding is sent out of the serial port for display in the serial monitor window.count = 0; Serial.println(count);In the sketch, the same lines of code are repeated, but each time the count variable is assigned a different value.Types of Variables
The integer (int) variable type is only one type of variable. An example of a different variable type is a float or floating point variable.A floating point variable is used to store a number that contains a fraction, e.g. 1.45, 99.99, 362.5634, -200.21, etc.A floating point variable is defined in the same way as an integer variable, except that the float keyword is used instead of int as shown in the example below.float average;
This floating point variable can now be assigned a floating point value e.g.:average = 1.2;Printing a Float Value
To print or send a floating point value to the Arduino serial monitor window, the println() function can be used.A second parameter can be passed to the println() function to specify the number of decimal places that must be printed as shown in the floats sketch below.void setup() { float average; Serial.begin(9600); average = 12.3299; Serial.println(average); Serial.println(average, 4); } void loop() { }The output of the above sketch is shown here.The above sketch assigns a value of 12.3299 to the average floating point variable. When the value of the variable is sent to the serial monitor window, we can see that println() automatically rounds the number off to two decimal places.The second time that println() is used to send the value of the variable to the serial monitor window, the number of decimal places is specified as 4. This is done by passing a second parameter value of 4 to the println() function.Other Variable Types
Other types of variables that are available can be seen in the Arduino language reference under the Variables heading.On this course, each variable type will be introduced and explained at the appropriate time.Naming Variables
Variables can be given any name that you like, so long as it sticks to the rules set out below. It is best to give variables meaningful names that help you and others to understand the sketch better, e.g. score_total would be a good variable name for a variable that is storing the running total of a score in a sports match.Variable Naming Rules
Initializing Variables
In the above example sketches, the variables that were used were first defined, and then assigned a value.
A variable can also be assigned an initial value when it is defined as shown below:
int total = 0;
In this example, the integer value named total is defined and assigned a value of zero (0) in one statement.
Notes
In this part of the course and other parts where concepts need to be explained, the example code will often be put in the setup() part of the sketch, rather than the loop() part. This is because the code usually only needs to run once to demonstrate some concept.
Most sketches that are written will have the bulk of the code in the loop() function and only code that initializes hardware in the setup() function.
No comments:
Post a Comment