 long curDistance = 0;  
 #define LED  13
 
+#define DEBUG 0
+
 File dataFile;
 File currentRunFile;
 File runs;
-//RTC_DS1307 RTC;
+long lastMeasurement = 0;
+long beginRun = 0;
 
 void setup()
 {  

  attachInterrupt(interupt, BtnDownCB, FALLING );

  // Call the function to enable low power consumption
  ble_low_power();

  // If you are using the Blend, uncomment below
  ble_set_pins(6, 7);

}


void stroller_ble_print() {
  #if DEBUG
    Serial.println("printing distance");
  #endif
  if (ble_connected()) {
              char output[27];
            // length of output should be: 5 fixed, 10 time, 12 distance;
         sprintf(output,"D:%10lu:%10lu\n\n",millis(),curDistance);
          
          ble_write_bytes((unsigned char *)output,27);
  }
}

volatile int hadRound = 0;
void loop()
{
  long time = millis();

  if (hadRound== 1) {
        #if DEBUG
    Serial.println("had round");
  #endif
    // compare the time of the last logged or the current logged value
    // the actual calculation
      if (beginRun==0) {
        beginRun=time;
      } 
      if (lastMeasurement == 0 ){
        lastMeasurement = time;
      }
      curDistance = curDistance + distance;
//
//      // write to SD card

////      // here, we can write the full, readable timestamp:
              char output[27];
            // length of output should be: 5 fixed, 10 time, 12 distance;
            sprintf(output,"R:%10lu:%10.2lu\n",time,curDistance);
            dataFile.write(output,27);
            dataFile.flush();

      byte reset= 0;
      if ((lastMeasurement-time)>timeBetweenRounds) {
          reset=1;
          char result[100];
          sprintf(result,"Run:%10lu:%10lu:%10.2lf\n",beginRun,time,curDistance);
          runs.write(result,100);
        runs.flush();
        curDistance=distance;
        beginRun=millis();
      }
          stroller_ble_print();
          lastMeasurement = millis();
          hadRound=0;

  }
  if (ble_available() )
