Shop OBEX P1 Docs P2 Docs Learn Events
16 variables in pBasic? — Parallax Forums

16 variables in pBasic?

joneschjjoneschj Posts: 6
edited 2009-03-20 07:00 in BASIC Stamp
Hello everyone, I keep getting my BS2 hooked up and then begin writing code but find I keep running into limitation on the number of variables I can have at a time.· I am attempting to run a Memory Stick Data Logger, a temp probe, and a temp and humidity IC.· The combination of these and the need to do conversions on my data is eating up the number of variables.· Is there a hard limit to the number of variables that I can use within one pBasic file and is there a way around this?

I have thought about using an array for each device to handle all related variables that I need, this would probably work for some of them only though as data types will be in conflict eventually.· I have also though it would be neat to use very generic variables that are written to the data logger, then wiped out and reused mid code, and when the original value is needed again I just retrieve it from the data·logger.· Although I think this would eat up most of my variables just building something that could handle this in the first place.

·
This is by far the biggest disappointment to date with the BS series that I have found.·cry.gif· Of course I am new to this whole thing so tell me how to fix it.· tongue.gif··

joneschj

Post Edited (joneschj) : 3/20/2009 5:02:47 AM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-20 04:14
    There is a hard limit to the amount of variable storage available. This can be allocated (and accessed) as 13 x 16 bit words, 26 x 8 bit bytes, 52 x 4 bit nibbles, or 208 individual bits or combinations of different sizes. You can have arrays of these storage units, but there's only 13 words overall available. Some of the Stamp models have amounts of "scratchpad ram" available which is accessed using the GET / PUT statements, but not as declared variables.

    Parallax Basic allows you to redefine variables with different names and even different sizes, so a given byte might be accessed as two nibbles with different names. This is how people get around the shortage of variable storage ... they reuse it with useful names in different parts of their code.

    The processor used to make the BS2 doesn't have much memory available and some of it has to be used to support the Basic interpreter itself and its I/O functions.
  • joneschjjoneschj Posts: 6
    edited 2009-03-20 04:40
    Doing a little read on this... The Get and Put commands appear to only be available in the BS2P and up, so for me that is out; I just have the BS2.

    Your other point seems to imply that you can clear the memory of a previously declared variable for use as a new variable. Care to elaborate I cannot see how to do this in the book or help. This would seem to be fine, but would mean that each section of code would require you to declare (and possibly re-declare) your variables with each pass. I am used to declaring all of my variables at the top of the code module; looks like this are going to change for me.

    Thanks for the quick response Mike; you are sure on top of these posts. (10,927 posts to date) WOW!!

    joneschj
  • joneschjjoneschj Posts: 6
    edited 2009-03-20 04:58
    Got it:
    ··· ' Create a byte-sized variable, then create an alias
    ··· cat VAR Byte
    ··· tabby VAR cat

    Does this clear the original value of the byte?

    I am going to do some testing and report back...

    '--Test CODE--'
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    cat VAR Word
    cat = 100
    DEBUG "Cat = ", DEC cat, CR

    dog VAR cat
    DEBUG "Dog = ", DEC dog, CR

    END
    '--End Code--'

    Debug Returns:
    Cat = 100
    Dog = 100

    Qoute from the help files:
    "In this example, tabby is an alias to the variable cat. Anything stored in cat shows up in tabby and vice versa. Both names refer to the same physical piece of RAM. This kind of alias can be useful when you want to reuse a temporary variable in different places in your program, but also want the variable's name to reflect its function in each place. Use caution, because it is easy to forget about the aliases; during debugging, you might end up asking 'How did that value get here?!' The answer is that it was stored in the variable's alias."

    So the variables are maintained in each location, but use the same memory location in the BS.· Okay this makes sense, but I would still like to change its value to 0 or null prior to reuse within my code.· Guess I will just have to program carefully around this.

    joneschj

    Post Edited (joneschj) : 3/20/2009 5:08:58 AM GMT
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-03-20 05:01
    Hi , this is a snippet from the PBasic IDE help file which uses the same variable space with two different labels. Providing you don't need to retain the value of these variables you can just initialize them (tabby=a_value) before each use. The help file has further examples.

    cat     VAR     Byte                    ' Create a byte-sized variabletabby   VAR     cat                     ' Create alias for cat
    In this example, [i]tabby[/i] is an alias to the variable [i]cat[/i]. Anything stored in [i]cat[/i] shows up in [i]tabby[/i] and vice versa. Both names refer to the same physical piece of RAM. This kind of alias can be useful when you want to reuse a temporary variable in different places in your program, but also want the variable's name to reflect its function in each place. Use caution, because it is easy to forget about the aliases; during debugging, you might end up asking 'How did that value get here?!' The answer is that it was stored in the variable's alias.
    
    

    Might also pay to check if any "variables" have a constant value, declaring constants does not use variable space.

    Jeff T.

    EDIT : you beat me to it !!! , good deal
  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-20 05:07
    The entire variable space (26 bytes) gets cleared to zeros on a reset (including power up). Redefining the name of the variable (aliasing) doesn't affect the value of the variable. To clear a variable, you just assign a zero to it (var = 0). You don't need to clear a variable unless your program requires it.
  • joneschjjoneschj Posts: 6
    edited 2009-03-20 05:09
    Thanks Unsoundcode. Hey how did you get that nifty box around your qoute and code?

    joneschj
  • joneschjjoneschj Posts: 6
    edited 2009-03-20 05:11
    Got it:

    The little # sign...
    

    Thanks guys, you have restored my faith in the BS2!!· turn.gif

    joneschj
  • W9GFOW9GFO Posts: 4,010
    edited 2009-03-20 07:00
    Also, you could create a variable who's sole purpose is to be reused throughout the program.

    Many of the examples you'll find use "workVal" as a variable. I couldn't make my 14 waypoint GPS autopilot on a BS2 without it! Only 14 because I ran out of memory space to store more.

    Rich H
Sign In or Register to comment.