Electronics Projects: How to Create Constants in PBASIC Code

If you begin to use programmable circuits in your electronics projects, you’ll end up writing some code. If your programmable circuit is a BASIC Stamp, you’ll write that code in PBASIC.


In PBASIC, you can create your own names to use as constants. A constant is a name that has been assigned a value. This allows you to use the constant name in your program rather than the value itself. Later, if you decide to change the value, you don’t have to hunt through the program to find every occurrence of the constant. Instead, you simply change the line that defines the constant.


Here’s a statement that creates a constant named Delay and assigns the value 500 to it:


Delay CON 500

The CON keyword indicates that Delay is a constant whose assigned value is 500.


To use a constant, just substitute the name of the constant wherever you would use the value. For example, this line pauses the program for the value assigned to the Delay constant:


PAUSE Delay

Listing 2-3 shows a version of the LED Flasher program that uses a constant to determine how fast the LEDs should flash.


' LED Flasher Program
' Doug Lowe
' July 10, 2011
'
' This program flashes LEDs connected to pins 0, 2, 4, 6, 8, and 10
' at one-half second intervals.
'
' This version of the program uses a constant
' for the time interval.
' {$PBASIC 2.5}
' {$STAMP BS2}
Delay CON 500
Main:
HIGH 0
HIGH 2
HIGH 4
HIGH 6
HIGH 8
HIGH 10
PAUSE Delay
LOW 0
LOW 2
LOW 4
LOW 6
LOW 8
LOW 10
PAUSE Delay
GOTO Main

You must follow a few simple rules when you create names in PBASIC:



  • Names can consist of a combination of upper- and lowercase letters, numbers, and underscore characters (_). Other special characters, such as dollar signs or exclamation marks, aren't allowed. Thus, Timer_Routine and Relay7 are valid names, but LED$ or Bang! aren't.



  • Names must begin with a letter or an underscore but can't begin with a number. Thus, Timer1 and _Timer1 are both valid names, 1Timer isn't.



  • Names may be as long as 32 characters.



  • Names aren't case-sensitive, which is to say that PBASIC doesn't distinguish between upper- and lowercase letters. Thus, PBASIC considers all of the following names to be identical: TimerCheck, timercheck, TIMERCHECK, and TiMeRcHeCk.



  • Actually, nothing in PBASIC is case sensitive, so anything can be written in upper- or lowercase. However, it is a common PBASIC programming convention that keywords such as HIGH and GOTO are written in all caps, while names are written with just the first letter capitalized.













dummies

Source:http://www.dummies.com/how-to/content/electronics-projects-how-to-create-constants-in-pb.html

No comments:

Post a Comment