|
| 1 | +/* |
| 2 | +* Project Name: RFID Transaction Management System |
| 3 | +* Author: Murangwa Pacifique |
| 4 | +* Co-Authors: Ighor - Tresor |
| 5 | +*/ |
| 6 | + |
| 7 | + |
| 8 | +#include <SPI.h> |
| 9 | +#include <MFRC522.h> |
| 10 | + |
| 11 | +#define RST_PIN 9 |
| 12 | +#define SS_PIN 10 |
| 13 | + |
| 14 | +MFRC522 mfrc522(SS_PIN, RST_PIN); |
| 15 | +MFRC522::MIFARE_Key key; |
| 16 | +MFRC522::StatusCode card_status; |
| 17 | + |
| 18 | +int moneyAmount = 0; |
| 19 | +int pointsAmount = 0; |
| 20 | + |
| 21 | +void setup() { |
| 22 | + Serial.begin(9600); |
| 23 | + SPI.begin(); |
| 24 | + mfrc522.PCD_Init(); |
| 25 | + |
| 26 | + Serial.println(F("\n ==== Place the card near the reader. ==== \n")); |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +void loop() { |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + for (byte i = 0; i < 6; i++) { |
| 35 | + key.keyByte[i] = 0xFF; |
| 36 | + } |
| 37 | + |
| 38 | + if (!mfrc522.PICC_IsNewCardPresent()) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (!mfrc522.PICC_ReadCardSerial()) { |
| 43 | + Serial.println(mfrc522.PICC_ReadCardSerial()); |
| 44 | + Serial.println(F("Error reading card.")); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + byte buffer[18]; // Increased buffer size to account for null character |
| 49 | + byte moneyBlock = 4; |
| 50 | + byte pointsBlock = 8; |
| 51 | + byte len; |
| 52 | + |
| 53 | + Serial.println(F("Card detected.")); |
| 54 | + |
| 55 | + // Authenticate moneyBlock to read money amount |
| 56 | + card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, moneyBlock, &key, &(mfrc522.uid)); |
| 57 | + if (card_status != MFRC522::STATUS_OK) { |
| 58 | + Serial.println(F("Authentication error for moneyBlock.")); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Read money amount from moneyBlock |
| 63 | + card_status = mfrc522.MIFARE_Read(moneyBlock, buffer, &len); |
| 64 | + if (card_status != MFRC522::STATUS_OK) { |
| 65 | + Serial.println(F("Error reading moneyBlock.")); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + buffer[len] = '\0'; // Null-terminate the buffer |
| 70 | + moneyAmount = atoi((char*)buffer); |
| 71 | + |
| 72 | + // Authenticate block 8 to read pointsBlock amount |
| 73 | + card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, pointsBlock, &key, &(mfrc522.uid)); |
| 74 | + if (card_status != MFRC522::STATUS_OK) { |
| 75 | + Serial.println(F("Authentication error for pointsBlock.")); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Read points amount from pointsBlock |
| 80 | + card_status = mfrc522.MIFARE_Read(pointsBlock, buffer, &len); |
| 81 | + if (card_status != MFRC522::STATUS_OK) { |
| 82 | + Serial.println(F("Error reading pointsBlock.")); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + buffer[len] = '\0'; // Null-terminate the buffer |
| 87 | + pointsAmount = atoi((char*)buffer); |
| 88 | + |
| 89 | + Serial.print(F("Available Money: $")); |
| 90 | + Serial.println(moneyAmount); |
| 91 | + Serial.print(F("Available Points: ")); |
| 92 | + Serial.println(pointsAmount); |
| 93 | + |
| 94 | + Serial.println(F("Enter 'm' to use money, 'p' to use points:")); |
| 95 | + |
| 96 | + while (Serial.available() <= 0) { |
| 97 | + //Wait for input |
| 98 | + } |
| 99 | + |
| 100 | + char input = Serial.read(); |
| 101 | + int amount = 0; |
| 102 | + |
| 103 | + if (input == 'm') { |
| 104 | + Serial.println(F("Enter the amount to deduct from money: ")); |
| 105 | + |
| 106 | + while (Serial.available() <= 1) { |
| 107 | + // Wait for input |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + amount = Serial.parseInt(); |
| 112 | + |
| 113 | + if (amount > moneyAmount) { |
| 114 | + Serial.println(F("Insufficient funds.")); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + moneyAmount -= amount; |
| 119 | + pointsAmount += 10; // Increased points by 10 because he/she used money |
| 120 | + |
| 121 | + Serial.println(F("Transaction completed successfully.")); |
| 122 | + } else if (input == 'p') { |
| 123 | + Serial.println(F("Enter the amount to deduct from points: ")); |
| 124 | + |
| 125 | + while (Serial.available() <= 1) { |
| 126 | + // Wait for input |
| 127 | + } |
| 128 | + |
| 129 | + amount = Serial.parseInt(); |
| 130 | + |
| 131 | + if (amount > pointsAmount) { |
| 132 | + Serial.println(F("Insufficient funds.")); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + pointsAmount -= amount; |
| 137 | + Serial.println(F("Transaction completed successfully.")); |
| 138 | + } else { |
| 139 | + Serial.println("Invalid Input."); |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + sprintf((char*)buffer, "%d", moneyAmount); |
| 144 | + card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, moneyBlock, &key, &(mfrc522.uid)); |
| 145 | + if (card_status != MFRC522::STATUS_OK) { |
| 146 | + Serial.println(F("Authentication error for moneyBlock.")); |
| 147 | + return; |
| 148 | + } |
| 149 | + card_status = mfrc522.MIFARE_Write(moneyBlock, buffer, 16); |
| 150 | + if (card_status != MFRC522::STATUS_OK) { |
| 151 | + Serial.println(F("Error writing to moneyBlock.")); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + sprintf((char*)buffer, "%d", pointsAmount); |
| 156 | + card_status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, pointsBlock, &key, &(mfrc522.uid)); |
| 157 | + |
| 158 | + if (card_status != MFRC522::STATUS_OK) { |
| 159 | + Serial.println(F("Authentication error for block 8.")); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + card_status = mfrc522.MIFARE_Write(pointsBlock, buffer, 16); |
| 164 | + |
| 165 | + if (card_status != MFRC522::STATUS_OK) { |
| 166 | + Serial.println(F("Error writing to pointsBlock.")); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + Serial.print(F("Remaining Money: $")); |
| 171 | + Serial.println(moneyAmount); |
| 172 | + Serial.print(F("Remaining Points: ")); |
| 173 | + Serial.println(pointsAmount); |
| 174 | + |
| 175 | + mfrc522.PICC_HaltA(); |
| 176 | + mfrc522.PCD_StopCrypto1(); |
| 177 | + |
| 178 | + Serial.println("Wait 10 seconds before making another transaction"); |
| 179 | + |
| 180 | + delay(10000); // Delay to allow card removal before the next transaction |
| 181 | +} |
0 commit comments