Search This Blog

Friday, July 23, 2010

Robotics Tutorials - Beginner - Programming Variables

Variables are values stored in computer memory. We need to use them for almost every program to allow the Robocore to remember things. There are various different types of values, for example strings, which are text values, and integers, which are whole number values.

The computer needs you to tell it what type of value you want to use, otherwise it will get confused.

Each type of variable has its uses and limitations, which is why there are simple methods for switching between variable types. This was shown in the last tutorial, lets look at that program again line by line.

Sub main()
Dim answerI As integer ' declaring
Dim answer As string ' variables

The Dim lines declare the variables, that means they set them up for use in the program. The type of variable must be specified using the ' As ' keyword. Here we have an integer and a string.

answerI = 5*5 ' doing the math

Here the integer variable is used, this is because mathematical functions can be performed using it such as multiplication

answer=Cstr(answerI) 'convert to printable format

Using the CStr statement the integer value held in answerI is converted into a string and held in answer, this is a text variable allowing the answer to be printed to the screen using debug.print.

debug.print "5 times 5 is "; answer 'print answer
End Sub

Variables can have nearly any name, but you cannot use words that are otherwise used in the programming language, and it is best to keep them simple, either a single letter, or a word describing what the variable does.

Other types of variables are available. One of the types used commonly in robotics is the Boolean variable. This provides us with a true or false value.

For example a bump sensor will either be pressed or not pressed, so the Boolean variable for it will either be true (pressed) or false.

We set up Boolean variables using the following statement

Dim variable As Boolean.
variable = true

Boolean logic is commonly used with conditional statements, these are covered in the next section.

There is also the Single variable type, which allows floating point numbers e.g 2.43 or -0.66, this is occasionally useful in robotics when more maths is necessary.

No comments:

Post a Comment