How To Check If String Value Is In Array - Arduino Forum
Maybe your like
I am working with a RFID scanner (RC522) and I want to store the Card and Tag UID's in an array and then when I scan the card check the array for the matching UID if there is a match it shows a message of unlocked if no match it shows message of denied.
I have the array set up but am unsure how to check all the values in the array. Below is the basics of what I have so far. What I have works with the OR statements but I wanted to use another method so I didn't need to add more and more OR statements and make it easier to add new cards or tags. Any help will be appreciated.
String cards[] = {"12 12 12 12","13 13 13 13"}; String tags[] = {"11 11 11 11", "22 22 22 22"}; int cardArry = 2; int tagArry = 2; if (content.substring(1) == cards[0] || content.substring(1) == tags[0] || content.substring(1) == tags[1] || content.substring(1) == cards[1]) { //do something } else { //do somethin } adwsystems October 10, 2018, 2:50pm 2Don't use String. Use char arrays, then use the strstr() function. Then in the case of your situation, make a loop and set a value and break when true (standard while loop search method).
groundFungus October 10, 2018, 3:01pm 3Why Strings are bad and how to use c-strings in their place.
jlusco October 10, 2018, 3:35pm 4adwsystems: Don't use String. Use char arrays, then use the strstr() function. Then in the case of your situation, make a loop and set a value and break when true (standard while loop search method).
I am at a total loss on the while loop and strsrt().
jlusco October 10, 2018, 3:43pm 5So the below works and since I have it working I can go with it but the issue I have is if I add another OR it breaks and all reads fail.
Works with 2 UID's:
#include <SPI.h> #include <MFRC522.h> #include <Servo.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SS_PIN 10 #define RST_PIN 9 #define LED_G 4 //define green LED pin #define LED_R 5 //define red LED #define LED_L 6 //Locked Pin #define BUZZER 2 //buzzer pin MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. Servo myServo; //define servo name #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); void setup() { Serial.begin(9600); // Initiate a serial communication SPI.begin(); // Initiate SPI bus mfrc522.PCD_Init(); // Initiate MFRC522 myServo.attach(3); //servo pin myServo.write(0); //servo start position pinMode(LED_G, OUTPUT); pinMode(LED_R, OUTPUT); pinMode(LED_L, OUTPUT); pinMode(BUZZER, OUTPUT); noTone(BUZZER); Serial.println("Put your card to the reader..."); Serial.println(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) // Clear the buffer. display.clearDisplay(); display.display(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(1); display.setCursor(0, 0); display.print("Please swipe card or tag to enter"); display.display(); } void loop() { digitalWrite(LED_G, LOW); digitalWrite(LED_L, HIGH); // Look for new cards if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } //Show UID on serial monitor Serial.print("UID tag :"); String content = ""; byte letter; for (byte i = 0; i < mfrc522.uid.size; i++) { Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i], HEX); content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); content.concat(String(mfrc522.uid.uidByte[i], HEX)); } Serial.println(); Serial.print("Message : "); content.toUpperCase(); if (content.substring(1) == "12 C3 12 12 " || content.substring(1) == "83 23 38 BB") //change here the UID of the card/cards that you want to give access { Serial.println("Authorized access"); Serial.println(); delay(500); display.clearDisplay(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(25, 0); display.print("Access"); display.setCursor(20, 15); display.print("Granted"); display.display(); digitalWrite(LED_G, HIGH); digitalWrite(LED_L, LOW); tone(BUZZER, 500); delay(300); noTone(BUZZER); myServo.write(180); delay(5000); myServo.write(0); digitalWrite(LED_G, LOW); display.clearDisplay(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(20, 15); display.print("Locked"); display.display(); delay(1000); display.clearDisplay(); display.display(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(1); display.setCursor(0, 0); display.print("Please swipe card or tag to enter"); display.display(); } else { Serial.println(" Access denied"); delay(500); display.clearDisplay(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(25, 0); display.print("Access"); display.setCursor(20, 15); display.print("Denied"); display.display(); digitalWrite(LED_R, HIGH); digitalWrite(LED_L, LOW); tone(BUZZER, 300); delay(1000); digitalWrite(LED_R, LOW); noTone(BUZZER); display.clearDisplay(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(20, 15); display.print("Locked"); display.display(); delay(1000); display.clearDisplay(); display.display(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(1); display.setCursor(0, 0); display.print("Please swipe card or tag to enter"); display.display(); } }Causes all UID's to fail:
#include <SPI.h> #include <MFRC522.h> #include <Servo.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SS_PIN 10 #define RST_PIN 9 #define LED_G 4 //define green LED pin #define LED_R 5 //define red LED #define LED_L 6 //Locked Pin #define BUZZER 2 //buzzer pin MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. Servo myServo; //define servo name #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); void setup() { Serial.begin(9600); // Initiate a serial communication SPI.begin(); // Initiate SPI bus mfrc522.PCD_Init(); // Initiate MFRC522 myServo.attach(3); //servo pin myServo.write(0); //servo start position pinMode(LED_G, OUTPUT); pinMode(LED_R, OUTPUT); pinMode(LED_L, OUTPUT); pinMode(BUZZER, OUTPUT); noTone(BUZZER); Serial.println("Put your card to the reader..."); Serial.println(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) // Clear the buffer. display.clearDisplay(); display.display(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(1); display.setCursor(0, 0); display.print("Please swipe card or tag to enter"); display.display(); } void loop() { digitalWrite(LED_G, LOW); digitalWrite(LED_L, HIGH); // Look for new cards if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } //Show UID on serial monitor Serial.print("UID tag :"); String content = ""; byte letter; for (byte i = 0; i < mfrc522.uid.size; i++) { Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i], HEX); content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); content.concat(String(mfrc522.uid.uidByte[i], HEX)); } Serial.println(); Serial.print("Message : "); content.toUpperCase(); if (content.substring(1) == "61 A4 70 08" || content.substring(1) == "83 23 38 BB" || content.substring(1) == "83 23 38 BB") //change here the UID of the card/cards that you want to give access { Serial.println("Authorized access"); Serial.println(); delay(500); display.clearDisplay(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(25, 0); display.print("Access"); display.setCursor(20, 15); display.print("Granted"); display.display(); digitalWrite(LED_G, HIGH); digitalWrite(LED_L, LOW); tone(BUZZER, 500); delay(300); noTone(BUZZER); myServo.write(180); delay(5000); myServo.write(0); digitalWrite(LED_G, LOW); display.clearDisplay(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(20, 15); display.print("Locked"); display.display(); delay(1000); display.clearDisplay(); display.display(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(1); display.setCursor(0, 0); display.print("Please swipe card or tag to enter"); display.display(); } else { Serial.println(" Access denied"); delay(500); display.clearDisplay(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(25, 0); display.print("Access"); display.setCursor(20, 15); display.print("Denied"); display.display(); digitalWrite(LED_R, HIGH); digitalWrite(LED_L, LOW); tone(BUZZER, 300); delay(1000); digitalWrite(LED_R, LOW); noTone(BUZZER); display.clearDisplay(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(20, 15); display.print("Locked"); display.display(); delay(1000); display.clearDisplay(); display.display(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(1); display.setCursor(0, 0); display.print("Please swipe card or tag to enter"); display.display(); } }This was why OI thought the array was a better solution. I do apologize as I am still learning all of this.
sterretje October 10, 2018, 3:57pm 6You're still using String (capital S) and might have encountered why you should not use them.
jlusco October 10, 2018, 5:45pm 7Here is an example that sets the UID as an INT array. I cant figure out how to add more UID's to this either.
#include <MFRC522.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <SPI.h> #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); #define SS_PIN 10 #define RST_PIN 9 #define LED_G 4 //define green LED pin #define LED_R 5 //define red LED #define LED_L 6 //Locked Pin #define BUZZER 2 //buzzer pin MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class MFRC522::MIFARE_Key key; int code[] = {97, 164, 112, 8}; //This is the stored UID {129,216,56,26} int codeRead = 0; String uidString; void setup() { pinMode(LED_G, OUTPUT); pinMode(LED_R, OUTPUT); pinMode(LED_L, OUTPUT); pinMode(BUZZER, OUTPUT); noTone(BUZZER); Serial.begin(9600); SPI.begin(); // Init SPI bus rfid.PCD_Init(); // Init MFRC522 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) // Clear the buffer. display.clearDisplay(); display.display(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(10, 0); display.print("RFID Lock"); display.display(); } void loop() { digitalWrite(LED_G, LOW); digitalWrite(LED_L, HIGH); if ( rfid.PICC_IsNewCardPresent()) { readRFID(); } delay(100); } void readRFID() { rfid.PICC_ReadCardSerial(); Serial.print(F("\nPICC type: ")); MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); Serial.println(rfid.PICC_GetTypeName(piccType)); // Check is the PICC of Classic MIFARE type if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) { Serial.println(F("Your tag is not of type MIFARE Classic.")); return; } clearUID(); Serial.println("Scanned PICC's UID:"); printDec(rfid.uid.uidByte, rfid.uid.size); uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]); printUID(); int i = 0; boolean match = true; while (i < rfid.uid.size) { if (!(rfid.uid.uidByte[i] == code[i])) { match = false; } i++; } if (match) { Serial.println("\nI know this card!"); printUnlockMessage(); } else { Serial.println("\nUnknown Card"); digitalWrite(LED_R, HIGH); digitalWrite(LED_L, LOW); delay(2000); digitalWrite(LED_R, LOW); digitalWrite(LED_L, HIGH); } // Halt PICC rfid.PICC_HaltA(); // Stop encryption on PCD rfid.PCD_StopCrypto1(); } void printDec(byte *buffer, byte bufferSize) { for (byte i = 0; i < bufferSize; i++) { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], DEC); } } void clearUID() { display.setTextColor(BLACK); // or BLACK); display.setTextSize(1); display.setCursor(30, 20); display.print(uidString); display.display(); } void printUID() { display.setTextColor(WHITE); // or BLACK); display.setTextSize(1); display.setCursor(0, 20); display.print("UID: "); display.setCursor(30, 20); display.print(uidString); display.display(); } void printUnlockMessage() { display.display(); display.setTextColor(BLACK); // or BLACK); display.setTextSize(2); display.setCursor(10, 0); display.print("RFID Lock"); display.display(); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(10, 0); display.print("Unlocked"); display.display(); digitalWrite(LED_G, HIGH); digitalWrite(LED_L, LOW); delay(2000); display.setTextColor(BLACK); // or BLACK); display.setTextSize(2); display.setCursor(10, 0); display.print("Unlocked"); display.setTextColor(WHITE); // or BLACK); display.setTextSize(2); display.setCursor(10, 0); display.print("RFID Lock"); display.display(); digitalWrite(LED_G, LOW); digitalWrite(LED_L, HIGH); } cattledog October 11, 2018, 1:09am 8I want to store the Card and Tag UID's in an array and then when I scan the card check the array for the matching UID if there is a match it shows a message of unlocked if no match it shows message of denied. I have the array set up but am unsure how to check all the values in the array.
It looks like you have an array of 4 bytes from the card read in the buffer mfrc522_uid_uidByte.
You do not need to use Strings.
Instead, you can test the read card array against stored approved ID's with memcmp. memcmp compares the entire array and you do not need a byte by byte comparison. http://www.cplusplus.com/reference/cstring/memcmp/
The allowed ID's are organized in a 2D byte array.
void setup() { Serial.begin(115200); //you have an ID from a card scan byte mfrc522_uid_uidByte[] = {0xC3, 0x7D, 0xDF, 0xC7}; byte allowedID[3][4] = { {0xC3, 0x7D, 0xDF, 0xC7}, {0xC3, 0x7D, 0xDF, 0xC6}, {0xC4, 0x7D, 0xDF, 0xC7}, }; Serial.print("Card ID read = "); for (int i = 0; i < 4; i++) { Serial.print(mfrc522_uid_uidByte[i],HEX); Serial.print(" "); } Serial.println(); Serial.println("Checking Match with allowed ID's"); for (int i = 0; i < 3; i++) { Serial.print("Testing Match with allowedID "); Serial.println(i); if (memcmp(mfrc522_uid_uidByte, allowedID[i], 4) == 0) Serial.println("Arrays Match"); else Serial.println("No Match"); } } void loop() { }Related topics
| Topic | Replies | Views | Activity |
|---|---|---|---|
| How to check a string from a existing string array. Programming | 3 | 930 | May 6, 2021 |
| Store and compare string Programming | 43 | 1279 | February 21, 2023 |
| RFID Check keys in ARRAY Programming | 9 | 2539 | May 5, 2021 |
| Array di stringhe - RFID Software | 19 | 1841 | May 7, 2021 |
| RFID Card Reader help Programming | 7 | 12987 | May 5, 2021 |
Tag » Arduino Find Text In Char Array
-
Find String In Char Array - Programming Questions - Arduino Forum
-
Searching String In Char Array - Arduino Forum
-
Find Substring In Array Of Chars, Parse It And Save To New Char Array
-
Finding A Specific Char In Array - Arduino Forum
-
Trouble Finding A Substring In A Char Array. Reading 2 UART Streams.
-
IndexOf() - Arduino Reference
-
Substring From A Char Array - Programming Questions - Arduino Forum
-
Get Words (actual Words) From A Char Array And See If It's In Another ...
-
Arduino String To Char Array Code Example - Grepper
-
Summary Of Strings With Arduino - AranaCorp
-
Arduino C++, Unable To Convert String To Char* - Stack Overflow
-
2.6 Understanding Character Strings In Arduino - ArduinoPlatform
-
Arduino String Functions - 免费编程教程
-
How To Get Char Array's Length - Arduino Stack Exchange