Tom's Main Menu

Physical Computing Home

Intro to Physical Computing Syllabus

Networked Objects

Sustainable Practices

blog

Resources

code, circuits, & construction

my del.icio.us links

 

More on Variables

 
As mentioned in the BX-24 programming notes, variables are storage locations in the BX-24's memory for changing information. Every variable has a data type. The data type of a variable determines how much memory the BX-24 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.

Here are the data types, broken down by memory needed:

Data Type Memory needed Description Range Example
Boolean 8 bits a true or false value True or False thisVar = true
Byte 8 bits a positive integer number 0 to 255 numVar = 45
Integer 16 bits also an integer number. Integer variables can store a greater range of values than Byte variables, however, and part of that range can be negative. -32 768 to 32 767 potVar = 4857
Long 32 bits also an integer number, but a greater range than Byte or Integer variables. Can also be positive or negative. -2,147,483,648 to 2,147,483,647 starsVar = 65535
Single 32 bits a floating-point number (i.e. a number with a fractional part, like 2.5, 3.14159, etc.). Note that all floating point numbers must have a decimal, even if they have no fractional part. E.g. 4.0 instead of just 4. -3.402,823 E+38 .. 3.402,823 E+38 angleVar = 4.5
String Varies an array of bytes representing alphanumeric characters. 0 to 64 bytes (characters) myStringVar = "Hello"
 
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. 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. The BX-24 has 400 bytes of memory for variable storage. Since a byte is eight bits, that means that there are 8 x 400, or 3200 bits of memory. We can envision a grid that represents that memory like this (this grid is only a part of the whole 3200):

bit0 bit1 bit2 bit3 bit4 bit5 bit6 bit7
bit8 bit9 bit10 bit11 bit12 bit13 bit14 bit15
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -

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 BX-24 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:

dim thisVar as byte
dim biggerVar as integer
dim anotherVar as byte
dim reallyBigVar as long

The BX-24 might assign memory space for those variables something like this (the bits set aside for each variable are color-coded):

myVar
-
-
-
-
-
-
myVar
integerVar
-
-
-
-
-
-
-
-
-
-
-
-
-
-
integerVar
anotherVar
-
-
-
-
-
-
anotherVar
reallyBigVar
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
reallyBigVar

When you refer to the variable, the BX-24 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:

1
1
1
0
1
1
1
0

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

In hexadecimal, use this notation:

myVar = &HA3	' 163 in decimal

Variable scope

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.

Doing Arithmetic With Variables

There are certain symbols you'll need to do math in BASIC. They're pretty much the same symbols as you use in other programming languages:

Addition +
Subtraction -
Multiplication *
Divide (float) /
Divide (integer) \
Modulus Mod
Absolute value Abs

 

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 system library for all the conversion functions.