/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: RFID Potentiometer
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-09-28 04:21:45

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The system shall utilize an MFRC522 RFID reader to */
	/* capture card data, with a potentiometer connected */
	/* to analog pin A0 for input adjustments, ensuring */
	/* reliable communication via SPI protocol on defined */
	/* digital pins. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <SPI.h>
#include <MFRC522.h>	// https://github.com/miguelbalboa/rfid

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void readPotentiometer(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t MFRC522_IRQ_PIN_D3 = 3;

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t Potentiometer_Vout_PIN_A0 = A0;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t MFRC522_RST_PIN_D2 = 2;

/***** DEFINITION OF SPI PINS *****/
const uint8_t MFRC522_SPI_PIN_MOSI_D11 = 11;
const uint8_t MFRC522_SPI_PIN_MISO_D12 = 12;
const uint8_t MFRC522_SPI_PIN_SCLK_D13 = 13;
const uint8_t MFRC522_SPI_PIN_CS_D10 = 10;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool MFRC522_RST_PIN_D2_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float MFRC522_RST_PIN_D2_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Create MFRC522 instance with the defined CS and RST pins
MFRC522 mfrc522(MFRC522_SPI_PIN_CS_D10, MFRC522_RST_PIN_D2); // Instance of the MFRC522 class

byte nuidPICC[4]; // Array to store new NUID

void setup(void)
{
    // Initialize serial communication
    Serial.begin(9600);
    
    // Set pin modes
    pinMode(MFRC522_IRQ_PIN_D3, INPUT_PULLUP);
    pinMode(Potentiometer_Vout_PIN_A0, INPUT);
    pinMode(MFRC522_RST_PIN_D2, OUTPUT);
    pinMode(MFRC522_SPI_PIN_CS_D10, OUTPUT);
    
    // Start the SPI library:
    SPI.begin();
    
    // Initialize the MFRC522
    mfrc522.PCD_Init(); // Initialize MFRC522
    Serial.println(F("MFRC522 Initialized."));
}

void loop(void)
{
    // Read the potentiometer value and update outputs
    readPotentiometer(); // Read the potentiometer value
    updateOutputs(); // Refresh output data

    // Check for new RFID cards
    if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) return;

    Serial.print(F("PICC type: "));
    MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
    Serial.println(mfrc522.PICC_GetTypeName(piccType));

    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;
    }

    if (memcmp(mfrc522.uid.uidByte, nuidPICC, 4) != 0) {
        Serial.println(F("A new card has been detected."));
        memcpy(nuidPICC, mfrc522.uid.uidByte, 4);

        Serial.println(F("The NUID tag is:"));
        Serial.print(F("In hex: "));
        printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
        Serial.println();
        Serial.print(F("In dec: "));
        printDec(mfrc522.uid.uidByte, mfrc522.uid.size);
        Serial.println();
    } else {
        Serial.println(F("Card read previously."));
    }

    mfrc522.PICC_HaltA(); // Halt PICC
    mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}

// Function to read the potentiometer value
void readPotentiometer()
{
    int potValue = analogRead(Potentiometer_Vout_PIN_A0); // Read the analog value from the potentiometer
    MFRC522_RST_PIN_D2_rawData = (potValue > 512); // Example condition to set raw data based on potentiometer value
    MFRC522_RST_PIN_D2_phyData = potValue * (5.0 / 1023.0); // Convert raw value to physical data (0-5V)
}

// Function to update outputs based on raw data
void updateOutputs()
{
    digitalWrite(MFRC522_RST_PIN_D2, MFRC522_RST_PIN_D2_rawData); // Update the RST pin based on raw data
}

// Function to print byte array in hex format
void printHex(byte *buffer, byte bufferSize) {
    for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], HEX);
    }
}

// Function to print byte array in decimal format
void printDec(byte *buffer, byte bufferSize) {
    for (byte i = 0; i < bufferSize; i++) {
        Serial.print(buffer[i] < 0x10 ? " 0" : " ");
        Serial.print(buffer[i], DEC);
    }
}

/* END CODE */