PDA

View Full Version : defining arrays



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

myarray=[1,34,5,6,4,3,2,4,6,7,4,4]

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

You could do:

MyArray VAR BYTE(12) ' 0..11
I VAR NIB

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

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

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

Mike Green
04-19-2007, 03: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.

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

allanlane5
04-19-2007, 04: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
I VAR Nib

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)
NEXT

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?

jasonpeinko
04-21-2007, 02: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.

allanlane5
04-21-2007, 02: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.

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

a:
SEROUT Lpin, Baud19200, ["a"]
goto main
b:
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)]

allanlane5
04-21-2007, 05: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.

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

Mike Green
05-01-2007, 03: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.