I need help to add data logging to a working program

I am new to this and am trying to get data logging to a SD card to work with a program that is already working. Right now the program just outputs to the serial port.
Here is my setup. I have a Parallax altimeter module (Microcontroller KickStarts | LEARN.PARALLAX.COM) attached to an Ardunio Uno as described on their webpage. Using their provided sketch the hardware is working great. When I open the serial monitor I get a scrolling display of altitude.
My use for this altimeter is for plotting the altitude of model rockets. I have added an Ardunio wireless SD shield in hopes of using the attached SD slot to log the serial data to a file on the SD card.
Can someone help me get this modified?
Below is the Altimeter_Ardunio_10 sketch. There is also IntersemaBaro.h that goes with it but is too long to post here. I assumed I could add the SD.h by using #include <SD.h>, then change the Serial.print lines to dataFile.print, but I'm just not getting anywhere....

Thanks for your help!!

#include <Wire.h>
#include "IntersemaBaro.h"

Intersema::BaroPressure_MS5607B baro(true);

void setup() { 
    Serial.begin(9600);
    baro.init();
}

void loop() {
  int alt = baro.getHeightCentiMeters();
  Serial.print("Centimeters: ");
  Serial.print((float)(alt));
  Serial.print(", Feet: ");
  Serial.println((float)(alt) / 30.48);
  delay(400);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

It's probably just one command that is wrong.

The datalogging sketch provided in the SD section of the examples in the Arduino IDE has all you need. It is just a matter of establishing the file and communication in the setup section and writing data to it in the loop.

code
open myfile
myFile.write (dadedah);
close myfile

Writing to a SD card has been changed and improved a few times.

Use the newest Arduino 1.0.2, and use the examples from the IDE.
Also the library reference is new : SD - Arduino Reference

Start by writing and reading a file. If you know how to do that, combine it with your sensor.

Thanks for your help!
I have started to play with combining the SD data logger sketch with my altimeter sketch. Below is where I am at. I am getting a compiling errors as follows:

Altimeter_Arduino_12:15: error: expected constructor, destructor, or type conversion before '.' token
Altimeter_Arduino_12:18: error: expected constructor, destructor, or type conversion before '(' token
Altimeter_Arduino_12:21: error: expected unqualified-id before 'if'
Altimeter_Arduino_12:26: error: expected constructor, destructor, or type conversion before '.' token
Altimeter_Arduino_12:27: error: expected declaration before '}' token

By the way I am using Arduino 1.0.2

Thanks!

#include <Wire.h>
#include "IntersemaBaro.h"
#include <SD.h>

const int chipSelect = 4;
Intersema::BaroPressure_MS5607B baro(true);

void setup() { 
    Serial.begin(9600);
       while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
    baro.init();
}

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {
  int alt = baro.getHeightCentiMeters();
  Serial.print("Centimeters: ");
  Serial.print((float)(alt));
  Serial.print(", Feet: ");
  Serial.println((float)(alt) / 30.48);
  delay(400);
}

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
// if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Here is your setup() function:

void setup() {
    Serial.begin(9600);
       while (!Serial)
    ; // wait for serial port to connect. Needed for Leonardo only
    baro.init();
}

Here is code that is NOT in a function:

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
 
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

All code must be in a function.

You have the same problem having tacked code on after loop(), too.

Thanks PaulS,
I have made some changes and think I am getting there....
I now get a file created on the SD card named DATALOG.TXT ...but it is an empty file. When I open the Serial monitor I do see the data scrolling:

CeCentimeters: 19461.00, Feet: 638.48
Initializing SD card...card initialized.
Centimeters: 19528.00, Feet: 640.68
Centimeters: 19499.00, Feet: 639.73
Centimeters: 19470.00, Feet: 638.78
Centimeters: 19490.00, Feet: 639.44
Centimeters: 19470.00, Feet: 638.78
Centimeters: 19480.00, Feet: 639.11
Centimeters: 19480.00, Feet: 639.11
Centimeters: 19470.00, Feet: 638.78

Like I said I am NEW. Any additional help to get the serial data I am seeing in the monitor to be written to my file would be appreciated!!

Here is my sketch as I have it now:

#include <Wire.h>
#include "IntersemaBaro.h"
#include <SD.h>

const int chipSelect = 4;
Intersema::BaroPressure_MS5607B baro(true);

void setup() { 
    Serial.begin(9600);
       while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
    baro.init();

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.close();
    // print to the serial port too:
  }  
// if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

void loop() {
  int alt = baro.getHeightCentiMeters();
  Serial.print("Centimeters: ");
  Serial.print((float)(alt));
  Serial.print(", Feet: ");
  Serial.println((float)(alt) / 30.48);
  delay(400);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.close();
    // print to the serial port too:
  }

You're certainly not writing much to the file, so it's not surprising that there is little in the file.

You need to open the file, write to it, and close the file, in the loop() function.

PaulS,
My limited understanding....is that these are the lines that are currently updating the altitude in my Serial monitor window. Updates happen about every 1/2 second (400ms).

void loop() {
  int alt = baro.getHeightCentiMeters();
  Serial.print("Centimeters: ");
  Serial.print((float)(alt));
  Serial.print(", Feet: ");
  Serial.println((float)(alt) / 30.48);
  delay(400);

where do I need to have this in relation to the write to card info in order to get these numbers written? Right now nothing gets written except for the file name.

Thanks

My limited understanding....is that these are the lines that are currently updating the altitude in my Serial monitor window.

In this case, limited == correct.

where do I need to have this in relation to the write to card

  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile)
  {
     int alt = baro.getHeightCentiMeters();

     dataFile.print("Centimeters: ");
     dataFile.print((float)(alt));
     dataFile.print(", Feet: ");
     dataFile.println((float)(alt) / 30.48);
     dataFile.close();

    // print to the serial port too:
     Serial.print("Centimeters: ");
     Serial.print((float)(alt));
     Serial.print(", Feet: ");
     Serial.println((float)(alt) / 30.48);
  }
  delay(400);

Thanks again PaulS for the help
I have code now that will generate a file and write one entry...not continuous entries like are displayed in the serial monitor.
Can you show me how to get continuous readings to be written to the file?
Thanks!

#include <Wire.h>
#include "IntersemaBaro.h"
#include <SD.h>

const int chipSelect = 4;
Intersema::BaroPressure_MS5607B baro(true);

void setup() { 
    Serial.begin(9600);
       while (!Serial) 
    ; // wait for serial port to connect. Needed for Leonardo only
    baro.init();

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) 
  {
  int alt = baro.getHeightCentiMeters();

     dataFile.print("Centimeters: ");
     dataFile.print((float)(alt));
     dataFile.print(", Feet: ");
     dataFile.println((float)(alt) / 30.48);
     dataFile.close();
     

    // print to the serial port too:
     Serial.print("Centimeters: ");
     Serial.print((float)(alt));
     Serial.print(", Feet: ");
     Serial.println((float)(alt) / 30.48);
  }
   delay(500);  
    
    // print to the serial port too:
  
  } 


void loop() {
  int alt = baro.getHeightCentiMeters();
  Serial.print("Centimeters: ");
  Serial.print((float)(alt));
  Serial.print(", Feet: ");
  Serial.println((float)(alt) / 30.48);
  delay(500);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Can you show me how to get continuous readings to be written to the file?

Sure. Do as I've said from the beginning and put the code that opens the file, writes to the file, and closes the file in the loop() function, not the setup() function.

Thanks for your help. I got it working now! Here is a picture of what my Arduino Altimeter will be taking a ride in.

Screen Shot 2012-12-02 at 7.58.15 PM.png