Shop OBEX P1 Docs P2 Docs Learn Events
Multi-dimensional array? — Parallax Forums

Multi-dimensional array?

m.e.m.e. Posts: 2
edited 2009-02-18 18:17 in BASIC Stamp
I'm trying to help my spouse with a project using BS2. He needs to create a multi-dimensional array (6 x 6 x 31, each cell containing an integer). Documentation doesn't seem to go into anything beyond one dimension ... so am I right in assuming 3 dimensions are not supported, or is there a trick/workaround we might try? Also, since my day job is Web development I'm not used to the memory constraints with BS2, and I suspect this will come into play here ... so any hints in that area would also be greatly appreciated. Thanks!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-14 21:50
    There's not enough memory for this sort of array. The BS2 only has 13 16-bit word variables. These can be referred to as 26 8-bit variables, 52 4-bit variables, or 208 individual bits. It is possible to store constant arrays in the EEPROM, but this only has 2K bytes of storage of which some has to used for the compiled program.

    There are other Stamp models with more memory, but not enough for what you want. There's a comparison page on Parallax's website that shows the different amounts (and kinds) of memory that come with each model.

    The only microcontroller that Parallax has that includes enough memory would be the Propeller. While that's easy to use, it's not as easy as the Stamps and it is very different. There is a version that, with some care, can be plugged into most Stamp boards like the Board of Education (the SpinStamp).

    Post Edited (Mike Green) : 2/14/2009 9:57:19 PM GMT
  • m.e.m.e. Posts: 2
    edited 2009-02-15 19:02
    Thanks for this quick response, Mike. We are trying to figure out another approach using formulae. That will lead to some choices in how we do the logic - again, impacting memory usage. Which of these approaches would be the most efficient? (not using valid syntax and numbers here, just the concepts). One assigns values to a variable, based on the results of 1 to 15 IF - THEN statements, then computes a formula incorporating the variable. The other gives a different formula for each of the 15 possibilities, plugging in the appropriate constants without having assigned those values to a variable first.

    IF X > 200 AND X < 300 THEN Y = 2 GO TO FORMULA
    IF X > 300 AND X < 400 THEN Y = 3 GO TO FORMULA
    IF X > 400 AND X < 500 THEN Y = 4 GO TO FORMULA
    .
    .
    .
    [noparse][[/noparse]there can be 15 of these IF THEN combos]
    .
    .
    FORMULA:
    A = Y * B

    OR

    IF X > 200 AND X < 300 THEN A = 2 * B GO TO OUTPUTA
    IF X > 300 AND X < 400 THEN A = 3 * B GO TO OUTPUTA
    IF X > 400 AND X < 500 THEN A = 4 * B GO TO OUTPUTA
    .
    .
    .
    [noparse][[/noparse]there can be 15 of these formulae too]

    Thanks again!
  • W9GFOW9GFO Posts: 4,010
    edited 2009-02-15 19:23
    I think the Select - case uses less memory...

    Calculate:
    
     SELECT x
       CASE >= 200 AND < 300
       y = 2
       CASE >= 300 AND < 400
       y = 3
       CASE >= 400 AND < 500
       y = 4
    
     etc...
    
     ENDSELECT
    
     A = x*y
    
    Return
    
    



    Rich H
  • DufferDuffer Posts: 374
    edited 2009-02-15 19:29
    Either way, you could use:

    Y = X/100 'Sometimes integer math has its advantages 
    GOTO FORMULA 
    or 
    A = (X/100) * B 
    GOTO OUTPUTA
    

    Duffer
  • InstinctzInstinctz Posts: 18
    edited 2009-02-16 09:17
    What exactly are you trying to accomplish, or specific thing your wanting to control?

    If you dont want to shout outloud pm, but I think knowing this will be a main decider on which action would be best to take.
  • achilles03achilles03 Posts: 247
    edited 2009-02-16 17:30
    There's one option you can use (I've used it in the past). It works, and it lets you have a larger array than the BS2 can afford you. In a nutshell, you can store your 3D array on an eeprom and parse the address to represent the 3 indices of the array. Let me explain:

    Lets say I have an 32kB eeprom (which you can easily connect to the BS2). You can designate the first 5 bits of the address to be the first index, the next 5 bits of the address to be the second index, and the last 5 bits of the address to be the third index. This gives you a 32 x 32 x 32 array.

    So if you want to look up (or store) a value at address (10, 5, 6), then you set the address at 10406 (that's bit value 010100010100110, where 01010=10, 00101=5, and 00110=6).

    You don't have to use a 5bit x 5bit x 5bit matrix either. For a 32kB eeprom, you could go 10bit x 3bit x 2bit (1024 x 8 x 4 array), or 8 x 4 x 3 (256 x 16 x 8 array)... so long as sum of the bits needed to store the array dimensions in 3 dimensions doesn't exceed 15 (15 for a 32kB chip, or 16 for a 64kB chip, 14 for 16kB, etc).

    Do you follow? Did I adequately explain the concept? smile.gif

    Dave

    Post Edited (achilles03) : 2/17/2009 3:59:11 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-02-18 18:17
    This is similar to what I was going to suggest…you could also use the multiply formula for calculating a 3D element…for example, the following formula calculates an offset in memory from a base address for a three dimensional array. In any event, if it is data that needs to be frequently changes you can also use an FRAM device. Take care.

    Address = Base + ((depthindex*col_size+colindex) * row_size + rowindex) * Element_Size

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
Sign In or Register to comment.