writing data to SD card - realtime input


I was wondering how I could transfer datasets in realtime on to an SD card.

What I’ve got:
I got the code that give me my data on my serial monitor.
I know the wiring for the arduino to my sd card breakout.

What I can figure out on my own:
Formatting the sd card.
Powering the whole project.

What I don’t know and need help with (mostly coding):
How to write the data that’s it’s getting and writing it to an sd card.

int threshold = 60;
int oldvalue = 0;
int newvalue = 0;
unsigned long oldmillis = 0;
unsigned long newmillis = 0;
int cnt = 0;
int timings[16];

void setup() {

void loop() {
  oldvalue = newvalue;
  newvalue = 0;
  for(int i=0; i<64; i++){ // Average over 16 measurements
    newvalue += analogRead(A2);
  newvalue = newvalue/64;
  // find triggering edge
  if(oldvalue<threshold && newvalue>=threshold){ 
    oldmillis = newmillis;
    newmillis = millis();
    // fill in the current time difference in ringbuffer
    timings[cnt%16]= (int)(newmillis-oldmillis); 
    int totalmillis = 0;
    // calculate average of the last 16 time differences
    for(int i=0;i<16;i++){
      totalmillis += timings[i];
    // calculate heart rate
    int heartrate = 60000/(totalmillis/16);

If you have limited time to code and don't mind paying for shipping, the Sparkfun OpenLog is a simple way of getting serial data onto an SD card.

bought the openlog chip. Will continue on this thread If I stumble on problems.