Cannot open a file from SD when using multiple loops [SOLVED]

Robin2:
Please post the latest version of your program in your next Reply. Also, please don't post photos of text, just copy and paste the text.

...R

Sorry, I always forget updating what I am trying now.

Version where if (coinsValue >= 1) is commented out:

const int coinInt = 0;
//Attach coinInt to Interrupt Pin (Any Digital Pin for DUE).

double coinsValue = 0.00;
//Set the coinsValue to a double

int coinsChange = 0;
//A Coin has been inserted flag

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#include <DAC.h>
#include <SD.h>
#include <SPI.h>
#include <Audio.h>
#include <Key.h>
#include <Keypad.h>

#define I2C_ADDR    0x3F // Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7


LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

const byte ROWS = 4;
const byte COLS = 4;

int code1 = 1451;  //The code I used, you can change it
int code2 = 2562;  //The code I used, you can change it
int code3 = 3673;  //The code I used, you can change it

int tot, i1, i2, i3, i4;
char c1, c2, c3, c4;

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};


Keypad myKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
  // debug output at 9600 baud
  Serial.begin(9600);

  //My LCD is 16x2
  lcd.begin (16, 2);

  //for DUE - any digital pin as Interrupt pin
  attachInterrupt(digitalPinToInterrupt(10), coinInserted, RISING);

  // Switch on the backlight
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home

  lcd.print("Suma:");
}

void coinInserted()
//The function that is called every time it recieves a pulse
{
  coinsValue = coinsValue + 0.05;
  //As we set the Pulse to represent 5p or 5c we add this to the coinsValue
  coinsChange = 1;
  //Flag that there has been a coin inserted
}

void loop() {    
  if (coinsChange == 1)
    //Check if a coin has been Inserted
  {
    coinsChange = 0;
    //unflag that a coin has been inserted
  }
  lcd.setCursor (0, 1);       // go to start of 2nd line
  //Print the Value of coins inserted
  lcd.print(coinsValue);
  
    keypadInput();   // <<<<------ When adding this here, system cannot assign file names through playSound("***.wav");!!!
    
  //if coins value is enough to make a call - dial
//  if (coinsValue >= 1)
//  {
//    lcd.setCursor (7, 0);       // go to start of 2nd line
//    lcd.print("Skambink!");
//  }
}
void keypadInput() {

  //pressing the key -> assigning the filename
  char customKey = myKeypad.waitForKey();

  if (customKey != NO_KEY)
  {
    c1 = customKey;
    Serial.print(customKey);
  }
  customKey = myKeypad.waitForKey();
  if (customKey != NO_KEY)
  {
    c2 = customKey;
    Serial.print(customKey);
  }
  customKey = myKeypad.waitForKey();
  if (customKey != NO_KEY)
  {
    c3 = customKey;
    Serial.print(customKey);
  }
  customKey = myKeypad.waitForKey();
  if (customKey != NO_KEY)
  {
    c4 = customKey;
    Serial.println(customKey);
  }

  //the keys pressed are stored into chars I convert them to int then i did some multiplication to get the code as an int of xxxx
  i1 = (c1 - 48) * 1000;
  i2 = (c2 - 48) * 100;
  i3 = (c3 - 48) * 10;
  i4 = c4 - 48;
  tot = i1 + i2 + i3 + i4;

  if (tot == code1) //if the code is correct you trigger to play .wav
  {
    playSound("jonas.wav");
  }
  else if (tot == code2)
  {
    playSound("petras.wav");
  }
  else if (tot == code3)
  {
    playSound("antanas.wav");
  }
  else if (tot == 0000)
  {
    playSound("test.wav");
  }
  else //if the code is wrong you get another thing
  {
    for (int j = 0; j < 256; j++) {
      for (int i = 0; i < 256; i++)
        analogWrite(DAC1, i);
    }
    playSound("blogas.wav");
  }
}

// Playing assigned filename
void playSound(const char* cName) {
  File myFile = SD.open(cName);
  const int S = 1024; // Number of samples to read in block
  short buffer[S];
  // until the file is not finished
  Serial.println("playing ");
  Serial.println(cName);
  Audio.begin(44100, 100);
  while (myFile.available()) {
    myFile.read(buffer, sizeof(buffer));
    // Prepare samples
    int volume = 1024;
    Audio.prepare(buffer, S, volume);
    Audio.write(buffer, S);
  }
  Audio.end();
  myFile.close();
}