 //
 // check Tag presence is used in the loop() (to populate the char array with the uid of the tag) and in function playTrack() to see
 // if the tag is still on the reader - this can only be used with the Adafruit NFC library
-#ifdef NFCTRACKDB
-boolean checkTag() {
+boolean checkForSameTag() {
   boolean sameTagPresent = true;
 
   return(sameTagPresent);
 }
-#endif
+
+boolean checkForTagPresence(void) {
+  boolean weHaveATag = false;
+  #ifdef NFCNDEF
+    // this implementation uses the Speedmaster Library and Don's NDEF lib
+    weHaveATag = nfc.tagPresent(1000);
+  #endif
+  #ifdef NFCTRACKDB
+    // this implementation uses the Adafruit PN532 library alone
+    weHaveATag = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
+  #endif
+  return(weHaveATag);
+}
 
 
 // 

//  BELOW THIS LINE THE MP3 PLAYER CONTROL IS DEFINED
//
// included in while loop of playTrack() for checks for volume changes
#ifdef VOLUMEPOT
static void plrAdjustVolume() {
  // volume control functions
  const byte volSensorDrift = 7;    // difference to last received sensor value that must be exceeded to activate a change in the volume
  word volCompareValue      = 0;    // just a convenience variable to make comparison easier
  
  // read the input on analog pin 0 and check if we have a change in volume.
  const word volSensorValue = analogRead(volPotPin);
  if (lastVolSensorValue > volSensorValue) {
    volCompareValue = lastVolSensorValue - volSensorValue;
  } else {
    volCompareValue = volSensorValue - lastVolSensorValue;
  }
  
  // If we have a high enough difference in the sensor reading, calculate it so we may set it on the player
  if (volCompareValue > volSensorDrift) {
    const byte soundVolume = volSensorValue / 10; // map the sensor value to a sound volume
    #ifdef DEBUG
      Serial.print(F("old sensor: ")); Serial.print(lastVolSensorValue); 
      Serial.print(F(", new sensor: ")); Serial.print(volSensorValue); 
      Serial.print(F(", vol: -")); Serial.println(soundVolume);
    #endif
    lastVolSensorValue = volSensorValue;
    musicPlayer.setVolume(soundVolume, soundVolume);
  } // end of change sound volume
}
#endif


//

}


// make sure we remember the just started track to be the new track, in case player is stopped 
static void checkAndSetCurTrack(byte curTrack, uint8_t numberOfFiles){
  #ifdef NFCTRACKDB
    if (curTrack != firstTrackToPlay) {
      // in case we are on the last track, choose the first track, so we start playback all from the beginning.
      if (curTrack == numberOfFiles) firstTrackToPlay = 1; else firstTrackToPlay = curTrack;
      
      // now store this new first track in the TrackDB
      // first create the new trackDb entry:
      setTrackDbEntry();
      // second store this entry in the TrackDb files, plus optional in the albumnfc.tdb file
      if (!writeTrackDbEntry()) {
        issueWarning("trackdb update error", "", false);
      }
    }
  #endif
}


// write the created trackDbEntry into the Track DB on SD
static boolean writeTrackDbEntry() {
  boolean success = true;
  
  #ifdef NFCTRACKDB
    byte bytesWritten;
    
    #ifdef DEBUG
      Serial.print(F("saving nfc <-> album: "));Serial.println(trackDbEntry);
    #endif
  
    // FIRST (if set) write the AlbumNFC file  --> ALBUMNFC.TDB
    #ifdef ALBUMNFC
      File file = SD.open(albumNfcFile, FILE_WRITE);
      bytesWritten = file.write(trackDbEntry, sizeof(trackDbEntry));
      file.close();                                      // and make sure everything is clean and save
  
      // now check if the write was successful - means we wrote the number of bytes of the trackDb entry to the file
      if (bytesWritten == sizeof(albumNfcFile)) {
        #ifdef DEBUG
          Serial.print(F("  ok. "));Serial.print(bytesWritten);Serial.print(F(" byte(s) written to "));Serial.println(albumNfcFile);
        #endif
        success = true;
      } else {
        #ifdef DEBUG
          Serial.print(F("  error writing "));Serial.println(albumNfcFile);
        #endif
        digitalWrite(warnLedPin, HIGH);
        success = false;
      }
    #endif
    
    // SECOND write the TrackDb-File --> e.g. larsrent.txt
    memset(trackDbFile, 0, sizeof(trackDbFile));       // make sure var to hold path to track db file is empty
    strcat(trackDbFile, trackDbDir);                   // add the trackDb Directory to the 
    strcat(trackDbFile, "/");                          // a / 
    strcat(trackDbFile, plrCurrentFolder);             // and the filename
    strcat(trackDbFile, ".txt");                       // plus of course a txt extension
    SD.remove(trackDbFile);                            // we want the file to be empty prior writing directory and track to it, so we remove it
    
    // used in case the albumnfc.tdb file is written first - redeclare file var
    #ifdef ALBUMNFC
      file = SD.open(trackDbFile, FILE_WRITE);
    #endif
    // used in case we only write the TrackDB file - need to initially declare file var
    #ifndef ALBUMNFC
      File file = SD.open(trackDbFile, FILE_WRITE);
    #endif
    bytesWritten = file.write(trackDbEntry, sizeof(trackDbEntry));
    file.close();
  
    // now check if the write was successful - means we wrote the number of bytes of the trackDb entry to the file
    if (bytesWritten == sizeof(trackDbEntry)) {
      #ifdef DEBUG
        Serial.print(F("  ok. "));Serial.print(bytesWritten);Serial.print(F(" byte(s) written to "));Serial.println(trackDbFile);
      #endif
      success = true;
    } else {
      #ifdef DEBUG
        Serial.print(F("  error writing "));Serial.println(trackDbFile);
      #endif
      digitalWrite(warnLedPin, HIGH);
      success = false;
    }
  
    // THIRD (if set) write the plrCurrentFolder (aka the album directory) to the file /trackdb0/LASTPLAY.TDB
    #ifdef RESUMELAST
      file = SD.open(resumeLastFile, FILE_WRITE);
      bytesWritten = file.write(plrCurrentFolder, sizeof(plrCurrentFolder));
      file.close();
    #endif
  #endif
  
  return (success);
}


// 
