Shop OBEX P1 Docs P2 Docs Learn Events
Must all Strings be define in array — Parallax Forums

Must all Strings be define in array

DosEdgeDosEdge Posts: 33
edited 2007-02-02 21:56 in BASIC Stamp
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?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-02 16:06
    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.
  • DosEdgeDosEdge Posts: 33
    edited 2007-02-02 19:27
    So Mike, are the codes below exceptable:

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

    or

    #DEFINE vName = "yes"
  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-02 19:58
    Neither of these are acceptable. You can initialize vName a byte at a time with:
    vName[noparse][[/noparse] 0] = "y"
    vName[noparse][[/noparse] 1] = "e"
    vName[noparse][[/noparse] 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[noparse][[/noparse] i]
      next
    


    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
    askAgain:
      debug "Enter answer: "
      debugin str vName\4\13 ' up to 4 bytes, ended with return
      i = 0
    compare:
      read vYes+i,temp
      if temp <> vName[noparse][[/noparse] i] then askAgain
      if temp = 0 then matchedIt
      i = i + 1
      goto compare
    matchedIt:
    
    


    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.
  • DosEdgeDosEdge Posts: 33
    edited 2007-02-02 20:00
    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
    ReadCurrentTemp:
  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-02 20:07
    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.
  • DosEdgeDosEdge Posts: 33
    edited 2007-02-02 20:17
    I really appreciate the help Mike!!! I understand the concept now.

    Thank You Mike,

    DosEdge
  • DosEdgeDosEdge Posts: 33
    edited 2007-02-02 21:16
    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.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-02-02 21:35
    "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
    ReadCurrentTemp:
    "

    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)

    MAIN:
    · 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
    ··· ENDIF
    · NEXT

    ' Match! So:
    GOSUB ReadTemp

    MisMatch:
    · GOTO MAIN


    ReadTemp:
    · RETURN


    Post Edited (allanlane5) : 2/2/2007 9:45:35 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-02 21:40
    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.
  • DosEdgeDosEdge Posts: 33
    edited 2007-02-02 21:56
    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,

    Dosedge
Sign In or Register to comment.