Parsing through byte data in a multidimensional array

I'm trying to do column scanning for my 8x8 LED matrix and I have a multidimensional int array. I would like some help figuring out how to parse through, let's say chars[0][0] (B0111111), into another array (bitInfo[7]) so that bitInfo = {0,1,1,1,1,1,1}. It would be much appreciated if someone could help me with my readCharacters() function which is where I want to initialize this bitInfo array. Thanks :smiley:

ATTEMPT.ino (4.14 KB)

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

I would like some help figuring out how to parse through, let's say chars[0][0] (B0111111), into another array (bitInfo[7]) so that bitInfo = {0,1,1,1,1,1,1}.

A couple of points before you do anything

The chars array should be declared as an array of bytes rather than ints. Why waste memory ?

The readCharacters() function returns an array that has been declared locally within the function and which will be out of scope even if you manage to return it having told the compiler that the function is void, ie it does not return a value

Why do you need to copy the value from one array to another in the first place ?

If you really need to read the bits of a byte and put them in an array the bitRead() is a handy function.

Furthermore, if you are storing bits, you should pack them in bytes or unsigned types, rather than allocating an entire byte for each bit. That is a huge waste of memory. Rather than unpacking them to a byte or int array, you should attempt to extract them directly from the chars[][] array for processing.

seems a lot of memory wasted indeed!

[url=https://www.arduino.cc/reference/tr/language/functions/bits-and-bytes/bitread/]bitRead[/url]() will behave like your bitInfo array but it's a function (macro actually) rather than a real array, so instead of dumping chars[0][0] into the bitInfo array and accessing bitInfo[2] you can do bitRead(chars[0][0],2) directly

Sorry, I'm new to this forum so I wasn't sure if posting all the code was kosher.

I'm trying to create a scrolling message on my LED matrix so my chars array has every letter or digit that someone could input into the array so it can be printed on the display. Ideally, I would get an input (a message) and another function would then read each letter. The letter would then correspond to one row in my 39x5 chars matrix. Each binary number in the row represents which rows I need to turn HIGH for each column.

I'm trying to copy these values into a different array so that I can try to rotate one letter on my matrix.

Thanks for the bitRead recommendation. I will go try it out.

OK so you want to handle your bitmap. That has been done a few zillion times, there is no real need for extra storage, really just bit manipulation and indexing

You can have a look at bitClear(), bitRead(), bitSet(), highByte(), lowByte() that exists for you already in the environment.

not sure I get the

The letter would then correspond to one row in my 39x5 chars matrix

don't you need multiple bytes to paint your letter ?

Yes. So each row in my chars array represents a single letter. The first row represents the letter A

void Scan(byte chars[][5])
{
  //Column to scan
  digitalWrite(C[0], LOW);
  digitalWrite(C[1], 1);
  digitalWrite(C[2], 1);
  digitalWrite(C[3], 1);
  digitalWrite(C[4], 1);
  digitalWrite(C[5], 1);
  digitalWrite(C[6], 1);
  digitalWrite(C[7], 1);
  for(int r = 0; r<8; r++)  
  {  
    for(int i=0; i<8; i++)
    {
    //Row to turn on based on readCharacters
    digitalWrite(R[r], bitRead(chars[0][0],i)); 
    Serial.println(bitRead(chars[0][0],i)); 
    }
    delay(10);
  }  
  digitalWrite(C[1], HIGH);
}

So I have this code here where I am trying to create something like the attached image on my matrix.

Only the first column is allowed to turn on and bitRead(chars[0][0],i) controls which rows are turned on. The delay is for me to simply see what is turning on since the refresh rate is too fast. For some reason, the first two rows are permanently on and rows 3-8 are all lighting up.

Capture.PNG

your image
Capture.PNG

you seem to only access chars[0][0]

can't really help with snippets of code... can you share full code or a sample demonstrating what you do with the definition of the alphabet bitmap ?

//characters array
byte chars[][5] =  
{  
  {B01111110, B10010000, B10010000, B10010000, B01111110},  // A - 65
  {B11111110, B10010010, B10010010, B10010010, B01101100}, // B - 66
  {B01111100, B10000010, B10000010, B10000010, B01000100}, // C - 67
  {B11111110, B10000010, B10000010, B10000010, B01111100}, // D - 68
  {B11111110, B10010010, B10010010, B10010010, B10000010}, // E - 69
  {B11111110, B10010000, B10010000, B10010000, B10000000}, // F - 70
  {B01111100, B10000010, B10001010, B10001010, B01001110}, // G - 71
  {B11111110, B00010000, B00010000, B00010000, B11111110}, // H - 72
  {B00000000, B10000010, B11111110, B10000010, B00000000}, // I - 73
  {B00000100, B00000010, B00000010, B00000010, B11111100}, // J - 74
  {B11111110, B00010000, B00101000, B01000100, B10000010}, // K - 75
  {B11111110, B00000010, B00000010, B00000010, B00000010}, // L - 76
  {B11111110, B01000000, B00110000, B01000000, B11111110}, // M - 77
  {B11111110, B00100000, B00010000, B00001000, B11111110}, // N - 78
  {B01111100, B10000010, B10000010, B10000010, B01111100}, // O - 79
  {B11111110, B10001000, B10001000, B10001000, B01110000}, // P - 80
  {B01111100, B10000010, B10001010, B10000100, B01111010}, // Q - 81
  {B11111110, B10010000, B10011000, B10010100, B01100010}, // R - 82
  {B01100100, B10010010, B10010010, B10010010, B01001100}, // S - 83
  {B10000000, B10000000, B11111110, B10000000, B10000000}, // T - 84
  {B11111100, B00000010, B00000010, B00000010, B11111100}, // U - 85
  {B11111000, B00000100, B00000010, B00000100, B11111000}, // V - 86
  {B11111110, B00000100, B00011000, B00000100, B11111110}, // W - 87
  {B11000110, B00101000, B00010000, B00101000, B11000110}, // X - 88
  {B11000000, B00100000, B00011110, B00100000, B11000000}, // Y - 89
  {B10000110, B10001010, B10010010, B10100010, B11000010}, // Z - 90
  {B01111100, B10001010, B10010010, B10100010, B01111100}, // 0 - 48
  {B00000000, B01000010, B11111110, B00000010, B00000000}, // 1 - 49
  {B01000110, B10001010, B10010010, B10010010, B01100000}, // 2 - 50
  {B01000100, B10000010, B10010010, B10010010, B01101100}, // 3 - 51
  {B00011000, B00101000, B01001000, B11111110, B00001000}, // 4 - 52
  {B11100100, B10100010, B10100010, B10100010, B10011100}, // 5 - 53
  {B00111100, B01010010, B10010010, B10010010, B10001100}, // 6 - 54
  {B10000000, B10001110, B10010000, B10100000, B11000000}, // 7 - 55
  {B01101100, B10010010, B10010010, B10010010, B01101100}, // 8 - 56
  {B01100100, B10010010, B10010010, B10010010, B01111100}, // 9 - 57
  {B00000000, B00000000, B00000000, B00000000, B00000000}, // Blank - 32
  {B00010000, B00010000, B00010000, B00010000, B00010000}, // Dash - 45
  {B10010010, B10010010, B10010010, B10010010, B10010010}  // Error
};

So this is the character array where each row is one letter.

For the letter A, the bitmap should look like

  0,1,1,1,0,0,0,0,
  1,0,0,0,1,0,0,0,
  1,0,0,0,1,0,0,0,
  1,1,1,1,1,0,0,0,
  1,0,0,0,1,0,0,0,
  1,0,0,0,1,0,0,0,
  1,0,0,0,1,0,0,0,
  0,0,0,0,0,0,0,0,

The rest of the code is just setup and pin assignments

byte r0 = 0;
byte r1 = 1;
byte r2 = 2;
byte r3 = 3;
byte r4 = 4;
byte r5 = 5;
byte r6 = 6;
byte r7 = 7;

byte c0 = 8;
byte c1 = 9;
byte c2 = 10;
byte c3 = 11;
byte c4 = 12;
byte c5 = 13;
byte c6 = A0;
byte c7 = A1;

byte R[] = {r0,r1,r2,r3,r4,r5,r6,r7};
byte C[] = {c0,c1,c2,c3,c4,c5,c6,c7};

void setup()  
{  
  Serial.begin(9600);
  // iterate over the pins:
  for(int i = 0;i<8;i++)  
  // initialize the output pins:
  {  
    pinMode(R[i],OUTPUT);  
    pinMode(C[i],OUTPUT);  
  }  
}

Right now the Matrix with the Scan function

void Scan(byte chars[][5])
{
  //Column to scan
  digitalWrite(C[0], LOW);
  digitalWrite(C[1], 1);
  digitalWrite(C[2], 1);
  digitalWrite(C[3], 1);
  digitalWrite(C[4], 1);
  digitalWrite(C[5], 1);
  digitalWrite(C[6], 1);
  digitalWrite(C[7], 1);
  for(int r = 0; r<8; r++) 
  { 
    for(int i=0; i<8; i++)
    {
    //Row to turn on based on readCharacters
    digitalWrite(R[r], bitRead(chars[0][0],i));
    Serial.println(bitRead(chars[0][0],i));
    }
    delay(10);
  } 
  digitalWrite(C[1], HIGH);
}

Is showing Row 1 and 2 as HIGH, and row 3-8 are flashing sequentially as it goes through the for loop.

that will print out your symbols for A B and C

const byte nbCols = 5;

byte chars[][nbCols] =
{
  {B01111110, B10010000, B10010000, B10010000, B01111110},  // A - 65
  {B11111110, B10010010, B10010010, B10010010, B01101100}, // B - 66
  {B01111100, B10000010, B10000010, B10000010, B01000100}, // C - 67
};

byte nbChars = sizeof(chars) / sizeof(chars[0]);

void setup()
{
  Serial.begin(115200);
  for (int8_t symbol = 0; symbol < nbChars; symbol++) {
    Serial.println(F("\n--------"));
    for (int8_t col = 0; col < nbCols; col++) {
      for (int8_t aBit = 7; aBit >= 0; aBit--) {
        if (bitRead(chars[symbol][col], aBit) == 1) Serial.write('X');
        else Serial.write(' ');
      }
      Serial.println();
    }
  }

  // rotated
  Serial.println(F("\n-------- ROTATED --------"));
  for (int8_t symbol = 0; symbol < nbChars; symbol++) {
    Serial.println(F("\n--------"));
    for (int8_t aBit = 7; aBit >= 0 ; aBit--) {
      for (int8_t col = 0; col < nbCols; col++) {
        if (bitRead(chars[symbol][col], aBit) == 1) Serial.write('X');
        else Serial.write(' ');
      }
      Serial.println();
    }
  }
}

void loop() {}
--------
 XXXXXX 
X  X    
X  X    
X  X    
 XXXXXX 

--------
XXXXXXX 
X  X  X 
X  X  X 
X  X  X 
 XX XX  

--------
 XXXXX  
X     X 
X     X 
X     X 
 X   X  

-------- ROTATED --------

--------
[color=blue]
 XXX 
X   X
X   X
XXXXX
X   X
X   X
X   X[/color]
     

--------
[color=blue]XXXX 
X   X
X   X
XXXX 
X   X
X   X
XXXX [/color]
     

--------
[color=blue] XXX 
X   X
X    
X    
X    
X   X
 XXX [/color]

(console at 115200 bauds)

does it help?

Is this supposed to print out the symbols in the serial monitor?

yes(console at 115200 bauds).

have a look again above I added code to rotate the letters too. that should give you an idea on how to play with the symbols, columns, bits etc

Ok I think I figured it why some of my leds were staying on. Something with the Serial.begin was keeping some of my pins on HIGH.

Ah yes don’t use pin 0 and 1 !