Help with combining 2 sketches together.

If I just leave this in the loop section

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')
        {
          
         
        }
          
        
      }

}

Then it works. It saves the digits.

But as soon as I add anything between the curly brackets after

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

Then nothing works. So I think it gets passed the setup. But hey I dont know I could be wrong.