Forum Rule: Always post complete source code & details to reproduce any issue!

1. ## Array Confusion

Hi Guys,

Need a little help with the following array situation.

I have 16 arrays from patternA through to patternP each with 16 elements:
uint8_t patternA;
uint8_t patternB;
uint8_t patternC;
..
..
..
uint8_t patternP;

What I'm trying to achieve is an 'Array' of 'arrays' of sorts.
So when I create a 'for' loop I can cycle through and retrieve each element in each pattern.

Please see my example code. Its an example of going through ONLY two patterns, however rather than writing 16 of them, I would like to create a loop.

Code:
```void recallPattern() {
if (pattern == 1) {
clearPattern();
for (int i = 0; i < 16; i++) {
if (patternA_StepState[i] == 1) {
stepState[i] = 1;
}
if (patternA_buttonFlag[i] == true) {
buttonFlag[i] = true;
}
}
}
if (pattern == 2) {
clearPattern();
for (int i = 0; i < 16; i++) {
if (patternB_StepState[i] == 1) {
stepState[i] = 1;
}
if (patternB_buttonFlag[i] == true) {
buttonFlag[i] = true;
}
}
}
}```
This was the intial concept I thought of...

Code:
```uint8_t patternArray =
{ {patternA_StepState[]} , {patternB_StepState[]} ,
{patternC_StepState[]} , {patternD_StepState[]} ,
{patternE_StepState[]} , {patternF_StepState[]} ,
{patternG_StepState[]} , {patternH_StepState[]} ,
{patternI_StepState[]} , {patternJ_StepState[]} ,
{patternK_StepState[]} , {patternL_StepState[]} ,
{patternM_StepState[]} , {patternN_StepState[]} ,
{patternO_StepState[]} , {patternP_StepState[]} ,
}```
Then I thought and tried using a struct, however that didn't seem to work.

Code:
```struct Pattern {
uint8_t buttonFlag;
uint8_t stepState;
};

Pattern patternArg;

void recallPattern() {
for (int i = 0; i < 16; i++) {
if (pattern == i) {
clearPattern();
if (patternArg[i].stepState[i] == 1) {
stepState[i] = 1;
}
if (patternArg[i].buttonFlag[i] == true) {
buttonFlag[i] = true;
}
}
}
}```
Any thoughts or help would be great   Reply With Quote

2. Since you know the value of variable pattern, there is no need to search for it.

Code:
```struct Pattern {
uint8_t buttonFlag;
uint8_t stepState;
};

Pattern patternArg;

void recallPattern() {
clearPattern();                    // I think this should probably be outside the loop
for (int i = 0; i < 16; i++) {
// if (pattern == i) {              remove this if statment
// clearPattern();
if (patternArg[pattern].stepState[i] == 1) {     // the structure index is pattern
stepState[i] = 1;
}
if (patternArg[pattern].buttonFlag[i] == true) {
buttonFlag[i] = true;
}
// }
}
}```  Reply With Quote

3. Maybe like this?

Code:
```uint8_t patternA;
uint8_t patternB;
uint8_t patternC;

uint8_t * patternList = {patternA, patternB, patternC};

void printPattern(int num) {
if (num >=0 && num < 3) {
uint8_t *pattern;
pattern = patternList[num];
for (int i=0; i < 16; i++) {
Serial.print("pattern ");
Serial.print(num);
Serial.print(" element ");
Serial.print(i);
Serial.print(" is ");
Serial.print(pattern[i]);
Serial.println();
}
}
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•