Dictionary features and getting ASCII values from a string

First issue: I have a byte array “alpha” where each row is a letter (ie alpha[0] is A with ASCII code 65)

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

I want to read a character array such as

char myMessage[] = "AAAA";

and have it append each letter’s ASCII code (ie for AAAA → message = {65, 65, 65, 65}

int message[sizeof(myMessage)]; //this would be int message[5] for AAAA with the null character

void appendMessage(int message[])
{
  for (int i = 0; i < sizeof(myMessage); i++)
  {
     message[i] = myMessage[i];
  }
}

Is this the proper approach or is there a better way to handle this.

Second issue: Is there any way I can create a dictionary to allow me to read A (ASCII 65) and have that equate to 0, B (ASCII 66) = 1, etc for my alpha array above.

Any help or pointers is appreciated.

Is there any way I can create a dictionary to allow me to read A (ASCII 65) and have that equate to 0, B (ASCII 66) = 1, etc for my alpha array above.

If the character is between 'A' and 'Z' subtract 65 from the ASCII value to get the array index If the character is between '0' and '9' subtract 48 from the ASCII value to get the array index Deal with the other characters individually by subtracting the appropriate value

So I should make another function that determines if the ascii is between those values?

A function to return the array index for a character passed to it would be helpful

You can do all of the range checks in the same function and return say, -1 if an invalid character is passed to it so that you can be sure that the index returned is valid for the array. Turn my reply #1 into programming statements and you have the function apart from the return

Does my appendMessage function seem correct or on the right path?

Other than that thank you for the help :D

Why do you want to copy the myMessage array to the message array ?

I have another section of code here that I am using to display a message on an LED matrix.

void Scan(byte alpha[][6], int startCol)
{ 
  for(int k=0; k<6; k++)
  {
    digitalWrite(C[k], LOW);
    for(int row = 0; row<8; row++) 
    {  
      digitalWrite(R[row], bitRead(alpha[0][(startCol+k)%6],row)); 
    }
    for(int row = 0; row<8; row++)
    {  
      digitalWrite(R[row], 0); 
    }
    digitalWrite(C[k], HIGH);
  }
}

I’m trying to copy the myMessage array to the message array so that I can scan an entire array.
Currently if I can continuously scan a single row in my alpha array so that my matrix shows a single character over and over again. I think I would be able to show an entire message if I replace alpha[0] with my message array.[/code]

I am not following what you want to do, but anywhere that you access the message array you could access the myMessage array instead so why the need to copy ?

What I wanted it to do when I change the myMessage array is to

1) get the ASCII values of each character in the array

Then I would take those values and

2) append an array with the values of alpha[][0 to 5]

Example

char myMessage[] = "AA";
message[sizeof] = {65,65}
messageToPrint[6*sizeof] = {{B01111110, B10010000, B10010000, B10010000, B01111110, B00000000},{B01111110, B10010000, B10010000, B10010000, B01111110, B00000000},}

The messageToPrint would go into my Scan function, but that part is sorta beyond my initial question and just complicates things.

append an array with the values of alpha[][0 to 5]

If I interpret you correctly you are trying to take a message such as "ABC" and populate an array with values like this

{ {B01111110, B10010000, B10010000, B10010000, B01111110, B00000000}, {B11111110, B10010010, B10010010, B10010010, B01101100, B00000000}, {B01111100, B10000010, B10000010, B10000010, B01000100, B00000000} }

Is that what you are trying to do ?

Or are you trying to read the 'A' and copy

{B01111110, B10010000, B10010000, B10010000, B01111110, B00000000}

to another array to be used somehow before copying the data for 'B' to the same array and using it and so on

The first part is more like what I'm trying to do. I would eventually be able to take an entire message such as "A MESSAGE" and have it populate an array with the bytes of each character in a single array in order.

Sorry, but I still don't see why you want to do this

Suppose that the message is "ABC"

Get the current character Use its value to determine the index to the alpha array Use the values in the alpha array at that index level to display the character Repeat until all characters have been display

I want to do this because the code I want to implement onto the matrix does not allow for it to read A,display A, read B, display B, etc.

The LED matrix must show a set of text rotating like a rotating banner. I believe that the code I have now will work if it I am able to populate an array with all the data of ABC.

I want to do this because the code I want to implement onto the matrix does not allow for it to read A,display A, read B, display B, etc.

To make sure that I understand what you want to do, given a message, say, "AB", please list the bytes to be sent and their order

Given message "AB", I want to populate an array with

  {B01111110, B10010000, B10010000, B10010000, B01111110, B00000000},
  {B11111110, B10010010, B10010010, B10010010, B01101100, B00000000}

which is then passed into my Scan function.

In C, you do not usually "populate" arrays. You can, but I believe you are really talking about copying arrays. In C, you usually manipulate pointers to arrays, rather than the contents. To "copy" an array, you assign its address to a different pointer variable. There are many language features to support it, because it is the most efficient way.

Try this. There are better ways as has been pointed (pun intended) out, but it does what you were originally trying to do

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

char theMessage[] = "AB";
const byte msgLength = 20;
byte codedMessage[msgLength][6];

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  for (int letterNumber = 0; letterNumber < strlen(theMessage); letterNumber++)
  {
    for (int column = 0; column < 6; column++ )
    {
      codedMessage[letterNumber][column] = alpha[getLetterIndex(theMessage[letterNumber])][column];
    }
  }
  showCodedMessage();
}

void showCodedMessage()
{
  for (int letterNumber = 0; letterNumber < strlen(theMessage); letterNumber++)
  {
    for (int column = 0; column < 6; column++)
    {
      printFullBinary(codedMessage[letterNumber][column]);
      Serial.print(" ");
    }
    Serial.println();
  }
}

void printFullBinary(byte number)
{
  for (int bit = 7; bit >= 0; bit--)
  {
    Serial.print(bitRead(number, bit));
  }
}

byte getLetterIndex(char letter)
{
  return letter - 'A';
}

void loop()
{
}