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];
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
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.
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.
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.
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);
}
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.
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.