Help needed with declaring the 32x8 array of bits

I need to declare the 32x8 table where each cell would represent a pixel on the led matrix.
I could define it as
byte my_array[32] [8]
But this actually ocupies 8 times more space then I actually need.
I could do it like this
int my array [2] [8]
That gives me 2x 16 bit in each row.
But then I am not that proficient with accessing the bits in those integers.

How would you write a command to display all bits to Serial monitor in the loop as an example

for (int i=0,i<32,i++){
  for (int j=0, i<8,j++){
     (print corresponding bit to serial monitor)
  }
}

Or maybe there is a better way to tacke that problem

you could define

byte my_array[32];

and use bit manipulation macros to access the individual bits

see

Bits and Bytes

bit()
bitClear()
bitRead()
bitSet()
bitWrite()

in your for loop you would use bitRead()

Thanks. I am trying to figure out how this would work .
I the example I gave how would the command look to display the bit. As those commands refer to individual bytes not to a string of 32 bits right?

Here is a quick function to print bits of a variable

void print_bits( const uint32_t num, const size_t size )
{
  size_t counter = size * 8;
  while ( counter )
  {
    Serial.print( ( ( num >> --counter ) & 1 ) + '0' );
  }
}

Example

uint8_t val = 0b11001010;
print_bits( val, sizeof( val ) );

I think I got it. It would be

Would that work?

This is cool.

uint32_t stores 32 bits so that is pretty much what I wanted.

One more question. What is +'0' for?

Actually, it's not needed here, you can remove it if you want. It converts a digit to a char, for example 0 to '0'

here is a more involved example

you should see in the Serial monitor

------------------------------------
|  *   * ***** *     *     ***** * |
|  *   * *     *     *     *   * * |
|  *   * *     *     *     *   * * |
|  ***** ***   *     *     *   * * |
|  *   * *     *     *     *   * * |
|  *   * *     *     *     *   * * |
|  *   * *     *     *     *   *   |
|  *   * ***** ***** ***** ***** * |
-----------------------------------

------------------------------------
|  *   * ***** *     *     *****   |
|  *   * *     *     *     *   *   |
|  *   * *     *     *     *   *   |
|  ***** ***   *     *     *   *   |
|  *   * *     *     *     *   *   |
|  *   * *     *     *     *   *   |
|  *   * *     *     *     *   *   |
|  *   * ***** ***** ***** *****   |
-----------------------------------

------------------------------------
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
|                                  |
-----------------------------------

------------------------------------
| ******************************** |
| ******************************** |
| ******************************** |
| ******************************** |
| ******************************** |
| ******************************** |
| ******************************** |
| ******************************** |
-----------------------------------

the code for reference

uint8_t framebuffer[32]  = // 32 columns x 8 lines black or white pixels
{
  0x00, 0xFF, 0x08, 0x08, 0x08, 0xFF, 0x00, 0xFF,
  0x89, 0x89, 0x81, 0x81, 0x00, 0xFF, 0x80, 0x80,
  0x80, 0x80, 0x00, 0xFF, 0x80, 0x80, 0x80, 0x80,
  0x00, 0xFF, 0x81, 0x81, 0x81, 0xFF, 0x00, 0xBF,
};

enum color_t : uint8_t {black, white};

void setPixel(uint8_t c, uint8_t l, color_t color) { // 0 <= c < 32 and 0 <= l < 8
  switch (color) {
    case black: // write a 0 in the corresponding bit
      bitSet(framebuffer[c], l); // https://www.arduino.cc/reference/tr/language/functions/bits-and-bytes/bitset/
      break;
    case white: // write a 1 in the corresponding bit
      bitClear(framebuffer[c], l); // https://www.arduino.cc/reference/tr/language/functions/bits-and-bytes/bitclear/
      break;
  }
}

color_t readPixel(uint8_t c, uint8_t l) { // 0 <= c < 32 and 0 <= l < 8
  return (bitRead(framebuffer[c], l) == 1) ? white : black;
}

void printScreen() {
  Serial.println("------------------------------------");
  for (uint8_t l = 0; l < 8; l++) {
        Serial.print("| ");
    for (uint8_t c = 0; c < 32; c++) {
      Serial.write(readPixel(c, l) == white ? '*' : ' ');
    }
    Serial.println(" |");
  }
  Serial.println("-----------------------------------\n");
}

void setup() {
  Serial.begin(115200);
  printScreen(); // will show our initial Hello pattern

  // remove the '!' at the end
  for (uint8_t l = 0; l < 8; l++) setPixel(31, l, white);
  printScreen();

  // clear up the screen (pixel by pixel)
  for (uint8_t l = 0; l < 8; l++)
    for (uint8_t c = 0; c < 32; c++)
      setPixel(c, l, white);
  printScreen();

  // fill up the screen (pixel by pixel)
  for (uint8_t l = 0; l < 8; l++)
    for (uint8_t c = 0; c < 32; c++)
      setPixel(c, l, black);
  printScreen();
}

void loop() {}

I prefer doing this kind of thing with straight-ahead code as has been done here with @J-M-L , but there are libraries for that. This one for bits

boolArray

comes from a family of three. In addition to the one for bits, there's one for nibbles (four bit words) and one for words of arbitrary bit length.

I did use the arbitrary precision version once when I had a need for 6 bit words and less time and energy than usual for writing it myself.

If you didn't know, now you know.

a7

Consult a few matrix IC libraries, they all contain code like this.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.