assign array to array element

Hi,

I have the below defined. Each flickbook element is an LED which contains an array of flashes for the led

boolean flickbook[][8] = {
{ HIGH,  LOW,  LOW,  LOW,HIGH,  LOW,  HIGH,  LOW},       // one led
{ LOW, HIGH,  LOW,  LOW,HIGH,  HIGH,  LOW,  LOW},
{  LOW, LOW, HIGH,  LOW,HIGH,  HIGH,  LOW,  LOW},
{  LOW,  LOW, LOW,  HIGH,HIGH,  LOW,  HIGH,  LOW}
};

when I want to change the flashing state of say LED0 which would be flickbook[0] I have to update each element of the nested array. Like so:

void ledState(int led,int state){
if(state == 0){
flickbook[led][0] = LOW;
flickbook[led][1] = LOW;
flickbook[led][2] = LOW;
flickbook[led][3] = LOW;
flickbook[led][4] = LOW;
flickbook[led][5] = HIGH;
flickbook[led][6] = LOW;
flickbook[led][7] = HIGH;
}

}

How can I just assign a new array to flickbook[0]?

tried:
flickbook[0] = boolean [8]{ LOW, LOW, LOW, HIGH,HIGH, LOW, HIGH, LOW}; and various other variation of but no luck. Any help most appreciated.

You really can't. It's usually easier if you use a for loop

for(int i=0;i<8;i++) {
   flickbook[0][i] = false;
}

You could also do something like this:

boolean newCode = {false, true, false, false, true, true, false, true};

for(int i=0;i<8;i++){
   flickbook[0][i] = newCode[i]
}

and then you could just copy and paste the loop. Of course, it adds a lot of unnecessary overhead, but that's not necessarily a bad thing.

Maybe use bit values for on or off:

uint8_t flickbook[] = {
  B10001010,
  B01001100,
  etc ...
};
...
if (state==0)
  flickbook[led] = B00000101;

Jim

Or add another level of indirection: keep all your patterns in the flickbook array and keep another array of int (byte?) that specifies which led uses which pattern. i.e. UsingPattern[0] specifies which row of flickbook holds the pattern that led0 is currently using.

WizenedEE:
You really can't. It's usually easier if you use a for loop

I was beginning to wonder! I've lived a sheltered life with higher level (web) languages :smiley:

JimG:
Maybe use bit values for on or off:

uint8_t flickbook[] = {

B10001010,
  B01001100,
  etc ...
};
...
if (state==0)
  flickbook[led] = B00000101;




Jim

If I'm honest I don't really understand what this changes or achieves?

wildbill:
Or add another level of indirection: keep all your patterns in the flickbook array and keep another array of int (byte?) that specifies which led uses which pattern. i.e. UsingPattern[0] specifies which row of flickbook holds the pattern that led0 is currently using.

Ah! I like this idea! I need to give it some more thought but I get what your saying

Thanks all for your suggestions :smiley:

dannix:

JimG:
Maybe use bit values for on or off:

uint8_t flickbook[] = {

B10001010,
  B01001100,
  etc ...
};
...
if (state==0)
  flickbook[led] = B00000101;




Jim

If I'm honest I don't really understand what this changes or achieves?

Oh,I think I know what you mean actually. Erm how do you call which bit of the byte you desire to read? I need to read up more but this would appear the least overhead method?

Yeah, if you have <= 8 true/false values you can store them in a a byte. To "get them out" again, you just do this:

value = !!(_BV(3) & flipbook[3]);
//_BV(x) expands to 1 << x , & is bitwire arithmatic, and !! means "not not"
// So this expands out to
value = !!(B00001000 & flipbook[3]);
//& means, "if each corresponding bit is true". Because of the first argument, only the third bit could ever be true, and it depends on whether the third bit of flipbook is true
// The !! changes the number from B00001000 to B00000001 or B00000000 to B00000000
// Do this since most functions expect to get a 1 instead of a 8 (B00001000 = 8)

You can also look up bitwise arithmetic to get more information about &

Many ways to do a bitwise on/off test. Here's one:

#define BIT_ON(b,n) ( ((b) & (1 << (n))) != 0 )
#define BIT_OFF(b,n) ( ((b) & (1 << (n))) == 0 )
void setup() {
  Serial.begin(57600);
  for( int i = 0; i < 8; i++ ) {
    uint8_t k = 1 << i;
    Serial.println( k, BIN );
    for( int j = 0; j < 8; j++ ) {
      Serial.print("Bit "); Serial.print( j, DEC ); Serial.print(":  ");
      Serial.print("BIT_ON:");Serial.print( BIT_ON(k,j) ); Serial.print(" , ");
      Serial.print("BIT_OFF:");Serial.print( BIT_OFF(k,j) );
      Serial.println();
    }
  }
}
void loop() {
}

Jim

There are also some bit twiddling functions provided - bitread would be useful to you in this case: http://arduino.cc/en/Reference/BitRead

WizenedEE:
Yeah, if you have <= 8 true/false values you can store them in a a byte. To "get them out" again, you just do this:

You can also look up bitwise arithmetic to get more information about &

I follow the principle but I do need to read up more.

wildbill:
There are also some bit twiddling functions provided - bitread would be useful to you in this case: http://arduino.cc/en/Reference/BitRead

That's too easy! It's nice for when I'm feeling lazy but since I seem to have avoided bitwise operations in other languages then I ought to get stuck in to what them functions would be otherwise hiding from me.

I'm glad I asked now :smiley: I was just going to leave it be as it was "working", Thanks all!