View Full Version : defining arrays

04-19-2007, 01:34 AM
is it possible to define a whole array data set like in C++ it is


04-19-2007, 01:53 AM
I don't think so, but I could be mistaken.

You could do:

MyArray VAR BYTE(12) ' 0..11

DATA 1, 34, 5, 6, 4, 3, 2, 4, 6, 7, 4, 4

for i = 0 to 11
READ i, MyArray(I)

Note that would take up half your RAM memory locations, though.

Mike Green
04-19-2007, 02:04 AM
What allanlane5 is implying is that there are arrays in PBasic, but they cannot be automatically initialized. They're set to zero as are all variables when the Stamp is reset (like at power on). They're also very limited in that there is a grand total of 26 bytes (or 13 words) of variable space.

You can store data in the EEPROM used to store your program and you can even compile in initial data using the DATA statement. The READ/WRITE (and STORE) statements are used to access these data areas. The posted example shows a data area in EEPROM being copied into an array called MyArray. You don't have to move the whole array to variables. You can process it one value at a time. Read the sections in the PBasic manual on READ/WRITE and keep in mind that EEPROM is limited to about 100000 write operations on any one "page" of about 32 to 64 bytes. The affected locations will "wear out" eventually and become unable to be written correctly.

04-19-2007, 02:27 AM
how would i do this if i wanted two arrays like that?

04-19-2007, 03:05 AM
1. Read the manual under "DATA".

2. Then:

' {$STAMP BS2}
' {$PBASIC 2.5}
MyArray VAR Byte(12) ' 0..11
MyArray2 VAR Byte(12) ' 0..11

MyData1 DATA 1, 34, 5, 6, 4, 3, 2, 4, 6, 7, 4, 4
MyData2 DATA 1, 34, 5, 6, 4, 3, 2, 4, 6, 7, 4, 4

FOR i = 0 TO 11
READ MyData1+i, MyArray(I)
READ MyData2+i, MyArray(I)

But -- you've now used all your 'VAR' space for arrays -- you have no RAM left over to process with.
Why do you want to do this?

04-21-2007, 01:30 AM
Well im trying to create a keyboard but i want a more efficiant way to define the keys instead of having to use serout to my lcd screen for each letter.

04-21-2007, 01:51 AM
Well, it sounds like "DATA" statements should be fine for this.

Though, you mention "Keyboard", then "using serout to my LCD screen" to "define the keys". So I don't understand what's going on.

04-21-2007, 01:58 AM
I mean that i could have my program like this:

SEROUT Lpin, Baud19200, ["a"]
goto main
SEROUT Lpin, Baud19200, ["b"]
goto main

but i was hoping to use an array instead so i can just replace with the "a" or "b" with this:
SEROUT Lpin, Baud19200, [row1(hitkey)]

04-21-2007, 04:00 AM
Yeah, I think you'd use the "LOOKUP" keyword to embed in your PBasic code the translation. "Use an array" is how you'd implement that in a language with WAY more RAM memory.

05-01-2007, 02:41 AM
Can you please give me an example?

Mike Green
05-01-2007, 02:58 AM
Please look in the PBASIC manual under LOOKUP first. Most of the statements have multiple examples and quite a bit of description, particularly the more complex statements like LOOKUP. If there's something you don't understand after reading the manual and trying it, show us what you've tried and we'll try to help.