Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Array Confusion

  1. #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[16];
    uint8_t patternB[16];
    uint8_t patternC[16];
    ..
    ..
    ..
    uint8_t patternP[16];

    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[16][16] =
    { {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[16];
      uint8_t stepState[16];
    };
    
    Pattern patternArg[16];
    
    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
    Last edited by mtiger; 02-07-2018 at 04:22 PM.

  2. #2
    Senior Member
    Join Date
    May 2017
    Posts
    208
    Since you know the value of variable pattern, there is no need to search for it.

    Code:
    struct Pattern {
      uint8_t buttonFlag[16];
      uint8_t stepState[16];
    };
    
    Pattern patternArg[16];
    
    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;
          }
       // }
      }
    }

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,238
    Maybe like this?

    Code:
    uint8_t patternA[16];
    uint8_t patternB[16];
    uint8_t patternC[16];
    
    uint8_t * patternList[3] = {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();
        }
      }
    }

Posting Permissions

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