Replace all 2d array elements in 1 line of code

Hi guys,

Just a quickie:
I initialized my array

int powerLineSelect[7][7] = {
								{0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0},
								{0,0,0,0,0,0,0}
};

Now later on i would love it if I can do something like this:

powerLineSelect[7][7] = {
								{0,0,0,1,0,0,0},
								{0,0,1,1,1,0,0},
								{0,1,0,1,0,1,0},
								{0,0,0,1,0,0,0},
								{0,0,0,1,0,0,0},
								{0,0,0,1,0,0,0},
								{0,0,0,1,0,0,0}
};

I'm using this table to adjust a led matrix, what leds are on and off.
is there a way because this doesn't compile and doing it each index at a time takes so long.

Thanks allot.

Assuming that you have enough memory, declare all the arrays you need in the program and copy the required array to the master array when required. Alternatively, have you thought of using a 3 dimensional array and using the first index to access the appropriate 2 dimensional array ?

Do you really mean that setting each array point individually takes a long time or just that it takes a lot of code lines ?

A better solution all round would be to use the 8 bits of an array of bytes to hold the 0/1 values. Using ints to hold 0 or 1 is a huge waste of space. If you must do it they way you are now then at least consider using an array of bytes instead of ints but even that will consume 8 times as much memory as necessary.

I did mean allot of code. And a byte might just work but later on i'm using this array to shift values depending on the index, to select the correct pins on my shift register. I will try it. :slight_smile: thanks for the tip.

I have no clue what would be fastest, setting it 1 by 1 or not.. I'm using an Arduino UNO.

I didn't know I could just copy arrays, I will try to do that and use it for my fixed displays. But the thing is that I will have allot of those, i might want to add shifting lines and random effects so it might be to much for my UNO , and I can't fit anything bigger in my casing :stuck_out_tongue:

It looks to me like you're wasting an awful lot of precious RAM - your first examples use 98 bytes to hold seven bytes of data.

Edit: UKHeliBob's post says this too. Oops.

Perhaps you should switch to this method instead, it uses the binary representation of bytes.

byte myArray[8]={
B00000000, 
B00011000,
B00111100,
B01011010,
B00011000,
B00011000,
B00011000,
B00000000
}

The bit approach seems the most compact. The C language has no array-assignment concept, let alone operator; only at declaration time can you initialize an array. I use the bit method to store 8-bit patterns for my LED display; the bitmaps for the digits 0…9 take 80 bytes. One trick I developed years ago is to declare a set of constants:

#define _____  0
#define ____W  1
#define ___W_  2
#define ___WW  3
...and so on, until
#define WWWWW 31

Then all I have to do is draw the picture:

/* eight */
_____,
_WWW_,
W___W,
W___W,
_WWW_,
W___W,
W___W,
_WWW_,

I am using a 7-high font to display on the Adafruit 8x8 NeoPixel display. I actually have two fonts: a 5-bit-wide font to display 0…9 and a 4-bit font to display 10…99. I put two 4-bit nibbles together to form the two digits in a single 8x1 array, then use a bit shift to determine which NeoPixel address I’m setting. As I extract each bit I send either an RGB(0,0,0) triple for 0 or a color triple appropriate to the information for a 1. (Green is 20…36; yellow for 10…20; red for < 10; these are the distances my car bumper is from the back wall of the garage).

To create a larger font than five-wide, I just write a C program that generates the #defines; it is faster than typing them all in by hand. Since a #define takes up no runtime space, the unused #defines don’t matter.

If I need more RAM, I will move this table to PROGMEM, but right now the 160 bytes for the two tables don’t hurt me.

You have not adequately explained why you need an int instead of an uint8_t. Or why you need a signed value.

Note that memcpy can copy one sequence of bytes from point A to point B, and is substantially faster than the nested loop. What you need to do is get rid of those “7” values for your array bounds and replace them with a symbolic name, which this week just happens to be 7. Such as NUM_ITEMS. Then

memcpy(target, source, NUM_ITEMS * NUM_ITEMS * sizeof(source[0][0]))

would copy it no matter what size the array was or what its data type was.