CSV logging

Anyone know how to use an Arduino Mega along with a ultrasonic SR04 sensor to measure a distance and log it? I want to log the data using the CSV file extension. Anyone know how to incorporate the logging problem into this?
I can get the sensor to measure a distance using the NewPing.h library.
The code is as follows;
‪#‎include‬ <NewPing.h>
‪#‎define‬ TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(1000); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}

GrahamKnowles:
Anyone know how to use an Arduino Mega along with a ultrasonic SR04 sensor to measure a distance and log it? I want to log the data using the CSV file extension. Anyone know how to incorporate the logging problem into this?
I can get the sensor to measure a distance using the NewPing.h library.
The code is as follows;
‪#‎include‬ <NewPing.h>
‪#‎define‬ TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(1000); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}

Please Read This Before Posting Programming Questions

.

I have a USB key I am going to save the file onto from the USB slot on the Mega.

Is it possible to change this code to work with a SD shield along with a UNO so?