Shop OBEX P1 Docs P2 Docs Learn Events
defining arrays — Parallax Forums

defining arrays

jasonpeinkojasonpeinko Posts: 20
edited 2007-04-30 19:58 in BASIC Stamp
is it possible to define a whole array data set like in C++ it is

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

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-18 18:53
    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 GreenMike Green Posts: 23,101
    edited 2007-04-18 19:04
    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.
  • jasonpeinkojasonpeinko Posts: 20
    edited 2007-04-18 19:27
    how would i do this if i wanted two arrays like that?
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-18 20:05
    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?
  • jasonpeinkojasonpeinko Posts: 20
    edited 2007-04-20 18:30
    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.
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-20 18:51
    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.
  • jasonpeinkojasonpeinko Posts: 20
    edited 2007-04-20 18:58
    I mean that i could have my program like this:

    a:
    SEROUT Lpin, Baud19200, [noparse][[/noparse]"a"]
    goto main
    b:
    SEROUT Lpin, Baud19200, [noparse][[/noparse]"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, [noparse][[/noparse]row1(hitkey)]
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-20 21:00
    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.
  • jasonpeinkojasonpeinko Posts: 20
    edited 2007-04-30 19:41
    Can you please give me an example?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-30 19:58
    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.
Sign In or Register to comment.