Help with combining 2 sketches together.

Well first of all let me tell you what I want the whole thing to do. I have it connected to a matrix keypad and when any digits are entered, followed by a pound, it saves the string of numbers on to the sd card. That part I have it down. It is the first sketch. Second thing I want it to do is when I connect to it via serial and send a command I want it to print the saved data to the serial. I have a sketch that does that by just reading the sd every time it starts up. So for it to read on my command I just reset the Arduino with sending an R and it just reads the SD card again. I made sketch for that. I just want to combine both sketches to run together. I greatly appreciate any help I can get.
Im using a Adruino Pro with the SparkFun SD shield. I've tried to just combine them, it compiles without any errors but when I upload it nothing happens. Just bunch of hash symbols. And Ive narrowed it down to the line "ArduinoOutStream cout(Serial);" from the second sketch. When I add this line to the Keypad sketch it compiles and uploads but nothing works.

HERE IS THE FIRST SKETCH:

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

const int chipSelect = 8;
SdFat sd;
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] = { 7, 6, 5, 4 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 2, 3, 9 }; 

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

char code[1024];
int index;

#define ledpin 13




void setup()
{
  index = 0;

  Serial.begin(9600);
  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++;
    }
  }
}

HERE IS THE SECOND ONE:

#include <SdFat.h>
const int chipSelect = 8;
int incomingByte;
SdFat sd;
ArduinoOutStream cout(Serial);
void setup(){
  digitalWrite(10, HIGH); //We need to set it HIGH immediately on boot
  pinMode(10,OUTPUT);     //We can declare it an output ONLY AFTER it's HIGH
                         // (( HACKHACKHACKHACK ))
  Serial.begin(9600);    //So you can watch the time printed

  char c;
  Serial.begin(9600);

  if (!sd.init(SPI_HALF_SPEED, chipSelect)) sd.initErrorHalt();

  
  // open file in current working directory
  ifstream file("codes.TXT");

  if (!file.is_open()) sd.errorHalt("open failed");

  // copy the file to Serial
  while ((c = file.get()) >= 0) cout << c;

  cout << "Done" << endl;

}

void loop(){

  if (Serial.available() > 0)
   {
     incomingByte = Serial.read();
     if (incomingByte == 'R')
       {
         Serial.println("RESET");
       digitalWrite(10, LOW);
       }
   
   }
}

Moderator edit: I went back to this post, hit "modify", highlighted each section of the code, then hit the "#" icon on the toolbar.
I then hit "save".
It has taken me longer to write this comment than it did to make the amendement.

Use code tags

Read
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Bump. Can someone please take a look at this code and tell me where Im going wrong.

ArmenFrost:
Bump. Can someone please take a look at this code and tell me where Im going wrong.

I'm sure people will be more than happy to once you start following directions.

ArmenFrost:
Bump. Can someone please take a look at this code and tell me where Im going wrong.

Do you mean by any chance can some one do it for you? Or do you really want help?

No I really want help. Both sketches work fine but when I combine them it just doesn't work. I think Im having a conflict in the loop section with the (Serial.println). That is just a guess. Because I added each section from one sketch to the other and uploaded and testes each line of code. Once I get down to the

Serial.begin(115200);
      if (!myFile.open("codes.txt", O_READ))
      Serial.println("code.txt:");
      int data;
      while ((data = myFile.read()) > 0) Serial.write(data);
      // close the file:
      myFile.close();

Thats when everything stops working. Here is the code Im using. Im sorry I just re-read the posting rules and I know Im suppose to post this way.

#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()
{

  index = 0;
  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.begin(115200);
      if (!myFile.open("codes.txt", O_READ))
      Serial.println("code.txt:");
      int data;
      while ((data = myFile.read()) > 0) Serial.write(data);
      // close the file:
      myFile.close();
      

      }


    }



  }

I followed the tutorial from the link you posted mike but It still didn't work.

Everyone please disregard the first code I posted in the original post.

but It still didn't work.

What exactly does that mean to you, please?

think Im having a conflict in the loop section with the (Serial.println). That is just a guess.

No such thing as a conflict with a serial print.

You should only have serial begin once in the sketch in the setup.

 if (!myFile.open("codes.txt", O_READ))

Has no braces, do you mean to only skip that first line if the condition is not met?

Well sorry for that. I meant to say is that, none of the sketches work. It just sits there when I input numbers it doesnt write it to serial or save them. And when I send the R command it doesnt print whatever was stores on the SD. Nothing works. The MC is on.

if this is not met

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

Then I want it to skip everything after it. Is that what you are sking Mike?

What I am saying is that if the condition is not met only that one line will be missed. I would have thought if you can not open the file you do not want to read from it.

if (Serial.available() > 0)
  {

    incomingByte = Serial.read();
    if (incomingByte == 'R')
    {
      Serial.begin(115200);
      if (!myFile.open("codes.txt", O_READ))
      Serial.println("code.txt:");  // miss this if the file is not open and do the rest of the lines
      int data;
      while ((data = myFile.read()) > 0) Serial.write(data);
      // close the file:
      myFile.close();
      
      }

Yes that is it Mike. If the the file cant be open then skip everything else.

If the the file cant be open then skip everything else.

In which case that code should be:-

 if (Serial.available() > 0)
  {

    incomingByte = Serial.read();
    if (incomingByte == 'R')
    {
      Serial.begin(115200);
      if (!myFile.open("codes.txt", O_READ)) { // you missed this opening brace
      Serial.println("code.txt:");
      int data;
      while ((data = myFile.read()) > 0) Serial.write(data);
      // close the file:
      myFile.close();
      
      }
  } // this is the matching closing brace

    }

Just tried that. Compiles fine but still nothing. Absolutely no reaction from Arduino when buttons are pressed or R is sent to it.

I didn't say it would fix it, I am looking for errors you made.
Have you made the same error anywhere else?

OP, please post your code.

Absolutely no reaction from Arduino when buttons are pressed or R is sent to it.

So put some debug printing in and see what you get. Post the code with the debug printing in it and say what happens.

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.