## 3D variable array?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

MJU
Posts: 455
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Contact:

### 3D variable array?

Hi there,

I need to calculate an amount of bits before I can handle them.
At this time I need to have 720 bits ready to use.
These are 3bytes * 30 that I need to calculate. (3 * 8 * 30 bits).

I remembered that in medieval times, in a kind of Basic, there was something like 3D array's.
Instead of variable[10] this 3D array had an extra index that was the Z coordinate.

Is there any way in Flowcode I can accomplish this?
How can I have some "container" for 720 bits that I can edit in a simple way?

I need to get the bits out one by one as soon as every one is calculated.

Kenrix2
Flowcode v5 User
Posts: 211
Joined: Tue Feb 19, 2013 9:51 pm
Contact:

### Re: 3D variable array?

Could you use three sets of 30 container arrays instead (3 * 30 * 8bits)? For example just add these string type variables to your project, X[29], Y[29] and Z[29]. I am sure I don't understand your question but, Flowcode is great at easily handling arrays.

SteveM
Posts: 55
Joined: Tue Mar 25, 2014 2:09 pm
Contact:

### Re: 3D variable array?

Hi,
For an array of fixed size, you can simulate a multi-dimensional array by using a bit of maths to calculate an index to use with a normal 'flat' array. So our 'multi-dimensional' array (or matrix!)...

Code: Select all

``````[a1, a2, a3]
[b1, b2, b3]
[c1, c2, c3]
...etc...``````
Becomes the 'flat' array

Code: Select all

``[a1, a2, a3, b1, b2, b3, c1, c2, c3 ...]``
In this 'flat' array, every fourth item is the start of a new 'row'. So we can calculate the index thus (for the 3 * 30 example)...

Total array size => height * width => 30 * 3 = 90 bytes
Index at (row, column) => (width * row) + column => (3 * row) + column

So to get at the second byte in the fourth row (remembering that indexes will begin at zero; columns go 0..2, and rows go 0..29)...

Code: Select all

``````array[90] = <data>
column = 1
row = 3
value = array[column + 3 * row]``````
This principle can be extended to an array/matrix with any number of dimensions, so long as the size of the array is fixed (a lot of data would have to be moved to change the number of columns dynamically!
You should be careful to keep both 'row' and 'column' within the right value ranges - for example, if you tried to access 'column 4' in this array, you would actually be reading the start of the next row.

Best regards,
Steve.

MJU
Posts: 455
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Contact:

### Re: 3D variable array?

I will look into it Steve!
Thank you..

MJU
Posts: 455
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Contact:

### Re: 3D variable array?

Hi there,

There is something that I don't understand.

I've made a "Matrix" with a variable (BYTE) with an array of 90 positions.
So I guess that I have 8 * 90 positions that I can fill?

What kind of variable must I use to get an array of that length?

I sometimes struggle with positions in array's and loop counters.
Are they all from 0 to X?
I sometimes think that some of these begin at 1 in stead of 0
A loop counter, when I tell it to run 8 times, does it count until 8 or does it internally goes to 0 (so 9 counts?)

It would be great to see a working example..

I need all the 90Bytes separate to send bit per bit. Right now I "parse" every bit to a macro that handles them separately one by one.
But the variables are very small compared to the 90Bytes.

Benj
Matrix Staff
Posts: 14865
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

### Re: 3D variable array?

Hello,
I've made a "Matrix" with a variable (BYTE) with an array of 90 positions.
So I guess that I have 8 * 90 positions that I can fill?
Yes that sounds correct, with each "position" being a single digital bit.
I sometimes struggle with positions in array's and loop counters.
Are they all from 0 to X?
Yes, in this case the variable to address a 8-bit byte in the array would range from 0 - 89.
A loop counter, when I tell it to run 8 times, does it count until 8 or does it internally goes to 0 (so 9 counts?)
Normally a loop counter running 8 times would do something like this to make it clear how many times to repeat the loop

while (index < 8 )

So the loop would run 8 times with index values 0 to 7, on the 9th run the while statement would fail allowing the program to exit the loop.

SteveM
Posts: 55
Joined: Tue Mar 25, 2014 2:09 pm
Contact:

### Re: 3D variable array?

Hi there,

A byte is the smallest 'unit' that you can make an array of - although the Boolean data type looks smaller (it's only on or off!), the data would still be stored as a whole byte, because of the way that access to the chip's memory works. So, although your 90 element array has room for 90 * 8 individual bits, you can only read or write a whole byte at a time.

However, as with the '3D' array of bytes, we can use a little maths to let us read or write an individual bit, because we know that the data will always be in 8bit chunks - though the necessary maths is a little more complicated in this case!
This FC6 file contains a set of macros that illustrate the way that this works...
Bits and bytes.fcfx
In this example, we have a matrix of 30 rows and 3 columns of bytes as before. Macros have been defined 'WriteMatrixByte(Row, Column, Data)' and 'ReadMatrixByte(Row, Column)' that access a specific row or column of the matrix - rows go from 0-29, and columns from 0-2, (To change this, set the COLUMNS and ROWS constants as necessary, then resize the 'Matrix' array variable to be ROWS * COLUMNS in size). A whole byte of data is read or written with each access.

OK, that's what we saw previously, but this example has an alternative way to access the matrix...
The macros 'ReadMatrixBit(Row, Column)' and ''WriteMatrixBit(Row, Column, Data)' can be used. These treat each row of 3 bytes as a single row of 24 bits (8 * 3), which you can index directly (so the Column value now goes from 0 to 23).
Although the array structure is no different in the chip's memory, accessing a single bit is possible because we know that dividing the column by 8 (and rounding down) gives us the location of the correct byte - and taking the remainder of column/8 tells us which exact bit out of that byte. The macro's 'WriteBit' and 'ReadBit' then handle getting at an individual bit of a byte so that they can be written or read independently of each other.

BTW) I am maybe not as fast a coder as you think! The macros in this little example are ripped straight out of the source for one of the Matrix components - these techniques are used in several of them. In cases where you need a large number of true/false values, these kind of 'bitwise' matrixes and arrays massively reduce the amount of memory that you need to store the data - always handy in embedded systems where we still measure memory in KiloBytes.

However, you need to be careful. The 'Bitwise' macro's have to read or write the whole byte from the array each time, just to get at a single bit. While they are handy if you need 'random access' to any old bit, any old time, they are not efficient if you are always reading the whole row in order from start to finish. In that case, it's better to read each whole byte, then use a loop to call the 'ReadBit' or 'WriteBit' macros for the 8 bits of the byte - that prevents each byte from having to be transferred to/from memory 8 times over!

Cheers,
Steve

MJU
Posts: 455
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Contact:

### Re: 3D variable array?

Hey Steve, this looks great.
I will study it carefully and try to learn from it.

Thanks a thousand times!

SteveM
Posts: 55
Joined: Tue Mar 25, 2014 2:09 pm
Contact:

### Re: 3D variable array?

You're welcome.

A quick warning - I've just had a PM to say that the download misbehaves when trying to generate a report, so I'll take a look into that just in case it indicates a problem for compiling the code.
I'll get back to you about that ASAP, and re-upload the file if any fixes are needed.

EDIT: Looks like the download is OK! (phew!) - but there is a bug in Flowcode's report generator (Waaa!)

Cheers,
Steve.