16 variables in pBasic?
joneschj
Posts: 6
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.·· Of course I am new to this whole thing so tell me how to fix it.· ··
joneschj
Post Edited (joneschj) : 3/20/2009 5:02:47 AM GMT
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.·· Of course I am new to this whole thing so tell me how to fix it.· ··
joneschj
Post Edited (joneschj) : 3/20/2009 5:02:47 AM GMT
Comments
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.
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
··· ' 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
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
joneschj
Thanks guys, you have restored my faith in the BS2!!·
joneschj
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