View Full Version : Must all Strings be define in array

02-02-2007, 05:20 PM
I was trying to declare·a string to a variable.· Then, I wanted to compare that variable to an input from the DEBUGIN command.· I assume that all strings must be define as an array.· You can not initialize a variable in PBasic as you would with any other high level programming language.· Can you use the DEFINE directive and initialize a variable?

Mike Green
02-02-2007, 11:06 PM
With PBasic, all strings are just arrays of bytes. Remember that there are only a few bytes available for variables (26) so general purpose string manipulation is not really practical. If you have one of the Stamps with Scratchpad RAM, you get an additional 126 bytes of memory although it has limited usability since it can only be accessed with the GET/PUT statements and the SPSTR formatter in the SERIN/I2CIN/LCDIN/OWIN statements.

You can't initialize a variable except by storing information in it using an assignment statement.

You can store constant strings in EEPROM that you can declare in your program. Look at the READ/WRITE/DATA statements in the manual. This is a common way to store strings used to interpret input information like from a DEBUGIN statement.

02-03-2007, 02:27 AM
So Mike, are the codes below exceptable:

vName·Var· Byte(3)
vName = "yes"


#DEFINE vName = "yes"

Mike Green
02-03-2007, 02:58 AM
Neither of these are acceptable. You can initialize vName a byte at a time with:

vName[ 0] = "y"
vName[ 1] = "e"
vName[ 2] = "s"

You could also copy the characters from EEPROM with:

initTbl data "yes"
i var nibble
for i = 0 to 2
read initTbl+i,vName[ i]

I don't recommend the above since it uses your very limited
variable space for what is essentially a string constant.
Better to have the string in EEPROM and compare it character
by character to the received DEBUGIN string like:

temp var byte
vName var byte(4)
vYes data "yes",0 ' Note - the zero makes the comparison easier
i var nibble
debug "Enter answer: "
debugin str vName\4\13 ' up to 4 bytes, ended with return
i = 0
read vYes+i,temp
if temp <> vName[ i] then askAgain
if temp = 0 then matchedIt
i = i + 1
goto compare

Note that it's an easy extension of this to have a table of responses and to search through it looking for a match. If there's a match, the index (number) of the matched response can be left in a variable for some use later.

02-03-2007, 03:00 AM
Can someone tell me what is wrong with defining a variable as a string and then comparing it to a string input:

#DEFINE Monitor = yes····· 'defines variable to a string
vMonitor VAR Byte(3)······· 'declares a variable for three characters

DEBUGIN STR vMonitor \ 3··'input three characters through terminal

IF vMonitor = Monitor· THEN 'compare strings

Mike Green
02-03-2007, 03:07 AM
Very simply, there are no string variables in PBasic. None. There is no such thing.

The only kind of variables in PBasic are 16-bit words, 8-bit bytes, 4-bit nibbles, and individual bits. A numeric value can be used to hold a character and an array of numeric values can be used to hold a short string (since there are only 26 bytes of variable space available), but no string manipulation is possible except on a byte by byte explicitly programmed basis. Sorry.

02-03-2007, 03:17 AM
I really appreciate the help Mike!!! I understand the concept now.

Thank You Mike,


02-03-2007, 04:16 AM
Excuse me, Mike! I need to ask you one more thing. Are you familiar with the SLEEP command. If you are familiar with it, could you explain to me the sleep duration part. I understand the BS2 has a resolution of 2.034 per second. I do not understand the command in the manual "SLEEP 10" is approximately 11.52 seconds (5 X 2.034). Are you suppose to take highest factor x 2.034 that come close to ten seconds. For the Sleep command to work for 15 minutes, you will have to take in consideration of 900 seconds is (450 X 2.034) 915.3 seconds. Will SLEEP 900 approximate to 15 minutes.

02-03-2007, 04:35 AM
"Can someone tell me what is wrong with defining a variable as a string and then comparing it to a string input:

#DEFINE Monitor = yes 'defines variable to a string
vMonitor VAR Byte(3) 'declares a variable for three characters

DEBUGIN STR vMonitor \ 3 'input three characters through terminal

IF vMonitor = Monitor THEN 'compare strings

Well, actually, there IS some "string" handling stuff -- mostly the 'STR' modifier.

' {$STAMP BS2}
' {$PBASIC 2.5}
TempByte VAR Byte
I VAR Byte

String1 DATA "YES" ' Stores the characters "Y", "E", "S" in EEPROM, storing the "Y" in String1(0).

vMonitor VAR Byte(4) ' Declares 3 byte 'byte array' as vMonitor(0), vMonitor(1), vMonitor(2)

· DEBUGIN STR vMonitor\3 ' Should read in a 3-character string into vMonitor 0..2, and put null in vMonitor(3).
· ' Not sure if it really does this though...
· FOR I = 0 TO 2
··· READ String1+I, TempByte
··· IF TempByte <> vMonitor(I) THEN
····· GOTO MisMatch

' Match! So:
GOSUB ReadTemp



Post Edited (allanlane5) : 2/2/2007 9:45:35 PM GMT

Mike Green
02-03-2007, 04:40 AM
You are supposed to divide the amount of time you want to wait in seconds by 2.304 and that is the operand of the SLEEP statement. For 15 minutes, you have 15 * 60 seconds = 900 seconds. Dividing by 2.304, you get 390.625. The closest integer is 391 so you use "SLEEP 391". The actual wait time will be about 15 minutes plus 1 second with an error of up to 1% (about 4 seconds plus or minus). Don't forget that the SLEEP statement lets the I/O pins float every 2.3 seconds. If you need more accurate timing or no floating of the I/O pins, use the PAUSE command. If you need more than 60 seconds' delay, you can use multiple PAUSE statements. This does use more power.

02-03-2007, 04:56 AM
I really appreciate the help, Mike! I will not bother you no more this weekend! I will reserve all my question for you first thing Monday morning! Have a nice weekend. Until later, peace!

Thank you Mike and allanlane5,