BS2 - Tips for Quicker Array Writing

olefalconolefalcon Posts: 2
edited September 30 in BASIC Stamp Vote Up0Vote Down
From the official BASIC Stamp Help document, it seems the most basic way of writing to an array is like this:
myArray VAR Bit(30)

myArray(0) = "A"
myArray(1) = "B"
myArray(2) = "G"

I know there is also a loop you can use to print the same thing to many slots of the array like this:
myArray VAR Bit(30)
i VAR Bit

FOR i = 0 TO 9
  myArray(i) = "B"
NEXT

I am wondering how I would go about forming a function that takes a list or string of values and writes them into an array. In javascript it is easy to write into an array like this:
var array[] = ["A", "B", "G"]

If you have upwards of twenty or thirty values you want to write into an array, you really don't want to type "myArray(0) =" every time you have a value to add.

If string handling is a solution, the values I am using are bit-sized (1 unit long), so a string like "AGAB" could become "A", "G", "A", "B", but I don't know how to do that.

Thanks for any help!

Oh and I am using BS2 with PBASIC 2.5

I reedited the post showing that the array can actually be bit datatypes, so storage is NOT the concern here!

Comments

  • 7 Comments sorted by Date Added Votes
  • The BASIC Stamp does not support strings the way you're attempting to use them. You can store strings as DATA and access them using READ.
    Jon McPhalen
      *It's "Jon" or "JonnyMac" -- please don't call me "Jonny"
  • PBASIC doesn't really support strings so much as arrays of characters. Your declaration of myArray defines an array of 10 bytes, each of which can hold a single character. There's very limited space for variables (13 words or 26 bytes), so you can't store too many characters.

    Some of the Stamp models have "scratchpad RAM" which can hold more, again as byte values, but not as conveniently as variables.
  • I reedited the post showing that the array can actually be bit datatypes, so storage is NOT the concern here!
  • What is it that you're trying to do? Remember that the Basic Stamps work with 16 bit integers as their native type. There are ways to declare and manipulate 8 bit byte variables, 4 bit nibs, and single bits in order to conserve the limited variable storage. Strings are there mostly for use as literals in output statements. They can be stored along with the program as constants, but really are treated as arrays of bytes using the DATA and READ statements.
  • JonnyMacJonnyMac Posts: 5,801
    edited October 1 Vote Up0Vote Down
    I'm with Mike; it would be easier to help if you stated your end goal.

    Since that is not known, I thought I'd post a working BS2 program that uses string constants. Note that these are actually stored in EEPROM using the DATA directive. You'll also find that some string constants are embedded into SEROUT commands.
    ' =========================================================================
    '
    '   File....... uMP3_Demo.BS2
    '   Purpose....
    '   Author..... JonnyMac
    '   E-mail.....
    '   Started....
    '   Updated....
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    ' =========================================================================
    
    
    ' -----[ Program Description ]---------------------------------------------
    
    
    ' -----[ Revision History ]------------------------------------------------
    
    
    ' -----[ I/O Definitions ]-------------------------------------------------
    
    
    Sio             PIN     15                      ' for DC-16, RC-4; no ULN
    TX              PIN     14                      ' to UMP3.R; no ULN
    RX              PIN     13                      ' to UMP3.T; no ULN
    Trigger         PIN     12                      ' setup = DN
    
    
    ' -----[ Constants ]-------------------------------------------------------
    
    #SELECT $STAMP
      #CASE BS2, BS2E, BS2PE
        T2400       CON     396
        T4800       CON     188
        T9600       CON     84
        T19K2       CON     32
        T38K4       CON     6
      #CASE BS2SX, BS2P
        T2400       CON     1021
        T4800       CON     500
        T9600       CON     240
        T19K2       CON     110
        T38K4       CON     45
    #ENDSELECT
    
    Inverted        CON     $4000
    Open            CON     $8000
    EfxBaud         CON     Open | T38K4            ' for DC-16, RC-4
    MP3Baud         CON     Open | T9600            ' for uMP3 player
    
    Yes             CON     1
    No              CON     0
    
    
    ' -----[ Variables ]-------------------------------------------------------
    
    char            VAR     Byte                    ' character value
    theMP3          VAR     Byte                    ' MP3 file pointer
    eePntr          VAR     Word                    ' EEPROM memory pointer
    pos             VAR     Word                    ' uMP3 file position
    loopNum         VAR     Byte                    ' file loop # in cycle
    lottery         VAR     Word                    ' random value
    
    
    ' -----[ Initialization ]--------------------------------------------------
    
    Reset:
      PAUSE 2000                                    ' let uMP3 start
    
    Prod_uMP3:
      DO
        SEROUT TX, MP3Baud, [CR]                    ' send CR
        SERIN  RX, MP3Baud, [char]                  ' get response
      LOOP UNTIL (char = ">")                       ' wait for ">"
    
    
    ' -----[ Program Code ]----------------------------------------------------
    
    Main:
      RANDOM lottery
      IF (Trigger = No) THEN Main
    
      theMP3 = lottery // 3                         ' make 0 - 2
      GOSUB Play_MP3                                ' play it
    
      PAUSE 5000
      GOTO Main
    
    
    ' -----[ Subroutines ]-----------------------------------------------------
    
    ' Put file # to play in "theMP3"
    ' -- add DATA label entries to LOOKUP as required
    
    Play_MP3:
      LOOKUP theMP3, [SFX0, SFX1, SFX2,
                      SFX3, SFX4, SFX5], eePntr     ' get base address
    
    Send_Play_Cmd:
      SEROUT TX, MP3Baud, ["PC F /"]                ' start play command
    
    Send_Name:                                      ' send file title
      DO
        READ eePntr, char
        eePntr = eePntr + 1
        IF char = 0 THEN EXIT
        SEROUT TX, MP3Baud, [char]
      LOOP
    
    Finish_Cmd:
      SEROUT TX, MP3Baud, [".MP3", CR]              ' send extention + CR
    
    Wait_For_Stop:                                  ' let song finish
      DO
        GOSUB Get_Status
      LOOP UNTIL (char = "S")
      RETURN
    
    ' -------------------------------------------------------------------------
    
    ' Gets status from uMP3
    ' -- "P" = playing, "D" = paused, "S" = stopped
    ' -- also gets file position and loop number of play cycle
    
    Get_Status:
      SEROUT TX, MP3Baud, ["PC Z", CR]
      SERIN  RX, MP3Baud, [char, DEC pos, DEC loopNum]
      RETURN
    
    
    ' -----[ EEPROM Data ]-----------------------------------------------------
    
    ' MP3 files are stored in root of SD card
    ' Table below needs only name, followed by a zero
    ' Keep names short to conserve EE space
    
    SFX0            DATA    "Hello", 0
    SFX1            DATA    "Wolf", 0
    SFX2            DATA    "Scream", 0
    SFX3            DATA    "Chuckie", 0
    SFX4            DATA    "Exorcist", 0
    SFX5            DATA    "Lightning", 0
    
    Jon McPhalen
      *It's "Jon" or "JonnyMac" -- please don't call me "Jonny"
  • Jon

    That is pretty impressive layout for code. I would imagine that is a standard Stamp template for you.


    Novel Solutions - http://www.novelsolutionsonline.com/ - Machinery Design • - • Product Development
    "Necessity is the mother of invention." - Author unknown.

  • JonnyMacJonnyMac Posts: 5,801
    edited October 1 Vote Up0Vote Down
    idbruce wrote: »
    Jon

    That is pretty impressive layout for code. I would imagine that is a standard Stamp template for you.
    Yes. I'm a very firm believer in using templates to keep things consistently organized. I have them for the BS1 and BS2 (EFX-TEK controllers use them), and I have several templates for the Propeller (due to the various boards I have designed and use).

    Jon McPhalen
      *It's "Jon" or "JonnyMac" -- please don't call me "Jonny"
Sign In or Register to comment.