Help with combining 2 sketches together.

Let me just start off by saying thank you guys for taking the time to help me. I tried the debugging code, but when I enter it anywhere in the loop sections then everything stops working again. SO nothing really prints for me to tell you where its stopping. So what i did was slowly take out code until it started working again. What I found out is that it all orks fine if I only run one code in the loop section at a time. But if I want to combine them then I can only go so far as this point

  if (Serial.available() > 0)
     {

         incomingByte = Serial.read();
         if (incomingByte == 'R')
        {
          
         //if I add anything in here then the whole thing stops working
        }
          
        
      }

Ive tried adding a simple LED turn on pin13 Ive tried to just print a simple word to serial. Nothing Just kills the whole thing.

Here is the complete code again.

#include <SdFat.h>
#include <Keypad.h>


const int chipSelect = 8;
SdFat sd;
int incomingByte;
SdFile myFile;


const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = 
{
  {
    '1','2','3'    }
  ,
  {
    '4','5','6'    }
  ,
  {
    '7','8','9'    }
  ,
  {
    '*','0','#'    }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 
  5, 4, 3, 2 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 
  9, 7, 6 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

char code[1024];
int index;

#define ledpin 13




void setup()
{
  
  Serial.begin(115200);
  if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();




}

void writeToFile()
{
  // open the file for write at end like the Native SD library
  if (!myFile.open("codes.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening codes.txt for write failed");
  }

  code[index] = '\0';
  myFile.println(code);

  // close the file:
  myFile.close();
  Serial.println("done.");
}

void printDigits(byte digits){

}

void loop()
{

  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    Serial.println(key);
    switch (key)
    {
    case '#':
      writeToFile();
      index = 0;
      Serial.print(code);
      break;
    default:
      code[index] = key;
      index++;
    }
  }

  if (Serial.available() > 0)
     {

         incomingByte = Serial.read();
         if (incomingByte == 'R')
        {
          
           Serial.println("RESET");   //this code will kill the hole sketch. If i erase it everything works fine. 
        }
          
        
      }

}

SInce it all stops working and I cant see what happens when I write a code after the

incomingByte = Serial.read();
         if (incomingByte == 'R')

I cant really know if that statement is working. Ive tried to add debug code but like I said if I add any serial.print anywhere in the loop section then nothing happens.