Comparing pool of INT's

Hello everyone. I have a problem with a project I'm working on. I'm building an access control system for multiple users. They all have unique passwords. What I'm trying to do is write an If statement that compares the users password to a string of passwords and if it matches access is allowed. As much as I research I don't know how to compare a single INT with a string or array of INT's This is what I have so far but obviously doesn't work. How can I compare a single INT with a pool of INT's. I just have it turning on the built in LED in this example just for testing purposes.

#include <RCSwitch.h>

int Pause = 2000;
int FrontDTPool[2] = { 12134312, 12134308 };
int RearDTPool[2] = { 12134308, 12134312 };
int FrontWBPool[2] = { 12134306, 12134312 };
int RearWBPool[2] = { 12134305, 12134312 };

unsigned long FrontDTMillis;
unsigned long RearDTMillis;
unsigned long FrontWBMillis;
unsigned long RearWBMillis;

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(D2);  // Receiver on interrupt 0 => that is pin #2
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
}

void loop() {

  if (mySwitch.available()) {

    Serial.print("Received ");
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(" / ");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println( mySwitch.getReceivedProtocol() );

    if ((mySwitch.getReceivedValue() == FrontDTPool[2]) && ((millis() - FrontDTMillis) >= Pause)) {
      FrontDTMillis = millis();
     digitalWrite(LED_BUILTIN, LOW);
    }

    if ((mySwitch.getReceivedValue() == RearDTPool[2]) && ((millis() - RearDTMillis) >= Pause)) {
      RearDTMillis = millis();
     digitalWrite(LED_BUILTIN, LOW);
    }

    if ((mySwitch.getReceivedValue() == FrontWBPool[2]) && ((millis() - FrontWBMillis) >= Pause)) {
      FrontWBMillis = millis();
      digitalWrite(LED_BUILTIN, LOW);
    }

    if ((mySwitch.getReceivedValue() == RearWBPool[2]) && ((millis() - RearWBMillis) >= Pause)) {
      RearWBMillis = millis();
      digitalWrite(LED_BUILTIN, LOW);
    }

    mySwitch.resetAvailable();
  }
}

compare each array element one at a time and set a flag if any are unequal. It's easy but it takes a few lines of code. I hope you know already that you can index an array like RearDTPool[i]...

What board are you using? None of those integer values will fit in a 16-bit integer, like on an UNO, Nano, Mega...

I'm using a NodeMCU D1 Mini. It works if I use the INT's separately. But combining them it doesn't seem to know what to do.

Did you used the for() loop to iterate each items of the password arrays to compare them with the user input?

You define an array with 2 elements below (index 0, index 1)

You try to access index 2 (array element 3) below... this is beyond the bounds of the array.

If you have them turned on you will be getting compiler warnings about this.

EDIT: If you are trying to compare to EACH element [ ] of the array then you have to loop through each element... you can't compare the whole array in one go.

You make verbal references to new code you've written. We can't see it. Please post it.

compare each value of the array with your readed number.

bool isValid(int hay[], size_t size, int needle)
{
  for (size_t i = 0; i < size; i++)
  {
    if (needle == hay[i]) return true;
  }
  return false;
}

handover the array of interest, the size of the array and the int to search (= the needle)

see:

// search a value in an array
// https://forum.arduino.cc/t/comparing-pool-of-ints/1029024
// to be deleted 2022-11-30

// #include <RCSwitch.h>     // we can't do nothing with includes without links 
constexpr int Pause = 2000;
int FrontDTPool[2] = { 12134312, 12134308 };
int RearDTPool[2] = { 12134308, 12134312 };
int FrontWBPool[2] = { 12134306, 12134312 };
int RearWBPool[2] = { 12134305, 12134312 };

unsigned long FrontDTMillis;
unsigned long RearDTMillis;
unsigned long FrontWBMillis;
unsigned long RearWBMillis;

// simulate the RCSwitch
struct RCSwitch
{
  int getReceivedValue() {return 12134306;}  // simulate a readed value
  bool available() {return true;}
  void resetAvailable(){;}
  int getReceivedBitlength() {return 2;}
  int getReceivedProtocol() {return 3;}
};
RCSwitch mySwitch = RCSwitch();

bool isValid(int hay[], size_t size, int needle)
{
  for (size_t i = 0; i < size; i++)
  {
    if (needle == hay[i]) return true;
  }
  return false;
}

void setup() {
  Serial.begin(9600);
  //mySwitch.enableReceive(D2);  // Receiver on interrupt 0 => that is pin #2
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  Serial.println(F("just some tests"));
  Serial.println(isValid(FrontDTPool, sizeof(FrontDTPool)/sizeof(FrontDTPool[0]), 12134312)); //OK
  Serial.println(isValid(FrontDTPool, sizeof(FrontDTPool)/sizeof(FrontDTPool[0]), 12345678)); //NOK
  delay(1000);
}

void loop() {

  if (mySwitch.available()) {

    Serial.print("Received ");
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(" / ");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println( mySwitch.getReceivedProtocol() );

    if (isValid(FrontDTPool, sizeof(FrontDTPool)/sizeof(FrontDTPool[0]), mySwitch.getReceivedValue()) && ((millis() - FrontDTMillis) >= Pause)) {
      Serial.println(F("FrontDTPool"));
      FrontDTMillis = millis();
     digitalWrite(LED_BUILTIN, LOW);
    }

    if (isValid(RearDTPool, sizeof(RearDTPool)/sizeof(RearDTPool[0]), mySwitch.getReceivedValue()) && ((millis() - RearDTMillis) >= Pause)) {
      Serial.println(F("RearDTPool"));
      RearDTMillis = millis();
     digitalWrite(LED_BUILTIN, LOW);
    }

    if (isValid(FrontWBPool, sizeof(FrontWBPool)/sizeof(FrontWBPool[0]), mySwitch.getReceivedValue()) && ((millis() - FrontWBMillis) >= Pause)) {
      Serial.println(F("FrontWBPool"));
      FrontWBMillis = millis();
      digitalWrite(LED_BUILTIN, LOW);
    }

    if (isValid(RearWBPool, sizeof(RearWBPool)/sizeof(RearWBPool[0]), mySwitch.getReceivedValue()) && ((millis() - RearWBMillis) >= Pause)) {
      Serial.println(F("RearWBPool"));
      RearWBMillis = millis();
      digitalWrite(LED_BUILTIN, LOW);
    }

    mySwitch.resetAvailable();
  }


  //while(1){}; //stop
  delay(500); // slow down a bit
}

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