Compare array to multi-dimesional array

I want to compare two arrays, one-dimesion array to multi-dimesional array. There is some error in my code, it only gets to first row and then exits loop, it only compares first row of multi-dimesional array. Caan someone help me with this? Thanks

#include <OneWire.h>

OneWire  reader1(2);            //reader 1 on pin 2


byte iButtons[][8] = {         //Aproved iButtons
  {1,41,215,246,1,0,0,193},
  {1,211,59,11,1,0,0,107}
};

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

void loop() {
  byte iButton[8]={};
  bool access = false;
  
  reader1.reset_search();
  reader1.search(iButton);


  if(iButton[0] != 0){
    for(int i = 0; i < 2; i++){
      for(int j = 0; j < 8; j++ ){
        if(iButton[j] != iButtons[i][j]) {
          access = false;
          break;
        } else {
          access = true;
        }
       }
    }
    if(access){
      Serial.println("Access Granted!");
    } else {
      Serial.println("Access Denied!");
    }
  }
  delay(500);
}

You should break the outside for loop if you got a match on all eight elements
At the moment you still go to through all of them and thus will fail unless the match is with the last array

Using memcmp makes life easier :wink:

  if (iButton[0] != 0)
  {
    for (int i = 0; i < 2; i++)
    {
      if (memcmp(iButton, iButtons[i], sizeof(iButton)) == 0)
      {
        access =true;
        break;
      }
    }
    if (access)
    {
      Serial.println("Access Granted!");
    }
    else
    {
      Serial.println("Access Denied!");
    }
  }

J-M-L:
You should break the outside for loop if you got a match on all eight elements
At the moment you still go to through all of them and thus will fail unless the match is with the last array

With my code I get true if the match is with first array, it doesn't compare last array.

sterretje:
Using memcmp makes life easier :wink:

  if (iButton[0] != 0)

{
   for (int i = 0; i < 2; i++)
   {
     if (memcmp(iButton, iButtons[i], sizeof(iButton)) == 0)
     {
       access =true;
       break;
     }
   }
   if (access)
   {
     Serial.println(“Access Granted!”);
   }
   else
   {
     Serial.println(“Access Denied!”);
   }
 }

Thanks man :slight_smile: This is really elegant solution :slight_smile:

simbe84:
With my code I get true if the match is with first array, it doesn't compare last array.

you get true if there is a match with the first array (i=0) but you still enter the second loop with i=1 and will update the success flag to false and break.

J-M-L:
you get true if there is a match with the first array (i=0) but you still enter the second loop with i=1 and will update the success flag to false and break.

Ah, thanks, I can see it now. My mistake :slight_smile:

for(int i = 0; i < 2; i++){
      for(int j = 0; j < 8; j++ ){
        if(iButton[j] == iButtons[i][j]) {
          access = true;
        } else {
          access = false;
        }
      }
      if(access){
        break;
      }
    }

Yes and you can keep the break as well when you found a wrong entry - no need to continue checking that array

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.