Below is the the code im using im trying to find a way to add more than one card that has access to the lock. for an example i have these 2 card ids i would like to have access "BA4 23 ED 80 & 53 3A 32 98"
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#define LIST_SIZE 2
#define SS_PIN 10
#define RST_PIN 9
String UID = "BA 42 ED 80";
byte lock = 0;
Servo servo;
LiquidCrystal_I2C lcd(0x27,16,2);
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
servo.write(180);
lcd.init();
lcd.backlight();
servo.attach(3);
SPI.begin();
rfid.PCD_Init();
}
void loop() {
lcd.setCursor(4,0);
lcd.print("W");
lcd.print("e");
lcd.print("l");
lcd.print("c");
lcd.print("o");
lcd.print("m");
lcd.print("e");
lcd.setCursor(1, 1);
lcd.print("S");
lcd.print("c");
lcd.print("a");
lcd.print("n");
lcd.print(" ");
lcd.print("C");
lcd.print("a");
lcd.print("r");
lcd.print("d");
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("S");
lcd.print("c");
lcd.print("a");
lcd.print("n");
lcd.print("n");
lcd.print("i");
lcd.print("n");
lcd.print("g");
Serial.print("nicks card:");
String ID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
lcd.print(".");
ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
ID.concat(String(rfid.uid.uidByte[i], HEX));
delay(300);
}
ID.toUpperCase();
if (ID.substring(1) == UID && lock == 0 ) {
servo.write(25);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D");
lcd.print("o");
lcd.print("o");
lcd.print("r");
lcd.print("'");
lcd.print("s");
lcd.print(" ");
lcd.print("O");
lcd.print("p");
lcd.print("e");
lcd.print("n");
delay(1500);
lcd.clear();
lock = 1;
} else if (ID.substring(1) == UID && lock == 1 ) {
servo.write(160);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D");
lcd.print("o");
lcd.print("o");
lcd.print("r");
lcd.print("'");
lcd.print("s");
lcd.print(" ");
lcd.print("L");
lcd.print("o");
lcd.print("c");
lcd.print("k");
lcd.print("e");
lcd.print("d");
delay(1500);
lcd.clear();
lock = 0;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong card!");
delay(1500);
lcd.clear();
}
}
Hi whitewolf,
welcome to the forum
you should post your code as a code-section.
So please RE -edit your posting
Hi newcomer,
This thread wants to show you how you can speed-up getting helpful answers with small effort.
If you manage at least to post this way. You have the sympathy of the experienced users.
If you are willing to invest another 20 minutes these 20 minutes extra-time will save you a minimum of 200 minutes to finish your project.
always
post your full sketch. As a
code-section
from the very first to the very last line
A code-section has a small height => keeps your posting "short" but…
red_car
November 25, 2022, 6:07am
3
whitew0lf:
lcd.print("W");
You know you can send more than one character at a time to the LCD?
You could put valid cards in an array...
#define LIST_SIZE 2
String UIDList[LIST_SIZE] = {"BA 42 ED 80",
"53 3A 32 98"};
...and create a routine to check if the card you read is in the list...
boolean validCard (String checkCard)
{
for (uint8_t x = 0; x < LIST_SIZE; x++)
{
if (checkCard == UIDList[x])
return true;
}
return false;
}
... and then replace this...
if (ID.substring(1) == UID &&
with this...
if (validCard(ID.substring(1)) &&
not sure how to post it as a code section
Did you read this tutorial?
Hi newcomer,
This thread wants to show you how you can speed-up getting helpful answers with small effort.
If you manage at least to post this way. You have the sympathy of the experienced users.
If you are willing to invest another 20 minutes these 20 minutes extra-time will save you a minimum of 200 minutes to finish your project.
always
post your full sketch. As a
code-section
from the very first to the very last line
A code-section has a small height => keeps your posting "short" but…
got it in a code section now
well done. As an additional hint: Whenever you want to post a modificated code
post the code in a new posting. Posting new postings is the usual way.
It is really accepted if a thread becomes hundreds of posts long.
this is what ive got so far but when i upload it theres a problem "could not convert 'servo.Servo::write(25)' from 'void' to 'bool' pops up and highlights "servo.write(25);"
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#define LIST_SIZE 2
#define SS_PIN 10
#define RST_PIN 9
String UIDList[LIST_SIZE] = {"BA 42 ED 80",
"53 3A 32 98"};
byte lock = 0;
boolean validCard (String checkCard)
{
for (uint8_t x = 0; x < LIST_SIZE; x++)
{
if (checkCard == UIDList[x])
return true;
}
return false;
}
Servo servo;
LiquidCrystal_I2C lcd(0x27,16,2);
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
servo.write(180);
lcd.init();
lcd.backlight();
servo.attach(3);
SPI.begin();
rfid.PCD_Init();
}
void loop() {
lcd.setCursor(4,0);
lcd.print("W");
lcd.print("e");
lcd.print("l");
lcd.print("c");
lcd.print("o");
lcd.print("m");
lcd.print("e");
lcd.setCursor(1, 1);
lcd.print("S");
lcd.print("c");
lcd.print("a");
lcd.print("n");
lcd.print(" ");
lcd.print("C");
lcd.print("a");
lcd.print("r");
lcd.print("d");
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("S");
lcd.print("c");
lcd.print("a");
lcd.print("n");
lcd.print("n");
lcd.print("i");
lcd.print("n");
lcd.print("g");
Serial.print("nicks card:");
String ID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
lcd.print(".");
ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
ID.concat(String(rfid.uid.uidByte[i], HEX));
delay(300);
}
ID.toUpperCase();
if (validCard(ID.substring(1)) &&
servo.write(25);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D");
lcd.print("o");
lcd.print("o");
lcd.print("r");
lcd.print("'");
lcd.print("s");
lcd.print(" ");
lcd.print("O");
lcd.print("p");
lcd.print("e");
lcd.print("n");
delay(1500);
lcd.clear();
lock = 1;
} else if (ID.substring(1) == UID && lock == 1 ) {
servo.write(160);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D");
lcd.print("o");
lcd.print("o");
lcd.print("r");
lcd.print("'");
lcd.print("s");
lcd.print(" ");
lcd.print("L");
lcd.print("o");
lcd.print("c");
lcd.print("k");
lcd.print("e");
lcd.print("d");
delay(1500);
lcd.clear();
lock = 0;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong card!");
delay(1500);
lcd.clear();
}
}
if (validCard(ID.substring(1)) &&
servo.write(25);
The line with the "if" has a boolean and-operator "&& "behind the closing parenthesis
if (validCard(ID.substring(1)) &&
This makes the compiler expextion a boolean expression
but the function-call
servo.write(25);
does not return a boolean value
I guess this should be simply a semicolon
if (validCard(ID.substring(1)) ;
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#define LIST_SIZE 2
#define SS_PIN 10
#define RST_PIN 9
String UIDList[LIST_SIZE] = {"BA 42 ED 80",
"53 3A 32 98"};
byte lock = 0;
if (validCard(ID.substring(1)) ;
Servo servo;
LiquidCrystal_I2C lcd(0x27,16,2);
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
servo.write(180);
lcd.init();
lcd.backlight();
servo.attach(3);
SPI.begin();
rfid.PCD_Init();
}
void loop() {
lcd.setCursor(4,0);
lcd.print("W");
lcd.print("e");
lcd.print("l");
lcd.print("c");
lcd.print("o");
lcd.print("m");
lcd.print("e");
lcd.setCursor(1, 1);
lcd.print("S");
lcd.print("c");
lcd.print("a");
lcd.print("n");
lcd.print(" ");
lcd.print("C");
lcd.print("a");
lcd.print("r");
lcd.print("d");
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("S");
lcd.print("c");
lcd.print("a");
lcd.print("n");
lcd.print("n");
lcd.print("i");
lcd.print("n");
lcd.print("g");
Serial.print("nicks card:");
String ID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
lcd.print(".");
ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
ID.concat(String(rfid.uid.uidByte[i], HEX));
delay(300);
}
ID.toUpperCase();
if (validCard(ID.substring(1)) &&
servo.write(25);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D");
lcd.print("o");
lcd.print("o");
lcd.print("r");
lcd.print("'");
lcd.print("s");
lcd.print(" ");
lcd.print("O");
lcd.print("p");
lcd.print("e");
lcd.print("n");
delay(1500);
lcd.clear();
lock = 1;
} else if (ID.substring(1) == UID && lock == 1 ) {
servo.write(160);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D");
lcd.print("o");
lcd.print("o");
lcd.print("r");
lcd.print("'");
lcd.print("s");
lcd.print(" ");
lcd.print("L");
lcd.print("o");
lcd.print("c");
lcd.print("k");
lcd.print("e");
lcd.print("d");
delay(1500);
lcd.clear();
lock = 0;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong card!");
delay(1500);
lcd.clear();
}
}
like this?
whitew0lf:
like this?
No. I'm sorry this makes no sense at all.
I was too quick with my last reply
I guess you want the lines of code below the if-conditions executed only if the if-condition is true
so it should be like this
.
complete sketch:
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#define LIST_SIZE 2
#define SS_PIN 10
#define RST_PIN 9
String UIDList[LIST_SIZE] = {"BA 42 ED 80",
"53 3A 32 98"
};
byte lock = 0;
boolean validCard (String checkCard) {
for (uint8_t x = 0; x < LIST_SIZE; x++) {
if (checkCard == UIDList[x])
return true;
}
return false;
}
Servo servo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Serial.begin(9600);
servo.write(180);
lcd.init();
lcd.backlight();
servo.attach(3);
SPI.begin();
rfid.PCD_Init();
}
void loop() {
lcd.setCursor(4, 0);
lcd.print("W");
lcd.print("e");
lcd.print("l");
lcd.print("c");
lcd.print("o");
lcd.print("m");
lcd.print("e");
lcd.setCursor(1, 1);
lcd.print("S");
lcd.print("c");
lcd.print("a");
lcd.print("n");
lcd.print(" ");
lcd.print("C");
lcd.print("a");
lcd.print("r");
lcd.print("d");
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("S");
lcd.print("c");
lcd.print("a");
lcd.print("n");
lcd.print("n");
lcd.print("i");
lcd.print("n");
lcd.print("g");
Serial.print("nicks card:");
String ID = "";
for (byte i = 0; i < rfid.uid.size; i++) {
lcd.print(".");
ID.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
ID.concat(String(rfid.uid.uidByte[i], HEX));
delay(300);
}
ID.toUpperCase();
if (validCard(ID.substring(1)) ) {
servo.write(25);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D");
lcd.print("o");
lcd.print("o");
lcd.print("r");
lcd.print("'");
lcd.print("s");
lcd.print(" ");
lcd.print("O");
lcd.print("p");
lcd.print("e");
lcd.print("n");
delay(1500);
lcd.clear();
lock = 1;
}
else if (ID.substring(1) == UID && lock == 1 ) {
//else if (ID.substring(1) == ID && lock == 1 ) {
servo.write(160);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("D");
lcd.print("o");
lcd.print("o");
lcd.print("r");
lcd.print("'");
lcd.print("s");
lcd.print(" ");
lcd.print("L");
lcd.print("o");
lcd.print("c");
lcd.print("k");
lcd.print("e");
lcd.print("d");
delay(1500);
lcd.clear();
lock = 0;
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong card!");
delay(1500);
lcd.clear();
}
}
what is UID ??
I haven't analysed the complete logic of your code.
You have to declare a variable "UID" if you want to use a variable named UID
this works for both key cards to open it now but wont lock it
You should clearly describe what you mean with "this "
The code I have posted does not compile
exit status 1
'UID' was not declared in this scope
This is the reason why I'm asking what is UID ?
If I shall give more support I want to suggest something:
adding serial debug-output with three macros that reduce writing three lines of code to a single line.
im not sure red_car said to add it. the lock is working but only to open it.
dear lovely whitew0lf,
can you please please be specific what you mean by "it" ?
And can you please answer my question about
best regards Stefan
im not sure what the UID is Red_Car repplied to me and said to add this in.
You could put valid cards in an array...
#define LIST_SIZE 2
String UIDList[LIST_SIZE] = {"BA 42 ED 80",
"53 3A 32 98"};
...and create a routine to check if the card you read is in the list...
boolean validCard (String checkCard)
{
for (uint8_t x = 0; x < LIST_SIZE; x++)
{
if (checkCard == UIDList[x])
return true;
}
return false;
}
... and then replace this...
if (ID.substring(1) == UID &&
with this...
if (validCard(ID.substring(1)) &&
Do you know what an array is?
Aha I understand. Red_Car aded an array where each element of the array holds one card-number
This is this part of the code
String UIDList[LIST_SIZE] = {
"BA 42 ED 80",
"53 3A 32 98"
};
So in your line of code
else if (ID.substring(1) == UID && lock == 1 ) {
Why did you wrote
else if (ID.substring(1) == UID && lock == 1 ) {
???
oh that part was added in from who ever made the original file. not all of the file is my work.
as the code is now the code does not compile
the only things im using is an Arduino uno with an rfid reader and a lcd screen and one servo