 }
 
 
+// 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
-#ifdef NFCTRACKDB
 static boolean writeTrackDbEntry() {
   boolean success = true;
-  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);
+  
+  #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();                                      // and make sure everything is clean and save
-
+    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(albumNfcFile)) {
+    if (bytesWritten == sizeof(trackDbEntry)) {
       #ifdef DEBUG
-        Serial.print(F("  ok. "));Serial.print(bytesWritten);Serial.print(F(" byte(s) written to "));Serial.println(albumNfcFile);
+        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(albumNfcFile);
+        Serial.print(F("  error writing "));Serial.println(trackDbFile);
       #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();
+  
+    // 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);
 }
-#endif 
 
 
 // 

      
      // check if we can still read the tag and if not, stop/pause the playback
      #ifdef NFCTRACKDB
        if (!checkTag()) plrTogglePause();
      #endif
      
      checkTime = millis(); // reset checktime so we can wait another 10 seconds aka 10000 ms

//  LLLLLL  EEEEEE  DDDDDD
//
// switches the value of lightOn between true and false - only needed in case there is a LED attached
static void switchLightState(void) {
  #ifdef OPRLIGHT
    if (!lightOn) {
      #ifdef DEBUG
        Serial.println(F("Switching light off -> ON"));
      #endif
      lightOn = true;
      #ifdef OPRLIGHTTIME
        lightStartUpTime = millis(); // also sets the lightStartUpTime
      #endif
    } else {
      #ifdef DEBUG
        Serial.println(F("Switching light on -> OFF"));
      #endif
      lightOn = false;
      digitalWrite(infoLedPin, LOW);   // also turns off the info led
    }
  #endif
}
