Advertisement
Guest User

RFID Toy

a guest
Nov 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <MFRC522.h>
  2. #include <SPI.h>
  3. #include "SoftwareSerial.h"
  4. #include "DFRobotDFPlayerMini.h"
  5.  
  6. #define RST_PIN         9          // Configurable, see typical pin layout above
  7. #define SS_PIN          10         // Configurable, see typical pin layout above
  8.  
  9. // Instance of the class
  10. MFRC522 rfid(SS_PIN, RST_PIN);
  11.  
  12. SoftwareSerial mySoftwareSerial(5, 6); // RX, TX
  13. DFRobotDFPlayerMini myDFPlayer;
  14.  
  15. // Init array that will store new NUID
  16. byte nuidPICC[4];
  17.  
  18. void setup() {
  19.  
  20.   //mySoftwareSerial.begin(9600);
  21.   Serial.begin(9600);
  22.   SPI.begin(); // Init SPI bus
  23.   rfid.PCD_Init(); // Init MFRC522
  24.  
  25.   if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
  26.     Serial.println("Issue starting DFplayer");
  27.   } else {
  28.     Serial.println("DFPlayer Mini online.");
  29.   }
  30.  
  31.   myDFPlayer.volume(30);  //Set volume value. From 0 to 30
  32.  
  33. }
  34.  
  35. void loop() {
  36.  
  37.   if (!rfid.PICC_IsNewCardPresent()) { return; } // Look for new cards
  38.   if (!rfid.PICC_ReadCardSerial()) { return; } // Verify if the NUID has been readed
  39.  
  40.   if (rfid.uid.uidByte[0] != nuidPICC[0] || rfid.uid.uidByte[1] != nuidPICC[1] || rfid.uid.uidByte[2] != nuidPICC[2] || rfid.uid.uidByte[3] != nuidPICC[3] ) {
  41.  
  42.     String tag = String(rfid.uid.uidByte[0]) + String(rfid.uid.uidByte[1]) + String(rfid.uid.uidByte[2]) + String(rfid.uid.uidByte[3]);
  43.     Serial.println(tag);
  44.  
  45.     if(tag == "1421286112") {
  46.       Serial.println("Play sound 1");
  47.       myDFPlayer.play(1);
  48.     }
  49.  
  50.     if(tag == "1269073112") {
  51.       Serial.println("Play sound 2");
  52.       myDFPlayer.play(2);
  53.     }
  54.  
  55.     if(tag == "17423272112") {
  56.       Serial.println("Play sound 3");
  57.     }
  58.      
  59.   }
  60.  
  61.   // Halt PICC
  62.   rfid.PICC_HaltA();
  63.  
  64.   // Stop encryption on PCD
  65.   rfid.PCD_StopCrypto1();
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement