I am making rotary car parking sysytem in which user press 1 for parking the place which is empty come down here motor is not stopping and rfid nowork

/*this code is of rotary car paring system which works like this 
if user press 1 then code will find empty space and every platform slot
 has give specifed angle and motor will check free space and stop and 
that desired angle 
if user press 2 to unpark then rfid code willl work every platform has 
attach rfid under it and user will enter platform which it want to bring down. 
code will check rfid id if id not match then move to next platform there it 
check id if not match then moveto next patform if id match then motor will 
stop there and it mark platform as occupied */
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MFRC522.h>
#include <Servo.h>
#include <Keypad.h>

#define TRIG_PIN 9
#define ECHO_PIN 10
#define SERVO_PIN 6
#define SS_PIN 9
#define RST_PIN 8

LiquidCrystal_I2C lcd(0x27, 16, 2);

MFRC522 rfid(SS_PIN, RST_PIN);
Servo platformServo;

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {2, 3, 4, 5};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

String platformIDs[] = {"B3 00 E0 0D", "ID2", "ID3", "ID4"};
bool platformStatus[] = {true, true, true, true};

void setup()
{
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Welcome to");
  lcd.setCursor(0, 1);
  lcd.print("Car Parking Sys");
  delay(2000);
  lcd.clear();

  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  platformServo.attach(SERVO_PIN);

  SPI.begin();
  rfid.PCD_Init();
}

void loop()
{
  lcd.print("Press 1 to park");
  lcd.setCursor(0, 1);
  lcd.print("Press 2 to unpark");

  char key = getKey();

  if (key == '1')
  {
    int emptyPlatform = findEmptyPlatform();
    if (emptyPlatform != -1)
    {
      lcd.clear();
      lcd.print("Parking...");
      delay(2000);
      lcd.clear();
      movePlatform(emptyPlatform);
      platformStatus[emptyPlatform] = false; // Mark platform as occupied
      delay(2000);
    }
    else
    {
      lcd.clear();
      lcd.print("No empty space");
      delay(2000);
      lcd.clear();
    }
  }
  else if (key == '2')
  {
    lcd.clear();
    lcd.print("Enter platform (1-4)");

    char platformKey;
    do {
      platformKey = getKey(); // Wait for the user to enter a key
    } while (platformKey == '\0'); // Keep waiting until a valid key is pressed

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Platform Key: ");
    lcd.setCursor(0, 1);
    lcd.print(platformKey);
    delay(3000);
    lcd.clear();
    int platformIndex = platformKey - '0' - 1;

    if (platformIndex >= 0 && platformIndex < 4)
    {
      lcd.print("Checking RFID...");
      delay(2000);
      lcd.clear();
      bool cardFound = false;
      int platformCounter = 0; // Initialize platform counter
      
      while (!cardFound && platformCounter < 4) // Continue until card is found or all platforms are checked
      {
        if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
          String rfidCardID = readRFID();
          Serial.println("expected id");
          Serial.print(platformIDs[platformIndex]);
          Serial.println("rfid id id");
          Serial.print(rfidCardID);
          if (rfidCardID == platformIDs[platformIndex]) {
            lcd.print("Match! Motor stopped");
            delay(2000);
            lcd.clear();
            movePlatform(platformIndex); // Park the platform
            cardFound = true;
            break;
          }
        }
        platformIndex = (platformIndex + 1) % 4; // Move to the next platform
        platformCounter++; // Increment platform counter
      }

      if (!cardFound) {
        lcd.print("RFID not matched. Moving motor...");
        movePlatform(platformIndex); // Move to the next platform
        delay(2000);
        lcd.clear();
        lcd.print("Invalid card");
        delay(2000);
        lcd.clear();
      }
    }
    else
    {
      lcd.print("Invalid platform");
      delay(2000);
      lcd.clear();
    }
  }
}

char getKey()
{
  char key = keypad.getKey();
  return key;
}

int findEmptyPlatform()
{
  for (int i = 0; i < 4; i++)
  {
    if (platformStatus[i])
    {
      return i;
    }
  }
  return -1;
}

String readRFID()
{
  if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial())
  {
    String rfidCardID = "";
    for (byte i = 0; i < rfid.uid.size; i++)
    {
      rfidCardID.concat(String(rfid.uid.uidByte[i] < 0x10 ? "0" : ""));
      rfidCardID.concat(String(rfid.uid.uidByte[i], HEX));
    }
    rfid.PICC_HaltA();
    return rfidCardID;
  }
  return "";
}

void movePlatform(int platformIndex)
{
  // Define the angles for each platform
  int platformAngles[] = {0, 60, 120, 180};
  
  if (!platformStatus[platformIndex]) {
    // If platform is occupied, stop at the corresponding angle
    platformServo.write(platformAngles[platformIndex]);
  } else {
    // If platform is empty, move to the specified angle
    platformServo.write(platformAngles[platformIndex]);
    // Optionally, add a delay to hold the position for some time
    delay(1000); // Adjust the delay as needed
    // After the delay, stop the motor by writing to a neutral angle (e.g., 90 degrees)
    platformServo.write(90); // Neutral position
  }
}

Please edit your post, select all code and click the <CODE/> button; next save your post.

This will make your code easier to read, easier to copy and the forum software will display it correctly. See How to get the best out of this forum.

1 Like

And what grade will your teacher give for the forum?

If you check you will find some of your classmates have already posted this question. What course is it for?

Electrical

University, high school, grade school, course # if applicable.

which communication protocol you are using ?

I am having same problem. Try DumpInfo

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