| Variables are places in computer memory for storing changing information. We use variables to keep records for our program, such as whether the user has pushed a button or not, how many times theyve pushed the button, how many times the microcontroller has flashed a light, how much time has passed since the last button push, and so forth.
Think of computer memory as a bunch of coffee cups that you can put a label on the outside and store things on the inside. Variables allow you to put your own names on the outside of the coffee cup and put things you want to remember inside of it. You can then use the stuff inside the cups by referring to them by name in your if statements and loops can then find the coffee cups by name and take different actions based on what's there. The real power comes not from the fact that you can place things in variables, but that you can replace them or vary them easily.
Before you use a variable in most languages BASIC, C, Wiring, etc.) you need to give it a name. This is generally done at the very beginning of your program or at the beginning of a routine. This is called declaring the variable.
Use a name that describes what youre using the variable to remember, because it will make your code much more readable. Adding "Var" to the ends of variable names, makes them easy to identify when reading the code. You can use any name you want for your variable as long as it does not start with a number, has no spaces, and isnt a keyword. When you try to run your program, the compiler will let you know if your variable name isnt allowed.
To store a value in a variable you put the name of the variable on the left side of an equation and the value you want to remember on the right, like so:
fooVar = 12 sensorVar = 250 switchVar = 125
Every variable has a data type. The data type of a variable determines how much memory the microcontroller needs for the variable, and how it will use the data stored in the variable. You declare both the name and the data type of the variable before you use it, like so:
char fooVar; int barVar; long timeVar;
byteVar var word switchVar var bit bigVar var word
dim byteVar as byte dim bigVar as integer dim fractionVar as single
Here are the data types for the PIC and BX-24, broken down by the amount of memory each needs:
|To understand how variables are stored in memory, it's useful to think about what memory is. a computer's memory is basically a matrix of switches, laid out in a regular grid, not unlike the switches you see on the back of a lot of electronic gear:
Each switch represents the smallest unit of memory, a bit (usually, we think in terms of bytes when talking about memory. a byte is simply eight bits). If the switch is on, the bit's value is 1. If it's off, the value is 0. Each bit has an address in the grid. We can envision a grid that represents that memory like this:
|Note: programmers like to start with 0 as the first number. So often, as in these arrays,the first element will be numbered 0 instead of 1.|
|Note that this grid is arranged in rows of 8 bits; each row represents a byte of memory in this illustration.
When we declare a variable, the microcontroller picks the next available address and sets aside as many bits are needed for the data type we declare. If we declare a byte, for example, it sets aside 8 bits. An integer, 16 bits. a string, one byte (eight bits) for every character of the string.
Let's say we made the following variable declarations at the top of our program:
byte thisVar; int biggerVar; byte anotherVar; long reallyBigVar;
The microcontroller might assign memory space for those variables something like this (the bits set aside for each variable are color-coded):
On the PIC,we only have bit size, byte size, and word-sized variables, so we might envision a grid like this:
switch1Var var bit switch2Var var bit switch3Var var bit thisVar var byte thatVar var word
Note that the space after the bit variables isn't filled by the next byte variable. The byte variable starts at the next byte in memory.
|For more on how memory is arranged in the PIC, see the notes on special function memory registers.|
|When you refer to the variable, the microcontroller checks to see what's in those bits, and gives them to you.
So if a bit can be only 0 or 1, how do we get values greater than 1?
When we count normally, we count in groups of ten. This is because we have ten fingers. So to represent two groups of ten, we write "20", meaning "2 tens and 0 ones". This counting system is called base ten, or decimal notation. Each digit place in base ten represents a power of ten: 100 is 102, 1000 is 103, etc.
Now, imagine we had only two fingers. We might count in groups of two. We'll call this base two, or binary notation. So two, for which we write "2" in base ten, would be "10" in base two, meaning one group of two and 0 ones. Each digit place in base two represents a power of two: 100 is 22, or 4 in base ten, 1000 is 23, or 8 in base ten, and so forth.
Any number we represent in decimal notation can be converted into binary notation by simply regrouping it in groups of two. Once we've got the number in binary form, we can store it in computer memory, letting each binary digit fill a bit o memory. So if the variable myVar from above were equal to 238 (in decimal notation), it would be 11101110 in binary notation. The bits in memory used to store myVar would look like this:
|There are three notation systems used in the BASIC to represent numbers: binary, decimal, and hexadecimal (base 16).In Hexadecimal notation, the letters A through F represent the decimal numbers 10 through 15. Furthermore, there is a system of notation called ASCII, which stands for American Standard Code for Information Interchange, which represents most alphanumeric characters from the romanized alphabet as number values. We'll deal with ASCII when we get to serial communication. For more, see this online table representing the decimal numbers 0 to 255 in decimal, binary, hexadecimal, and ASCII. While we'll work mostly in decimal notation, here are times when it's more convenient to represent numbers in ms other than base 10.
To represent a number in binary notation on the BX-24, use this notation:
myVar = bx1010_0011 ' 163 in decimal
On the PIC, the notation is as follows:
myVar = %10100011
In hexadecimal, use this notation:
myVar = &HA3 ' 163 in decimal
On the PIC:
myVar = $A3
In Wiring syntax for Wiring or Arduino, we only have decimal and hexadecimal formats. To write a number in hex format, write it like this:
myVar = 0xA3;
Variables can be local to a particular subroutine if they are declared in that subroutine. Local variables can't be used by subroutines outside the one that declares them, and the memory space allotted to them is released and the value lost when the subroutine ends. Variables can also be global to a module, in which case they are declared at the beginning of the module, outside all subroutines. Global variables are accessible to all subroutines in a module, and their value is maintained for the duration of the program. Usually you use global variables for values that will need to be kept in memory for future use by other subroutines, and local variables as a "scratch pad" to store values while calculating within a subroutine.
PicBasic Pro variables are all global in scope.
Doing Arithmetic With Variables
There are certain symbols you'll need to do math in a program. They're pretty much the same symbols as you use in other programming languages:
Data Type Conversions
On the PIC, when you're adding, subtracting, multiplying, or dividing values stored in variables, you can mix data types, as long as whatever variable holds the result is big enough. So you need to use data types that can fit the results you expect. For example:
sensorVar var byte counterVar var byte bigVar var word bigVar = 500 sensorVar = 200 counterVar = 70
sensorVar = sensorVar + counterVar
The result is 270, which is more than can fit in a byte. What happens in this case is that the variable rolls over. If we put 256 in the variable, it reads as 0, 257, reads as 1, and so forth. Counting this way, 270 would read as 14.
sensorVar = bigVar - 10
The result is 490. This doesn't fit in a byte variable, so the result will roll over. The result would read as 235.
In BX-Basic, when you're adding, subtracting, multiplying, or dividing values stored in variables, you must make sure that the variables you're operating on are of the same type. For example:
dim someVar as byte dim anotherVar as byte dim smallVar as byte dim bigVar as integer dim yetAnotherVar as integer Sub main() call delay(0.5) ' start program with a half-second delay someVar = anotherVar + smallVar ' allowed, because all three ' variables are data type byte somevar = anotherVar * 3 ' allowed, because the BX-24 ' interprets 3 as a byte someVar = anotherVar + bigVar ' not allowed, because ' bigVar is an integer, while ' anotherVar and someVar are bytes someVar = anotherVar - 568 ' not allowed, because 568 ' is larger than a byte yetAnotherVar = bigvar + 568 ' allowed, because 568 will fit ' in an integer variable yetAnotherVar = bigvar * someVar ' not allowed, because someVar ' is a byte and the other two ' variables are integers end sub
There are conversion functions to convert data types. For example:
bigVar = cInt(someVar) ' sets bigVar = an integer ' of equal value to someVar's value debug.print cStr(65) ' converts value to ASCII bytes, ' the character "6" and the character "5"
See the BX-24 system library for all the conversion functions.
In addition to variables, every programming language also includes constants, which are simply variables that dont change. Theyre a useful way to label and change numbers that get used repeatedly within your program. For example, in chapter 6 youll see an example program that runs a servo motor. Servo motors have a minimum and maximum pulse width that doesnt change, although each servos minimum and maximum might be somewhat different. Rather than change every occurrence of the minimum and maximum numbers in the program, we make them constants, so we only have to change the number in one place.
In PicBasic Pro, constants are declared at the beginning of your program, like so:
MinPulse con 100
Then you can refer to them in the program just like you do variables, like so:
PulseWidth = minPulse + angleVar
In BX Basic, we also have to declare the type of the constant, like so:
Const minPulse as single = 0.001
You dont have to use constants in your programs, but theyre handy to know about, and you will encounter them in other peoples programs.
In Wiring/Arduino, you declare constants using a function called define:
#define LEDpin 3 #define sensorMax 253
Note that defines are always preceded by a #, and are don't have a semicolon at the end of the line. Defines always come at the beginning of the program. They actually work a bit like aliases. What happens is that you define a number as a name, and before compiling, the compiler checks for all occurrences of that name in the program and replaces it with the number. This way, defines don't take up any memory, but you get all the convenience of a named constant.