Shop OBEX P1 Docs P2 Docs Learn Events
Array of constant on BS2? — Parallax Forums

Array of constant on BS2?

MCAMCA Posts: 13
edited 2014-05-23 14:14 in BASIC Stamp
I would like to make an array of constants on my BS2. The reason is I wish to save ram - don't need a variable, but I do want to be able to loop the values of the array. Is it possible to declare an array of constants in Pbasic? From what I can tell it is not.

If not, is there another way to do this smoothly?

BR/ MCA

Comments

  • SapphireSapphire Posts: 496
    edited 2014-05-16 14:06
    You could put them in a DATA statement and retrieve them with a READ statement
    DATA 10,20,30,40,50,60,70,80
    
    X VAR BYTE
    Y VAR BYTE
    
    FOR X = 0 TO 7
      READ X,Y
      DEBUG DEC X, TAB, DEC Y, CR
    NEXT
    

    The DEBUG is just to show you how the READ works.

    You probably wouldn't read them in a loop in your program; instead just READ the one you want when you need it.
    READ 5,Y
    
    
  • MCAMCA Posts: 13
    edited 2014-05-19 06:11
    Thanks, that might be useful!

    BR / MCA
  • ZootZoot Posts: 2,227
    edited 2014-05-23 14:14
    Here is a pseudo "constant" array that will take up code space but no ram.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    
    const1 CON 1
    const2 CON 2
    const3 CON 3
    const4 CON 4
    ' ... etc
    
    tmpN1 VAR NIB ' work nib
    tmpB1 VAR BYTE ' work variable; you should always have at least one!
    tmpW1 VAR WORD ' ditto
    
    
    ' pass the index value of the constant you want (say, 0-3) and get the value back in tmpW1
    Get_Const_By_Index:
        LOOKUP tmpN1, [ const1, const2, const2, const4 ], tmpW1
        RETURN
    
    ' get the constant whose value is just greater than the passed "test" value
    ' presumes constants are arranged in ascending order
    Get_Const_By_LT_Val:
        LOOKDOWN tmpN1, <[ const1, const2, const2, const4 ], tmpN1
        GOSUB Get_Const_By_Index
        RETURN
    
    ' etc
    
    
Sign In or Register to comment.