MYLABLE:'This sets the label
HIGH 2 ' Turn pin 2 on
PAUSE 1000 'Pause for about 1 sec
LOW 2 'Turn pin 2 off
PAUSE 1000 'Pause for about 1 sec
GOTO MYLABLE 'Sends it back up to top of loop
'BX basic is a little different for the main loop
Sub main()
Do
Statement1
Statement2
Loop
End sub
To make a loop you first have to understand labels. You can label a part of your program just by putting the label name followed by a colon. The first line in the example set the label MYLABLE. You can call your labels anything you like. Anything following the single quote mark is considered a comment which BASIC ignores. The lines between the label and the goto are repeated. The last line sends the program back up to the label MYLABLE and starts the loop over.
‘{$PBASIC 2.5]
If IN6 = 1 then
Statement1
Endif
'BX- BASIC:
If getPin(6) = 1 then
Statement1
End if
Before version 2.5 Pbasic had a less convenient form for their if statement. There was no endif. You had to put the commands you wanted to happen into a routine and point to the routine from the if statement. The new form allows you to put the commands you want to happen right under the if statement. The new way is much better. You might see the old style used in older examples and application notes on the Web. All of our examples are written in Pbasic 2.5, so you should always insert the following line at the beginning of your programs:‘{$PBASIC 2.5]
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. Your if statements and loops can then find the coffee cups by name and take different actions, depending on what they find inside. The real power comes not from the fact that you can place things in the cup, but that you can replace them or “vary” them easily. That’s why they are called variables and not memorables.
Before you use a variable in BASIC, 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 you’re using the variable to remember, because it will make your code much more readable. We often add “Var” to the ends of our variable names as well, so they’re easy to distinguish from reserved words 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 isn’t a keyword. When you try to run your program, the compiler will let you know if your variable name isn’t 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:
DateVar = 12 ticketValueVar = 250 FareVar = 125Type of Variable: In higher- level languages, like Lingo in Macromedia' Director MX or ActionsScript in Macromedia Flash MX, you can put all kinds of information into your variables: strings of text, integers, fractional numbers, and more. The type of data you will store in that variable will automatically be interpreted from the context in which you use the variable, and the variable will be given more space in memory dynamically, on an as-needed basis. In contrast, microcontroller operating systems are a little more bare bones and memory space is a little tighter. As a result, when you declare your variables, you also have to specify how to interpret the data you plan to store, and how much storage space you need. Variables are declared in your program like this:
SensorVar var byte TicketVar var byte BiggerVar var word 'BX- BASIC: Dim ticketVar as byte Dim biggerVar as integer
The size of your variable and the way it’s interpreted is called the data type of the variable, and it’s largely determined by how big a number you need to store. For example, if you plan to put one of only two values (0 or 1) into your variable, it will fit in the smallest unit of memory space, called a bit. A number between 0 and 255 will require 8 bits of memory, which is called a byte. You’ll need even more space if the ranges of values you are storing need to be interpreted as fractions or with negative numbers. Beginner’s’ programs rarely run short on memory space , so when in doubt, knock your self out and use big data types.
It’s important that you understand types of variables so that you’re able to use them with the microcontroller’s built-in functions, which we will discuss below. Often, you’ll be supplying variables as parameters for these built-in functions, or having them return information by placing it in variables. You have to match the variable types that you use to those that the built-in functions expect. For example, the RCTIME rctime function on the BASIC Stamp expects a word-sized variable, and the PULSEOUT Ppulseout() function on the BX expects to be supplied with a type called a single. . The type of variable expected by a function is usually the first thing item specified in the documentation for a given function. Sometimes you’ll need to use the same variable with two functions that have conflicting types, in which case you’ll have to convert the variable to another type using a type conversion function.
Various forms of BASIC use different data types and different names for the same types. Listed below are the types for each language, and how much space they take in memory, and what the range of possible values are.:
PBASIC: Type Number of bBits Range of vValues Bit 1 1 or 0 Nib 4 0 to 15 Byte 8 0 to 255 Word 16 0 to 65535 MBASIC: Type Number of bBits Range of vValues Bit 1 1 or 0 Nib 4 0 to 15 Byte 8 0 to 255 SByte 8 –-127 to +128 Word 16 0 to 65535 SWord 16 -–32,767 to +32,768 BX -BASIC: Type Number of bBits Range of vValues Boolean 8 True or false Byte 8 0 to 255 Integer 16 -–32,768 to 32,767 Long 32 -–2,147,483,648 to 2,147,483,647 Single 32 -–3.402823 E+38 to 3.402823 E+38 String Varies 0 to 64 bytes PICBASIC PRO: Type Number of bBits Range of vValues Bit 1 1 or 0 Byte 8 0 to 255 Word 16 0 to 65535
Not all of the microcontrollers we’re using you’ll use we'’re showing allow you to use negative numbers or fractional numbers. You might think about whether you need this when choosing one. Those that can store negative numbers use one of the bits to store the sign of the number. In MBasic, for example, they refer to the data types that can store negative numbers as swWord and sByte, short for “signed word” and “signed byte.”. BX- Basic is the only environment we’re discussing that can store fractional numbers, in its Single type.
[Add to or Correct This Page]|[Yes, this is a Wiki]