Advertisement
pleasedontcode

RFID Potentiometer rev_01

Sep 27th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: RFID Potentiometer
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-09-28 04:21:45
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall utilize an MFRC522 RFID reader to */
  21.     /* capture card data, with a potentiometer connected */
  22.     /* to analog pin A0 for input adjustments, ensuring */
  23.     /* reliable communication via SPI protocol on defined */
  24.     /* digital pins. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SPI.h>
  29. #include <MFRC522.h>    // https://github.com/miguelbalboa/rfid
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void updateOutputs(void);
  35. void readPotentiometer(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t MFRC522_IRQ_PIN_D3 = 3;
  39.  
  40. /***** DEFINITION OF ANALOG INPUT PINS *****/
  41. const uint8_t Potentiometer_Vout_PIN_A0 = A0;
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t MFRC522_RST_PIN_D2 = 2;
  45.  
  46. /***** DEFINITION OF SPI PINS *****/
  47. const uint8_t MFRC522_SPI_PIN_MOSI_D11 = 11;
  48. const uint8_t MFRC522_SPI_PIN_MISO_D12 = 12;
  49. const uint8_t MFRC522_SPI_PIN_SCLK_D13 = 13;
  50. const uint8_t MFRC522_SPI_PIN_CS_D10 = 10;
  51.  
  52. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  53. /***** used to store raw data *****/
  54. bool MFRC522_RST_PIN_D2_rawData = 0;
  55.  
  56. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  57. /***** used to store data after characteristic curve transformation *****/
  58. float MFRC522_RST_PIN_D2_phyData = 0.0;
  59.  
  60. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  61. // Create MFRC522 instance with the defined CS and RST pins
  62. MFRC522 mfrc522(MFRC522_SPI_PIN_CS_D10, MFRC522_RST_PIN_D2); // Instance of the MFRC522 class
  63.  
  64. byte nuidPICC[4]; // Array to store new NUID
  65.  
  66. void setup(void)
  67. {
  68.     // Initialize serial communication
  69.     Serial.begin(9600);
  70.    
  71.     // Set pin modes
  72.     pinMode(MFRC522_IRQ_PIN_D3, INPUT_PULLUP);
  73.     pinMode(Potentiometer_Vout_PIN_A0, INPUT);
  74.     pinMode(MFRC522_RST_PIN_D2, OUTPUT);
  75.     pinMode(MFRC522_SPI_PIN_CS_D10, OUTPUT);
  76.    
  77.     // Start the SPI library:
  78.     SPI.begin();
  79.    
  80.     // Initialize the MFRC522
  81.     mfrc522.PCD_Init(); // Initialize MFRC522
  82.     Serial.println(F("MFRC522 Initialized."));
  83. }
  84.  
  85. void loop(void)
  86. {
  87.     // Read the potentiometer value and update outputs
  88.     readPotentiometer(); // Read the potentiometer value
  89.     updateOutputs(); // Refresh output data
  90.  
  91.     // Check for new RFID cards
  92.     if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) return;
  93.  
  94.     Serial.print(F("PICC type: "));
  95.     MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  96.     Serial.println(mfrc522.PICC_GetTypeName(piccType));
  97.  
  98.     if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
  99.         piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  100.         piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  101.         Serial.println(F("Your tag is not of type MIFARE Classic."));
  102.         return;
  103.     }
  104.  
  105.     if (memcmp(mfrc522.uid.uidByte, nuidPICC, 4) != 0) {
  106.         Serial.println(F("A new card has been detected."));
  107.         memcpy(nuidPICC, mfrc522.uid.uidByte, 4);
  108.  
  109.         Serial.println(F("The NUID tag is:"));
  110.         Serial.print(F("In hex: "));
  111.         printHex(mfrc522.uid.uidByte, mfrc522.uid.size);
  112.         Serial.println();
  113.         Serial.print(F("In dec: "));
  114.         printDec(mfrc522.uid.uidByte, mfrc522.uid.size);
  115.         Serial.println();
  116.     } else {
  117.         Serial.println(F("Card read previously."));
  118.     }
  119.  
  120.     mfrc522.PICC_HaltA(); // Halt PICC
  121.     mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
  122. }
  123.  
  124. // Function to read the potentiometer value
  125. void readPotentiometer()
  126. {
  127.     int potValue = analogRead(Potentiometer_Vout_PIN_A0); // Read the analog value from the potentiometer
  128.     MFRC522_RST_PIN_D2_rawData = (potValue > 512); // Example condition to set raw data based on potentiometer value
  129.     MFRC522_RST_PIN_D2_phyData = potValue * (5.0 / 1023.0); // Convert raw value to physical data (0-5V)
  130. }
  131.  
  132. // Function to update outputs based on raw data
  133. void updateOutputs()
  134. {
  135.     digitalWrite(MFRC522_RST_PIN_D2, MFRC522_RST_PIN_D2_rawData); // Update the RST pin based on raw data
  136. }
  137.  
  138. // Function to print byte array in hex format
  139. void printHex(byte *buffer, byte bufferSize) {
  140.     for (byte i = 0; i < bufferSize; i++) {
  141.         Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  142.         Serial.print(buffer[i], HEX);
  143.     }
  144. }
  145.  
  146. // Function to print byte array in decimal format
  147. void printDec(byte *buffer, byte bufferSize) {
  148.     for (byte i = 0; i < bufferSize; i++) {
  149.         Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  150.         Serial.print(buffer[i], DEC);
  151.     }
  152. }
  153.  
  154. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement