Dynamically adding bits to byte 'positions' using LedControl Library

Hi

I would lie to know how I can dynamically add 1's to a byte representing rows on my 8x8 matrix.

I'm constantly updating the matrix using the setRow() function and I'm storing the rows in an array:

byte rows[8] ={B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000};

Depending on which column/row I would like to be able to just update the array to keep track of which LEDs to keep lit/clear. So, if I'm in column 2, row 5, I could update the array to:

byte rows[8] ={B00000000,B00000000,B00000000,B00000000,B00000000,B00100000,B00000000,B00000000};

and so on.

I just have no idea how this is done.

bitSet() function

UKHeliBob:
bitSet() function

This is what I'm trying:

bitSet(rows[colsFull],randomColumn);

but I'm getting error:

invalid types 'byte [8] {aka unsigned char [8]}[int [8]]' for array subscript

It only works if I use numbers instead of variables:

bitSet(rows[3],4);

Please post a small but complete sketch that illustrates the problem
What data type is colsFull, for example ?

UKHeliBob:
Please post a small but complete sketch that illustrates the problem
What data type is colsFull, for example ?

My code was wrong. Sorry.

Thanks for the bitSet() solution - just what I needed :slight_smile:

I am glad that it worked. You might like to look at the bitClear(), bitRead() and bitWrite() functions too

UKHeliBob:
I am glad that it worked. You might like to look at the bitClear(), bitRead() and bitWrite() functions too

Thanks, I will be. Question though: When referring to the bit in bitSet, the count starts with the LSB first, therefore from the right. I'm finding this problematic because in the 8x8 array, columns are from left to right. So, if I have a bit to set in the 3rd column, I would need to set bit 5. (right to left)

Is there a way to set bits using the MSBF?

noiasca:
well, if you subtract your bit postion from 8 you will get the "reversed" order, won't you?

Fair enough. Was hoping there would be a more direct way.

a more direct way

are you aware of that your bitmaps are macros

#define B00000100 4

which could be easily replaced in your sketch already by standard 0b00000100 ?

And very similar that bitSet is just a macro?

#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))

or don't bother to much about it and write your own macro including the eight minus ...

Those macros don't just work with bytes. They will work with larger types, up to "unsigned long int", which is 32 bits. Unfortunately, you need 64 bits, which is "unsigned long long int". The macros don't work with those, but I think that's only because they contain "1UL" rather than "1ULL". So you could try:

#define bitSetULL(value, bit) ((value) |= (1ULL << (bit)))
#define bitClearULL(value, bit) ((value) &= ~(1ULL << (bit)))
#define bitWriteULL(value, bit, bitvalue) (bitvalue ? bitSetULL(value, bit) : bitClearULL(value, bit))

Then hopefully you can use "unsigned long long int rows" instead of "byte rows[8]".

If you try it, please let us know if it works.