ARRAY SYNTAX ???

I'm trying to get the syntax for Arrays.. setup especially
I know How to work the loops once I get them set up.
I've downloaded 5 books and nothing.
My goal is large 3 D arrays.

Generally:
byte arrayName[numberOfBytes]; // data not pre-assigned
or
byte arrayName[] = {value0, value1, etc. ... valueEnd-1, valueEnd, }; // data is preassigned
these arrays will occupy SRAM.

"large 3 D arrays" is relative - you only get 2048 total bytes of SRAM in an Uno, those bytes are used for all variables & registers & stack & heap storage, so less than 2048 is available for the array.

If the data is non-changing, you can put it in PROGMEM which stores it in flash, and leaves SRAM free.

I'm using a Mega ADK i think that gives me 256K of ram ..if my memory serves.
... I am making some progress between posts

I am able to get initial data into an 3d array one byte at a time. I'm using a 3x3x3 to test logic.

int mycharacter[3][3][3];

 void setup (){

    mycharacter[0][0][0]=0;
      mycharacter[0][0][1]=0;
      mycharacter[0][0][2]=1;
  
      mycharacter[0][1][0]=0;
      mycharacter[0][1][1]=0;
      mycharacter[0][1][2]=1;
  
//...

      mycharacter[2][1][0]=0;
      mycharacter[2][1][1]=0;
      mycharacter[2][1][2]=1;
  
      mycharacter[2][2][0]=0;
      mycharacter[2][2][1]=0;
      mycharacter[2][2][2]=1;

this works and I need a way to load these arrays more efficiently.

the following does NOT work
// primary data load 38x 20 by 20

mycharacter[1][][]=           1,0,1,
                              1,0,0
                              1,0,1;

// <---- but it looks like what I think I need.

in the imediate future I'll need these much larger myblock[38][20][20]
copied into a specific locations on mydisplay[150][50]
and even larger in the medium future when this works

I've got the logic of all of the manipulation (I think) i just need the syntax for the primary data load

I'm using a Mega ADK i think that gives me 256K of ram ..if my memory serves.

Your memory doesn't serve.
8kB

I'm using a Mega ADK i think that gives me 256K of ram ..if my memory serves.

Nope. 8kbytes of RAM. 256k bytes of flash.

I'll need these much larger myblock[38][20][20]
copied into a specific locations on mydisplay[150][50]

The AVR Arduino are not going to be a usable solution for you. Abandon them now; go to an ARM based thing like Due (96k ram), Teensy 3.1 (64k RAM), PIC32 based thing like ChipKit Max32 (128k RAM), or board-level solutions like Raspberry Pi, BeagleBone, etc.

You can initialize multi-dimensional arrays at compile time with something like:

int mychar[4][3][2] = {
   {
      {'a', 'b'},
      {'c', 'd'},
      {'e', 'f'}
   },
   {
      {'a', 'b'},
      {'c', 'd'},
      {'e', 'f'}
   },
   {
      {'x', 'y'},
      {'c', 'd'},
      {'e', 'f'}
   },
   {
      {'a', 'b'},
      {'q', 't'},
      {'e', 'f'}
   }
};

To change the full array at runtime, you'd typically define a bunch of similar arrays stored in flash, and use a flash-aware memcopy-like function:memcopyf2r(ramarray, flasharray1, sizeof(ramarray));

If your arrays are actually constants, and your just switching between different sets of data (say, like font tables), you might get away with your ADK by putting all the data in flash instead of RAM arrays...

If you're RAM-rich, I think modern coding styles lean away from 3-D arrays unless you're actually dealing with a three dimensional thing. Instead, you'd use simpler arrays of structures (that might include other arrays.) So the font table example would be an array indexed by character of glyphs or pointers to glyphs, and a glyph would be (logically) a 2d array of bits...

thank you .
I must have heard your thinking
it was the third set of {} i was missing .. i found a 2d example in a c++ book and expanded it like this

int mycharacter[3][3][3]=
{  //
{{0,1,1,},
{0,0,0,},
{1,1,0,}},
   //
{{0,1,1,},
{0,0,1,},
{0,1,0,}},
   //
{{0,0,1,},
{0,1,0,},
{1,1,1,}}
};

looks like I'm learning about flash ram tomorrow,, I need 38x20x20 + 150X50 that's 25k
i'm going to look at bit level as well

thanks for the info on the other computers.. I'm staying with Arduino for now and I'll look into them

Change the data type from int to byte and you will halve the memory usage straight away. Using bits rather than bytes will have an even more dramatic effect on memory usage of course.

rodloy, learn about loops and save your typing fingers some wear.

The example sketches (in your IDE under File->Examples) in 05 Control cover arrays, loops and switch-case.

GoForSmoke,
I know about Loops.. I'm learning the syntax of this language...
thank you for the input ..I've looked at all of that
and the examples you recommend do not even cover two dimension arrays

I'm still looking for a more elegant way to load the data and what I have covers that
see my reply" it was the third set of {} i was missing}

the big issue now is saving RAM so I think I need to come up with some logic to
read the bits in a 37page x 3byte by 20 line array :roll_eyes:

a SD shield is another possibility and I'm considering that for my next hardware version..

UKHeliBob:
Change the data type from int to byte and you will halve the memory usage straight away. Using bits rather than bytes will have an even more dramatic effect on memory usage of course.

Good tip! After writing C programs on a PC for so long you kind of forget about efficiency.

rodloy:
GoForSmoke,
I know about Loops.. I'm learning the syntax of this language...
thank you for the input ..I've looked at all of that
and the examples you recommend do not even cover two dimension arrays

I'm still looking for a more elegant way to load the data and what I have covers that
see my reply" it was the third set of {} i was missing}

Instead of this:

int mycharacter[3][3][3];

 void setup (){

    mycharacter[0][0][0]=0;
      mycharacter[0][0][1]=0;
      mycharacter[0][0][2]=1;
  
      mycharacter[0][1][0]=0;
      mycharacter[0][1][1]=0;
      mycharacter[0][1][2]=1;
  
//...

      mycharacter[2][1][0]=0;
      mycharacter[2][1][1]=0;
      mycharacter[2][1][2]=1;
  
      mycharacter[2][2][0]=0;
      mycharacter[2][2][1]=0;
      mycharacter[2][2][2]=1;

This saves typing:

byte mycharacter[3][3][3];

void setup ()
{
    byte i, j, k;

    for ( i = 0; i < 3; i++ )
    {
      for ( j = 0; j < 3; j++ )
      {
        for ( k = 0; k < 3; k++ )
        {
          if ( k == 2 ) mycharacter[i][j][k]=1;
        }
      }
    }
}

the big issue now is saving RAM so I think I need to come up with some logic to
read the bits in a 37page x 3byte by 20 line array :roll_eyes:

37 page x 3 byte x 20 lines? What page? AVRs use 32 byte pages.

If you're only storing 0 and 1 then 1 byte can do 8 of those.

If the data is constant, won't change, then you probably have 20+k space for it in flash with the program. The thing to learn about there is PROGMEM and the steps to copy bytes into RAM as you use them.

a SD shield is another possibility and I'm considering that for my next hardware version..

If you have a breadboard, jumpers, resistors and a microSD card with adaptor then you can roll your own SD adapter. Here's a DIY SD adapter on a perf board but think that what matters most is components and connections so you can substitute wires for pins, breadboard for stripboard, etc, if you're in a rush.
http://nathan.chantrell.net/20111128/diy-micro-sd-shield-for-arduino/

The biggest PITA hooking SD to Arduino is that SD is 3.3V, not 5V. The resistors in that circuit are for voltage dividers. It's likely a better circuit than my $1.50 LCSOFT SD adapters that use shortcuts that I've been warned will only work for a limited time. But the lesson I learned is that SD is actually pretty simple and can be cheap, as in I may have spent too much at $1.50 ea even if they'll work forever.

When I started Arduino the minimum SD shield I saw was over $20! A member here had a blog showing an even simpler version of the DIY above and because I didn't know better, I was sure I needed more. I was wrong.

And there is another option if you're not running stand-alone. Have your PC send binary data bytes (as opposed to text data) through serial and have the Arduino send a set back for confirmation. The binary data part will take you some learning but it doesn't have to be translated (more Arduino code), just copied to your array.

Is this just to learn about arrays or do you have a practical application? Can you say what it is?

Honduras If it isn't worth doing right,
it isn't worth doing at all. I like that.. and thanks there is also .." if it isn't worth doing, why do it right?"

jasmine2501 it's a display application

GoForSmoke if ( k == 2 ) mycharacter*[j][k]=1; //I know about nested loops*
// where does this come from ? the data is not calculated it looks like a bit font 20x20 in the final program
// 38 characters x 20 rows x 20 columns

rodloy:
GoForSmoke if ( k == 2 ) mycharacter*[j][k]=1; //I know about nested loops*
[/quote]
I never would have guessed given your stretched out example.
> // where does this come from ? the data is not calculated it looks like a bit font 20x20 in the final program
> // 38 characters x 20 rows x 20 columns
That's 1900 bytes if you use every bit. 38 x 20 x 20 / 8 = 1900. The bit-position algorithm will be a bit tricky. It can easily fit in PROGMEM with most sketches. You need 400 bits per char, you could arrange that as a 2D array like say, bitMap[ 38 ][ 50 ] where the first dimension is char and the second is the bits.

I like your math.. I'm trying to work in flash because of the 256k there and it is not easy either

PROGMEM boolean mycharacter[9][20][20] ={ // when I go from [9] to [10] the stuff hits the fan and
// Necessary casts and dereferencing, just copy.
strcpy_P(buffer, (char*)pgm_read_byte(&(mycharacter[myx][myy][myz])));

back at it tomorrow afternoon Santa Fe, New Mexico

I don't think that's going to work like you think it will.

I packed the [20][20] into a single [50] and mentioned algorithms out of experience but just maybe this compiler has a new surprise for me!

Please, let us know how that works out.

Also, check this out:
http://ruggedcircuits.com/html/quadram.html

They plug and work on MEGA2560. I got one just because the price and what it is. I don't need it but awww cmon! 512KB (not Kb, KB!) SRAM on an AVR is a lot of freedom!

More reasonably especially of you're working towards a product is SPI bus SRAM or even I2C SRAM. 256kb (32KB) SPI SRAM is cheap but just due to needing fewer commands to access must be faster than SD on the same bus.

So far Very little has worked like I thought it would..
I still don't have predictable results with PROGMEM OR DOCUMENTATION
I'm calling it a learning experience

OK,
Thanks for keeping us up-to-date

PROGMEM docs:
http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html

Here's a start point to mess with:

#include <avr/io.h>
#include <avr/pgmspace.h>


const char PROGMEM msg[][40] = { // all this text stored in flash
  "this message stored in flash ram",
  "and printed with a 1 byte buffer",
  "a byte is a byte, you can do numbers"
};

PGM_P Msg;

#define  MSGLINES  3

void  printFlashString( PGM_P M )
{
  byte  B;
  
  do 
  {
    B = pgm_read_byte( M++ );    
    if ( B )  Serial.print((char) B );
  }
  while ( B );
}

void printMsg(void)
{
  for ( byte i = 0; i < MSGLINES; i++ )
  {
    printFlashString( msg[ i ]);
    Serial.println();
  }
  Serial.println();
}

void setup(void)
{
  Serial.begin(9600);
  printMsg();
} 

void loop(void)
{
}

Multi-dimension arrays are a convenience only, RAM is linear.
Learn how ints, etc, are stored and some mysteries will just go away.

THANK YOU

I'm finally back at it ..