Back to Parent

//nano ble sense code

#include <SPI.h>
#include <MFRC522.h>
#include "Arduino.h"
#include "DFRobotDFPlayerMini.h"

DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);


#define RST_PIN 9  // Configurable, see typical pin layout above
#define SS_PIN 10  // Configurable, see typical pin layout above

//potentiometer
const byte volumePot = A0;
int prevVolume;

byte volumeLevel = 0;  //variable for holding volume level

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);  // Initialize serial communications with the PC
  while (!Serial)
    ;                  // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();         // Init SPI bus
  mfrc522.PCD_Init();  // Init MFRC522

  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));

  if (!myDFPlayer.begin(Serial1)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while (true) {
      delay(0);  // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));
  //myDFPlayer.volume(15);  //Set volume value. From 0 to 30
  volumeLevel = map(analogRead(volumePot), 0, 1023, 0, 30);  //scale the pot value and volume level
  myDFPlayer.volume(volumeLevel);
  prevVolume = volumeLevel;
}

void loop() {
  //scale the pot value and volume level
  volumeLevel = map(analogRead(volumePot), 0, 1023, 0, 30);

  if (volumeLevel - prevVolume >= 2 || prevVolume - volumeLevel >= 2) {
    myDFPlayer.volume(volumeLevel);
    Serial.println(volumeLevel);
    prevVolume = volumeLevel;
    // delay(1);
  }

  // Look for new cards
  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  Serial.println("New card Detected");
  // Read tapped card data
  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  // get the ID
  String id;
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    // Create a RFID Hexdecimal String
    id += String(mfrc522.uid.uidByte[i], HEX);
  }
  // Convert to Uppercase
  id.toUpperCase();

  // print it to the console.
  Serial.print(id);
  Serial.println();

  if (id == "486F6F231290") {
    myDFPlayer.play(1);
    Serial.println("Play 1");
  } else if (id == "486F7F231290") {
    myDFPlayer.play(11);
    Serial.println("Play 2");

  } else if (id == "486FDF231290") {
    myDFPlayer.play(3);
    Serial.println("Play 3");

  } else if (id == "486FCF231290") {
    myDFPlayer.play(13);
    Serial.println("Play 4");

  } else if (id == "486FBF231290") {
    myDFPlayer.play(5);
    Serial.println("Play 5");

  } else if (id == "486FAF231290") {
    myDFPlayer.play(15);
    Serial.println("Play 6");

  } else if (id == "486F3F231290") {
    myDFPlayer.play(7);
    Serial.println("Play 7");

  } else if (id == "486F2F231290") {
    myDFPlayer.play(17);
    Serial.println("Play 8");

  } else if (id == "486F9F231290") {
    myDFPlayer.play(9);
    Serial.println("Play 9");

  } else if (id == "486F8F231290") {
    myDFPlayer.play(19);
    Serial.println("Play 10");

  } else if (id == "486F4F231290") {
    myDFPlayer.play(21);
    Serial.println("Play 11");

  } else if (id == "486F5F231290") {
    myDFPlayer.play(23);
    Serial.println("Play 12");

  } else if (id == "486F1F231290") {
    myDFPlayer.play(25);
    Serial.println("Play 13");
    
  } else {
    Serial.println("Unknown card");
  }


  // Reset Id
  id = "";
  mfrc522.PICC_HaltA();
  mfrc522.PCD_StopCrypto1();
}

void printDetail(uint8_t type, int value) {
  switch (type) {
    case TimeOut:
      Serial.println(F("Time Out!"));
      break;
    case WrongStack:
      Serial.println(F("Stack Wrong!"));
      break;
    case DFPlayerCardInserted:
      Serial.println(F("Card Inserted!"));
      break;
    case DFPlayerCardRemoved:
      Serial.println(F("Card Removed!"));
      break;
    case DFPlayerCardOnline:
      Serial.println(F("Card Online!"));
      break;
    case DFPlayerUSBInserted:
      Serial.println("USB Inserted!");
      break;
    case DFPlayerUSBRemoved:
      Serial.println("USB Removed!");
      break;
    case DFPlayerPlayFinished:
      Serial.print(F("Number:"));
      Serial.print(value);
      Serial.println(F(" Play Finished!"));
      break;
    case DFPlayerError:
      Serial.print(F("DFPlayerError:"));
      switch (value) {
        case Busy:
          Serial.println(F("Card not found"));
          break;
        case Sleeping:
          Serial.println(F("Sleeping"));
          break;
        case SerialWrongStack:
          Serial.println(F("Get Wrong Stack"));
          break;
        case CheckSumNotMatch:
          Serial.println(F("Check Sum Not Match"));
          break;
        case FileIndexOut:
          Serial.println(F("File Index Out of Bound"));
          break;
        case FileMismatch:
          Serial.println(F("Cannot Find File"));
          break;
        case Advertise:
          Serial.println(F("In Advertise"));
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0