-help- rfid with sonar sensor and display door lock

I’m trying to make an RFID door lock system. Right now I’m just trying to get an output on the display but it doesn’t seem to be working too well. I want it to be when the door is at a certain distance (really close) the door would lock and when there is a large distance (when its open) it won’t lock the door. Right now my problem is that it doesn’t seem to be taking input from the sonar sensor and gets stuck on the door is the open message on the display when I try to use it.

I am a complete beginner and could use some help. If I’ve left any important details out just tell me.

this code is mostly parts taken from other things mashed togeither to make something.

#include <Adafruit_SSD1306.h>

#include <gfxfont.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN   9
#define SS_PIN    10
#define trigPin 7
#define echoPin 8
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);


long duration;
int distance;
int safetyDistance;
byte readCard[4];
char* myTags[100] = {};
int tagsCount = 0;
String tagID = "";
boolean successRead = false;
boolean correctTag = false;
boolean doorOpened = false;

// Create instances
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  // Initiating
  SPI.begin();        // SPI bus
  mfrc522.PCD_Init(); //  MFRC522
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE); // or BLACK);
  display.setTextSize(2);
  display.setCursor(10,0); 
  display.print("Door Locked");
  display.display();

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
  // Waits until a master card is scanned
  while (!successRead) {
    successRead = getID();
    if ( successRead == true) {
      myTags[tagsCount] = strdup(tagID.c_str()); // Sets the master tag into position 0 in the array
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE); // or BLACK);
  display.setTextSize(2);
  display.setCursor(10,0); 
  display.print("MasterLock");
  display.display();
      tagsCount++;
    }
  }
  successRead = false;
  printNormalModeMessage();
}

void loop() {
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  int distance = digitalRead(8);
  // If door is closed...
  if (distance > 100) {
    if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
      digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
      return;
    }
    if ( ! mfrc522.PICC_ReadCardSerial()) {   //Since a PICC placed get Serial and continue
      digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
      return;
    }
    tagID = "";
    // The MIFARE PICCs that we use have 4 byte UID
    for ( uint8_t i = 0; i < 4; i++) {  //
      readCard[i] = mfrc522.uid.uidByte[i];
      tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
    }
    tagID.toUpperCase();
    mfrc522.PICC_HaltA(); // Stop reading

    correctTag = false;
    // Checks whether the scanned tag is the master tag
    if (tagID == myTags[0]) {
       display.clearDisplay();
      display.display();
      display.setTextColor(WHITE); // or BLACK);
      display.setTextSize(2);
      display.setCursor(10,0); 
      display.print("Program mode");
      display.display();
         display.setTextColor(WHITE); // or BLACK);
    display.setTextSize(1);
    display.setCursor(0,20); 
    display.print("Add/Remove Tag");
    display.setCursor(30,20); 
    display.display();
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
      while (!successRead) {
        successRead = getID();
        if ( successRead == true) {
          for (int i = 0; i < 100; i++) {
            if (tagID == myTags[i]) {
              myTags[i] = "";
    display.clearDisplay();
    display.setTextColor(WHITE); // or BLACK);
    display.setTextSize(2);
    display.setCursor(10,0); 
    display.print("Tag Removed");
    display.display();;
              printNormalModeMessage();
              digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
              return;
            }
          }
          myTags[tagsCount] = strdup(tagID.c_str());
    display.clearDisplay();
    display.setTextColor(WHITE); // or BLACK);
    display.setTextSize(2);
    display.setCursor(10,0); 
    display.print("Tag Added");
    display.display();;
          printNormalModeMessage();
          tagsCount++;
          digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
          return;
        }
      }
    }
    successRead = false;
    // Checks whether the scanned tag is authorized
    for (int i = 0; i < 100; i++) {
      if (tagID == myTags[i]) {       
    display.clearDisplay();
    display.setTextColor(WHITE); // or BLACK);
    display.setTextSize(2);
    display.setCursor(10,0); 
    display.print("Unlocked");
    display.display();
        printNormalModeMessage();
 digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
        correctTag = true;
      }
    }
    if (correctTag == false) {
    display.clearDisplay();
    display.setTextColor(WHITE); // or BLACK);
    display.setTextSize(2);
    display.setCursor(10,0); 
    display.print("Ur bad");
    display.display();
      printNormalModeMessage();
      digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
    }
  }
  // If door is open...
  else {
    display.clearDisplay();    
    display.setTextColor(WHITE); // or BLACK);
    display.setTextSize(2);
    display.setCursor(10,0); 
    display.print("Door is open");
    display.display();
    digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
    while (!doorOpened) {
      distance = digitalRead(8);
      if (distance > 100) {
        doorOpened = true;
        return;
      }
    }
    doorOpened = false;
    delay(500);
    display.clearDisplay();    
    display.setTextColor(WHITE); // or BLACK);
    display.setTextSize(2);
    display.setCursor(10,0); 
    display.print("Locked");
    display.display();
    printNormalModeMessage();
    digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
  }
}

uint8_t getID() {
  // Getting ready for Reading PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
    return 0;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {   //Since a PICC placed get Serial and continue
    return 0;
  }
  tagID = "";
  for ( uint8_t i = 0; i < 4; i++) {  // The MIFARE PICCs that we use have 4 byte UID
    readCard[i] = mfrc522.uid.uidByte[i];
    tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); // Adds the 4 bytes in a single String variable
  }
  tagID.toUpperCase();
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;
}

void printNormalModeMessage() {
  delay(1500);
  digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
    display.clearDisplay();    
    display.setTextColor(WHITE); // or BLACK);
    display.setTextSize(2);
    display.setCursor(10,0); 
    display.print("-Acces Control-");
    display.display();
        display.setTextColor(WHITE); // or BLACK);
    display.setTextSize(1);
    display.setCursor(20,30); 
    display.print("Scan your tag!");
    display.display();
}

when the door is at a certain distance (really close) the door would lock

Distance to what? Use a microswitch to determine if the door is shut, then lock it.

this code is mostly parts taken from other things mashed togeither to make something.

Not a good idea. You should make sure that every part of the project (sonar, door lock) works on its own, and that you understand it, before putting several things together.

We strongly recommend that you try out the simple examples that come with the Arduino program development software, to learn the language and the special features of the Arduino, before tackling a complex project like this.

I am a complete beginner

Are you sure? No missing parts?

Right now I'm just trying to get an output on the display but it doesn't seem to be working too well.

How could it possibly work "too well"?

long duration;
int distance;

Why are these global? You should have a function that returns the distance, which you store in a local variable. The distance is a function of the time, which should be local to the distance measuring function.

char* myTags[100] = {};

Do you REALLY have 100 tags? Do you REALLY have enough memory to store all 100 tag IDs as strings?

String tagID = "";

Do you REALLY need crutches?

  int distance = digitalRead(8);

That is NOT how to get the distance from a PING sensor. You need to TIME how long it takes for the echo pin (which you named, but seem to have forgotten) to go HIGH after the trigger pin toggling sent the pulse.

Why are you creating a local variable with the same name as a global variable (which should NOT exist)?

  // If door is closed...
  if (distance > 100) {

What sensor are you using, exactly? The value returned by digitalRead() will be 0 or 1. Neither value is remotely close to being over 100.