Help with code

Hello! Im looking for help with this code. Im new to arduino and after a lot of research i dont understand why when my first "if" statement is true my "else" statement becomes true as well. When my second "if statement is true the "else statement is false and everything works the way it should. Any suggestions would be greatly appreciated.

// LiquidCrystal - Version: Latest 
#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>

/*
 * ----------------------------------------------------------------------------
 * This sketch uses the MFRC522 library ; see https://github.com/miguelbalboa/rfid
 * for further details and other examples.
 * 
 * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
 * 
 * This sketch show a simple locking mechanism using the RC522 RFID module.
 * ----------------------------------------------------------------------------
 * Typical pin layout used:
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno           Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
 *
 */
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN         9           // Configurable, see typical pin layout above
#define SS_PIN          10          // Configurable, see typical pin layout above
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

String read_rfid;
String ok_rfid_1="9a26685";
String ok_rfid_2="c4f1a85"; //add as many as you need.
int lock=7; //Which pin the lock will be on if using a relay or solenoid or similar 
int green=3;
int red=4;
int buzzer=5;
 

/*
 * Initialize.
 */
void setup() {
    Serial.begin(9600);         // Initialize serial communications with the PC
    while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
    SPI.begin();                // Init SPI bus
    mfrc522.PCD_Init();         // Init MFRC522 card
    //WE define our LCD 16 columns and 2 rows
lcd.begin(16,2);
lcd.backlight();//Power on the back light
//lcd.backlight(); Power off the back light
    //Choose which lock below:
    pinMode(lock, OUTPUT);
    pinMode(green,OUTPUT);
    pinMode(red,OUTPUT);
    pinMode(buzzer,OUTPUT);
}

/*
 * Helper routine to dump a byte array as hex values to Serial.
 */
void dump_byte_array(byte *buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
        read_rfid=read_rfid + String(buffer[i], HEX);
    }
}

void open_lock() {
  //Use this routine when working with Relays and Solenoids etc.
  digitalWrite(lock, HIGH);
  digitalWrite(green, HIGH);
  delay(4000);
  digitalWrite(lock,LOW);
  digitalWrite(green,LOW);
lcd.clear();
}

void loop() {

//Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;

//Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;

dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(read_rfid);
    
if (read_rfid==ok_rfid_1) {
lcd.setCursor(0,0); //we start writing from the first row first column
lcd.print("  ");//16 characters poer line
lcd.setCursor(0,1); //we start writing from the first row first column
lcd.print("ACCESS GRANTED");//16 characters poer line
digitalWrite(buzzer,HIGH);
delay(100);
digitalWrite(buzzer,LOW);
open_lock();
lcd.clear();
}  

 
//Add below as many "keys" as you want
if (read_rfid==ok_rfid_2) {
lcd.setCursor(0,0); //we start writing from the first row first column
lcd.print(" ");//16 characters poer line
lcd.setCursor(0,1); //we start writing from the first row first column
lcd.print("ACCESS GRANTED");//16 characters poer line
digitalWrite(buzzer,HIGH);
delay(100);
digitalWrite(buzzer,LOW);
open_lock();
lcd.clear();
}  

else  {
digitalWrite(red,HIGH);
lcd.print("ACCESS DENIED");
delay(2000);
digitalWrite(red,LOW);
lcd.clear();
}
}

after a lot of research

Not enough research to know where to post this question.

Ask a moderator to move this question.

Study the rules for posting on this web site.

.

Hi,

//Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;

//Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;

What do you expect the return; to do?

Might help with your code if structure.

Tom.... :slight_smile:

@jmoore160, please do not cross-post. Threads merged.

@jmoore160, if you use the AutoFormat tool to indent your code it will be much easier to see which blocks of code go together.

...R

Thanks Delta_G. I used the "else if" statement instead and it fixed my problem. Sorry for posting in the wrong forum. I recognize the error i made in posting. First time using the forum. Wont happen again.
Cheers!