Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include <SPI.

h>
#include <MFRC522.h>
#include <EEPROM.h>

#define RST_PIN 9 // Define the reset pin


#define SS_PIN 10 // Define the SDA pin
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create an instance of the MFRC522 class

const int maxStoredCards = 10; // Maximum number of cards to store


const int cardDataSize = 4; // Size of card data (4 bytes for RFID cards)

struct StoredCard {
uint32_t cardNumber;
bool timerRunning;
unsigned long startTime;
};

StoredCard storedCards[maxStoredCards];
uint32_t lastDetectedCardNumber = 0; // Store the last detected card number

const int paymentRate = 10; // Payment rate per second in Rs

void setup() {
Serial.begin(9600); // Initialize serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate the RFID module

// Initialize stored card data


for (int i = 0; i < maxStoredCards; i++) {
storedCards[i].cardNumber = 0;
storedCards[i].timerRunning = false;
storedCards[i].startTime = 0;
}

Serial.println("Place an RFID card near the reader...");


}

void loop() {
if (mfrc522.PICC_IsNewCardPresent()) {
if (mfrc522.PICC_ReadCardSerial()) {
uint32_t currentCardNumber = 0;
for (byte i = 0; i < mfrc522.uid.size; i++) {
currentCardNumber = (currentCardNumber << 8) | mfrc522.uid.uidByte[i];
}

// Check if the card is already stored


int storedCardIndex = findStoredCard(currentCardNumber);

if (storedCardIndex != -1) {
// Same card detected again, stop the timer and remove the card data
unsigned long elapsedTime = stopTimer(storedCardIndex);
removeStoredCard(storedCardIndex);
Serial.print("Card removed. Timer stopped. Card Number: ");
Serial.println(currentCardNumber);
Serial.print("Elapsed Time (s): ");
Serial.println(elapsedTime / 1000);
displayPayment(elapsedTime);
} else {
// New card detected, start the timer and store the card data
int emptySlot = findEmptySlot();
if (emptySlot != -1) {
startTimer(emptySlot, currentCardNumber);
storeCardData(emptySlot, currentCardNumber);
lastDetectedCardNumber = currentCardNumber; // Store the last detected
card number
Serial.print("Card detected. Timer started. Card Number: ");
Serial.println(currentCardNumber);
} else {
Serial.println("Storage is full. Can't store more cards.");
}
}

mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();
}
}

// Check and manage timers for all stored cards


for (int i = 0; i < maxStoredCards; i++) {
if (storedCards[i].timerRunning) {
// Check timer and perform actions while the timer is running for each stored
card
unsigned long elapsedTime = millis() - storedCards[i].startTime;
// Do something with the elapsed time
// ...

// If you want to stop the timer after a certain period, you can add a
condition here
// For example, if (elapsedTime >= someThreshold) stopTimer(i);
}
}
}

int findStoredCard(uint32_t cardNumber) {


for (int i = 0; i < maxStoredCards; i++) {
if (storedCards[i].cardNumber == cardNumber) {
return i;
}
}
return -1; // Card not found
}

int findEmptySlot() {
for (int i = 0; i < maxStoredCards; i++) {
if (storedCards[i].cardNumber == 0) {
return i;
}
}
return -1; // No empty slot found
}

void startTimer(int cardIndex, uint32_t cardNumber) {


storedCards[cardIndex].cardNumber = cardNumber;
storedCards[cardIndex].timerRunning = true;
storedCards[cardIndex].startTime = millis();
}

unsigned long stopTimer(int cardIndex) {


storedCards[cardIndex].timerRunning = false;
return millis() - storedCards[cardIndex].startTime;
}

void removeStoredCard(int cardIndex) {


storedCards[cardIndex].cardNumber = 0;
}

void storeCardData(int cardIndex, uint32_t cardNumber) {


// You can store additional data or perform actions when a card is stored
// Here, we simply store the card number in EEPROM
int address = cardIndex * cardDataSize;
EEPROM.put(address, cardNumber);
}

void displayPayment(unsigned long elapsedTime) {


// Calculate and display the payment amount
float paymentAmount = (float)elapsedTime / 1000 * paymentRate;
Serial.print("Payment Amount (Rs): ");
Serial.println(paymentAmount);
}

You might also like