 
 // **************** FOR SD CARD ******************
 #define CHIP_SELECT_PIN 10
-File myFile;
+File data_file;
 String file_name;
 
 // **************** FOR GROVE SENSOR ****************
 #define GROVE_READ_PIN 3 // Digital PIN 3 ATMEGA 5
 unsigned long grove_duration;
-unsigned long grove_sample_time = 1000; //sample 1s ;  FIXME- used in calulation
+unsigned long grove_start_time;
+unsigned long grove_stop_time;
 unsigned long lowpulseoccupancy = 0;
 float ratio = 0;
 float concentration = 0;
+
 
 // **************** FOR SHARP SENSOR ****************
 #define SHARP_READ_PIN 3 // Analog PIN 3 ATMEGA 26

  DateTime rtc_time=rtc.now();
  file_name = String( rtc_time.month() ) + '-' + String( rtc_time.day() ) + '-' + 
      String( rtc_time.hour() ) + ".txt";

  if ( !SD.begin(CHIP_SELECT_PIN) ) { 
#ifdef DEGUB
    Serial.println("SD card initialization failed!");
#endif
  }
  delay(100); // otherwise open() will fail

  // write header
 
#ifdef DEBUG
    Serial.println("Writing header to SD");
    Serial.flush();
#endif

  myFile = SD.open(file_name.c_str(), FILE_WRITE);
  if (myFile) {
    myFile.print("Rec. #\tDD/MM/YYYY\tHH:MM:SS\tlatitude\tlongitude\t");
    myFile.println("Sharp value\tGrove value\tCO read\tCO heat\tHumidity\tTemp (celsius)");
    myFile.close();
  } else {
#ifdef DEBUG
    Serial.println("Error opening SD file to write header");
#endif
  }

  // **************** FOR SENOR READING TIMER ****************
  sensors_read_time_start = millis();


  
  // **************** FOR TEMP & HUMIDITY SENSOR ****************
  float h = dht.readHumidity();
  float f = dht.readTemperature(false); // change to true for Farenheit
  if (isnan(h) || isnan(f)) { // Check if any reads failed and set to error value.
    h=255;    f=255;
  }


  // **************** FOR GROVE SENSOR ****************
    // Integer percentage 0=>100
  ratio = lowpulseoccupancy / ((grove_stop_time - grove_start_time) * 10.0);
    // using spec sheet curve, gives particles per 0.01 cubic foot
  concentration = 1.1*pow(ratio, 3) - 3.8*pow(ratio, 2) + 520 * ratio + 0.62;
  lowpulseoccupancy = 0;  // reset
  grove_start_time=millis();


  // ********** KEEP TRACK OF NUMBER OF RECORDS *********
  num_records++;

#ifdef DEBUG
