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

Robin2:
Have you got your keypadInput() function to work and play .wav files in any other context? - if so post that program.

...R

Sure. It is a simpler program - without LED and Coin acceptor.
I could swear I had it working in the current program, outside IF statement, but now it doesn't seem possible.

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

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);
  // setup SD-card
  SD.begin(10);
}

void loop() {
    keypadInput();
}
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 whatever you want here it just print a message on the screen
  {
    playSound("jonas.wav");
  }
  else if (tot == code2) //if the code is correct you trigger whatever you want here it just print a message on the screen
  {
    playSound("petras.wav");
  }
  else if (tot == code3) //if the code is correct you trigger whatever you want here it just print a message on the screen
  {
    playSound("antanas.wav");
  }
  else if (tot == 0000) //if the code is correct you trigger whatever you want here it just print a message on the screen
  {
    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");
    Serial.println("wrong code!");
  }
}

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();
}