bitRead or array

hello, i am building a 16step sequencer and would like to know what would be the better way to store some predefined patterns.

currently i did it like this:

pattern[3][16] = { {1,0,0,0,1,0,0,0,1,0,0,0,1,0,1,0}, {1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0}, {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0} } and read it like grid = pattern[num][currentStep];

this has the advantage that i see the pattern inside the editor. but the file would become

on the other hand, i could store it like this:

pattern[3] = {34954,33288,32896}

... and than use bitRead to cycle thru the sequence by using grid = bitRead(pattern[num],16-currentStep);

would this save space on my arduino nano? or is there no benefit of using bitRead over the actual array. or did i misunderstand the whole purpose?

thanks for any input here, Ello

If you declare byte pattern[3] [16]... You need 48 Byte (plus a bit overhead). If you declare int pattern[3]... You need 6 bytes - with the same overhead for array management. So int is good idea.

this has the advantage that i see the pattern inside the editor

So could you if you expressed the number in binary format

0b1000100010001010

UKHeliBob:
So could you if you expressed the number in binary format

0b1000100010001010

ah. see, i am completely new to arduino stuff. this comes in totally handy. and the impact on the memory would be the same i guess?

thank you both

Couple of suggestions:

  • You can gain back some of the visibility into your bit pattern that the other approach had by defining your constants using binary notation. Just be careful to count the bits. @UKHeliBob just beat me to this one.

  • Different processor platforms use different sizes for 'int' variables. So, I wouldn't rely on the compiler giving you what you expect. Rather, explicitly specify the type as 'uint16_t' --- i.e. a 16-bit wide, unsigned integer.

  • Let the compiler figure out the size of the array for you, and provide a variable with that value.

  • You didn't say so, but I'm guessing these bit patterns are constant and won't be changed in your program. In that case, specify them using the 'const' keyword.

So, you end up with:

const uint16_t pattern[] = { 0b1000100010001010, 0b1000001000001000, 0b1000000010000000 };
const uint8_t numPatterns = sizeof(pattern) / sizeof(pattern[0]);

gfvalvo:
Couple of suggestions:

Just be careful to count the bits.

and that is exactly why I tend to write bit patterns vertically aligned

const uint16_t pattern[] = {
  0b1000100010001010,
  0b1000001000001000,
  0b1000000010000000
};
const uint8_t numPatterns = sizeof(pattern) / sizeof(pattern[0]);

as a visual check I didn’t accidentally add/remove a bit.

have to say thank you again. using numbers allows for soo cool manipulations to the patterms.

using numbers allows for soo cool manipulations to the patterms.

Do you mean numbers expressed like 0b1000100010001010 or something else ?

UKHeliBob: Do you mean numbers expressed like 0b1000100010001010 or something else ?

no, generally i never thought about the fact that numbers represent a pattern. maybe i didn't see the wood because of all the trees. dunno, since it is so obvious :)