Creating an array of `structs`

I'm defining a struct and attempting to create an array filled with 89 of them.
The method below compiles, and using keyBuffer[key].keyStruck or keyBuffer[key].channel etc, I can access correct values. key is an integer between 0 - 88.
However, I suspect that the code isn't doing quite what I think it is, as declaring the keyBuffer array breaks other functionality in the sketch, for instance doing a strip.show() in the setup loop.
is keyState keyBuffer[88] creating an array of type keyState with 89 values, or is this code doing something else that I'm not aware of?

//define primary struct
typedef struct{
  unsigned int  keyStruck;
  unsigned int  channel;
  unsigned int  velocity;
  unsigned int  keyLight1 = 0;
  unsigned int  keyLight2 = 0;
  bool  isDown;
  bool  recentlyReleased;
  uint32_t  lastReleased;
}keyState;

keyState keyBuffer[88];

If you want 89 elements in an array then the definition must be

keyState keyBuffer[89];

and the elements will be numbered 0 - 88

...R

This is my normal method of creating an array of structs

struct timerData  //struct to define the data layout
{
  unsigned long startTime;  //start time
  unsigned long period; //period

};

timerData timers[2];  //an array of 2 structs of type timerData

Hi Dr-Zee

It's very hard to help you when you keep us in the dark about so many things. You post only a fragment of your code. You don't tell us what is connected to your circuit, other than vague hints. You don't even tell us what kind of Arduino you are using. So please read the forum guide before you post again. It's not a lecture about bad language or intolerance, it's about what we need to know and what we need you to do in order for us to help you.

Right now my guess is your code is running short of ram memory, or accessing arrays outside their boundaries is overwriting ram that is used elsewhere, and this is causing the strange side effects between different parts of your code.

PaulRB:
Hi Dr-Zee

It's very hard to help you when you keep us in the dark about so many things. You post only a fragment of your code. You don't tell us what is connected to your circuit, other than vague hints. You don't even tell us what kind of Arduino you are using. So please read the forum guide before you post again. It's not a lecture about bad language or intolerance, it's about what we need to know and what we need you to do in order for us to help you.

Right now my guess is your code is running short of ram memory, or accessing arrays outside their boundaries is overwriting ram that is used elsewhere, and this is causing the strange side effects between different parts of your code.

The scope of my question is only the assignment of structs into an array. I'm not asking for any kind of diagnostic. The only question I have is: Is this how to create an array of structs. Any more info would be off topic. It's a large sketch, and I don't want anyone to feel like they have to troubleshoot it for me. Thank you for taking the time.

MyStruct myArray [mySize];

PaulRB:
Hi Dr-Zee

It's very hard to help you when you keep us in the dark about so many things. You post only a fragment of your code. You don't tell us what is connected to your circuit, other than vague hints. You don't even tell us what kind of Arduino you are using. So please read the forum guide before you post again. It's not a lecture about bad language or intolerance, it's about what we need to know and what we need you to do in order for us to help you.

Right now my guess is your code is running short of ram memory, or accessing arrays outside their boundaries is overwriting ram that is used elsewhere, and this is causing the strange side effects between different parts of your code.

The issue was that I wasn't converting values from the struct to local variables of type before use.
Eliminating my struct declaration as a culprit brought me to this pretty quickly, and without making anyone slog through my code. Thank you again.

The issue was that I wasn't converting values from the struct to local variables of type before use.

Can you give an example of where using the struct variable directly causes a problem that needs some sort of conversion ?

The easiest sollution should be:

struct AB_TYPE {
  int a, b;
} ab_array[10];

for (int i = 0; i < 10; i++)
{
  ab_array[i].a = i * 10;
  ab_array[i].b = i * 100;
}

for (int i = 0; i < 10; i++)
{
  Serial.print(i);
  Serial.print(":");
  Serial.print(ab_array[i].a);
  Serial.print(",");
  Serial.println(ab_array[i].b);
}

UKHeliBob:
Can you give an example of where using the struct variable directly causes a problem that needs some sort of conversion ?

for(i = 1; i < 88; i++) {
strip.setPixelColor(keyBuffer[i].keyLight1, strip.Color( 0, 50, 0, 0));
}

It can't convert the value.
Similar things were all over my sketch.

for(i = 1; i < 88; i++) {
uint_8 tempValue = keyBuffer[i].keyLight1;
strip.setPixelColor(tempValue, strip.Color( 0, 50, 0, 0));
}

Works correctly.

I guess I was unclear. To pass a value into a function, I have to first convert it to a variable of type.

Dr-Zee:
I guess I was unclear. To pass a value into a function, I have to first convert it to a variable of type.

Nope. The compiler knows the type of each struct element. If .keyLight is of type uint8_t and you weren't accessing non-existent array elements and the .keyLight value contained in the array element accessed was valid for the size of the LED strip, it would have worked. Something else must have been wrong.

Something else must have been wrong.

That would be my conclusion too

It can't convert the value.
Similar things were all over my sketch.

if that would be needed, than there might be other problems in your sketch.

In your struct you have defined:

 unsigned int  keyLight1 = 0;

now you try to convert:

uint_8 tempValue = keyBuffer[i].keyLight1;

not a good idea, what ever you think your current maximum value could be today.

All Adafruits setPixelColor methods take a uint16_t as first parameter:

void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b);
  
void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b,
 uint8_t w);
  
void setPixelColor(uint16_t n, uint32_t c);

--> if you define a uint16_t in your struct, you can hand over the variable by value.

show a complete compile-able sketch with as less external libraries as possible and some guys here may spot, what's wrong.

Dr-Zee:
The scope of my question is only the assignment of structs into an array. I'm not asking for any kind of diagnostic. The only question I have is: Is this how to create an array of structs. Any more info would be off topic. It's a large sketch, and I don't want anyone to feel like they have to troubleshoot it for me. Thank you for taking the time.