Does a struct require something to be included in order to work?

I am getting a “error: ‘Letter’ does not name a type” error when creating and initializing a struct.

struct Letters
{
int bMatrix [8][4];
};

struct Letters Letter[26];

// Letter A
Letter[0].bMatrix[8][4] =
{
{0,1,1,0},
{1,0,0,1},
{1,0,0,1},
{1,1,1,1},
{1,0,0,1},
{1,0,0,1},
{1,0,0,1},
{1,0,0,1},
}

// Letter B
Letter[1].bMatrix[8][4] =
{
{1,1,1,0},
{1,0,0,1},
{1,0,0,1},
{1,1,1,0},
{1,0,0,1},
{1,0,0,1},
{1,0,0,1},
{1,1,1,1},
}

Try declaring and initializing in one go


Letters Letter[26] = {
  {…}, // A
  {…}, // B
…
};

PS an int will use lots of ram (2 or 4 bytes) if what you need is just 0 and 1 as values. Seems 4 bytes (32 bits) instead of 64 bytes (or128 depending on your arduino) would be enough.

Thanks. I’ll try that.

While I was waiting I converted it to a straight 3 dimensional matrix and got the same error.

I’ll be adding other numbers. This is a work in progress. I’m re-inventing the wheel here I’m sure, but I’ll try some different things to reduce it after I get it working.

Could I go to a char? Is that smaller in the Arduino world?

This is on an ESP32 Thing right now.

This compiles:

byte Letter[][8][4] =
{
  {
    // Letter A
    {0, 1, 1, 0},
    {1, 0, 0, 1},
    {1, 0, 0, 1},
    {1, 1, 1, 1},
    {1, 0, 0, 1},
    {1, 0, 0, 1},
    {1, 0, 0, 1},
    {1, 0, 0, 1},
  },

  {
    // Letter B
    {1, 1, 1, 0},
    {1, 0, 0, 1},
    {1, 0, 0, 1},
    {1, 1, 1, 0},
    {1, 0, 0, 1},
    {1, 0, 0, 1},
    {1, 0, 0, 1},
    {1, 1, 1, 1},
  }
};

You can’t allocate content to global variables in two steps, it has to be done in one go or you need to put the instructions (and for an array it’s cell by cell) in the setup() function for example

For example, defining var this way is not ok

int var; // declare var as an int
var = 12; // ERROR not OK to write statements outside functions or declarations
void setup() {}
void loop() {}

But this is OK

int var = 12; // declare var as an int and define its initial value
void setup() {}
void loop() {}

Or you could set the initial value in the setup

int var; // declare var as an int
void setup() {
  var = 12; // OK to write statements inside functions
}
void loop() {}

I would do it this way:

struct Letters  // use Letters as typedef
{
  int bMatrix[8][4];
};

Letters Letter[26] =
{
  { // Letter A
    { 
      {0,1,1,0},
      {1,0,0,1},
      {1,0,0,1},
      {1,1,1,1},
      {1,0,0,1},
      {1,0,0,1},
      {1,0,0,1},
      {1,0,0,1}, 
    },
  },
  { // Letter B
    {
      {1,1,1,0},
      {1,0,0,1},
      {1,0,0,1},
      {1,1,1,0},
      {1,0,0,1},
      {1,0,0,1},
      {1,0,0,1},
      {1,1,1,1},
    },
  },
};

void setup()
{
  Serial.begin( 9600);

  for( int i=0; i<2; i++)
  {
    for( int j=0; j<8; j++)
    {
      for( int k=0; k<4; k++)
      {
        Serial.print( Letter[i].bMatrix[j][k]);
        Serial.print( ",");
      }
      Serial.println();
    }
    Serial.println();
  }
}

void loop()
{
}

I assume that it should be in PROGMEM as well and using so many integers for a 8x4 character is not needed. Just 8 bytes, which you can write as hexadecimal or as binairy is more common. In the sketch the bits have to be used.

I have tested the sketch above in Wokwi: https://wokwi.com/arduino/projects/297647641073811978.
Click the green start button and then you can compare the output data.
Click the stop button to change the sketch.

Thanks for all the great help to both you & J-M-L. I now have a full days worth of things to try. This is my first deep foray into Arduino so I’m learning it’s capabilities as I go along. Is there something that describes the Arduino programming environment in detail?

I’m replicating this at the link below but I’m adding letters as well. I’ll also add a spigot algorithm of some sort so that I can scroll the digits of pi as well.

I’m putting LED’s behind ping pong balls and making letters that I’ll scroll across the screen so I do need an 8 x 4 matrix for each letter. Each dot in the matrix could be a different color. I’m thinking that maybe I might be able to reduce that but it’s where I’m at now.

All the letters and other characters that will be added are constants of course except there is a possible color associated with each dot. I’m thinking about how to do that as I go along.

Thanks. I’ll try this.

I’d go with single byte per row instead of four bytes per row:

byte Letter[][8] =
{
  {
    // Letter A
    0b0110,
    0b1001,
    0b1001,
    0b1111,
    0b1001,
    0b1001,
    0b1001,
    0b1001,
  },

  {
    // Letter B
    0b1110,
    0b1001,
    0b1001,
    0b1110,
    0b1001,
    0b1001,
    0b1001,
    0b1111,
  }
};
1 Like

A difference I see

compare to

I know that @Koepel’s version has = and goes on to supply initial values. Otherwise the two declarations accomplish the same thing storage/naming wise, so I wonder why the ‘struct’ in the OP’s version wasn’t an error or warning generated.

It appears to simply be ignored, which seems unusual in C/C++.

BTW testing variously gave me the “Low memory available, stability problems may occur.” which Have found one ignores at seem peril.

Just sayin’.

a7

Once the struct “Letters” is defined, the name “Letters” is a synonym for “struct Letters”.

In ancient times you would need to use:
typedef struct {...} Letters;
to create a ‘type name’. In recent C++ versions it is done automatically for you.

2 Likes

I would go like this

const uint32_t  alphabet[] =
{
   0b01101001100111111001100110011001,  // Letter A
   0b11101001100111101001100110011111,  // Letter B
…
};

And use bitwise operations to get to the pixels

How can that be split over 8 lines to show the pixel pattern of the character :thinking:

Did you Try a backslash at the end of the line?
(I’m on my mobile so can’t test, never tried cutting in the middle of a number)


const uint32_t  alphabet[] =
{
   0b\
0110\
1001\
1001\
1111\
1001\
1001\
1001\
1001,  // Letter A
…
};

What part did you build with? I’m using a esp32 Thing and haven’t seen that yet. I do need to sort out & understand all my memory spaces though. Thanks.

Haha, no worries then.

I was just noodling with the syntax, I need to look just now and I see I happened to have the board type set at UNO.

a7

That’s another good idea. When I sort out how I’m going to handle color, I’ll remember this. Thanks again.

This way works even when auto-formatting the sketch indents the data. I tested it by printing each letter out in hex.

#define letter(a,b,c,d,e,f,g,h) 0b##a##b##c##d##e##f##g##h##UL

unsigned long Letters[] =
{
  letter(
    0110,
    1001,
    1001,
    1111,
    1001,
    1001,
    1001,
    1001),


  letter(
    1110,
    1001,
    1001,
    1110,
    1001,
    1001,
    1001,
    1111),
};

WARNING: Do not put a ‘// Comment’ to the right of ‘letter(’. For some reason it is not stripped out but is passed to the macro as ‘/* Comment */’.

1 Like

Cool idea

It came from a reply in that thread where someone wanted the compiler to let them put spaces in their binary literals.