     sprintf(ts,"%02d-%02d-%02d %02d:%02d:%02d",yyyy,mt,dd,hh,mm,ss);
 	ts[17] = '\0' ;	
 
-	// sensors
+
+    // sensors data
+	char buffer[12] ;
+	get_sensor_data(buffer);
+
+    // print on serial and LCD
+    #if defined (SERIAL_DEBUG)
+        Serial.println(ts);
+    #endif
+    display_output(buffer);
+
+    // write to SD card
+    dataFile = SD.open("yuktix.log", O_CREAT | O_WRITE | O_APPEND);
+    if (! dataFile) {
+        char error[] = "error opening yuktix.log";
+        display_output(error);    
+        // Wait forever since we cant write data
+        while (1) ;
+    }
+
+    dataFile.print(ts);
+    dataFile.print(",");
+    dataFile.println(buffer);
+	micro_delay(20);
+    dataFile.close();  
+    micro_delay(20);
+
+}
+
+void get_sensor_data(char* buffer) {
+    
     int dht11_code = DHT11.read(DHT11_PIN);
     micro_delay(200);
 	int ldr = analogRead(LDR_PIN); 
 	
     if(dht11_code != 0 ) {
         // error
-        sprintf(data,"%11s","TH_ERR");
+        sprintf(buffer,"%11s","SENSOR_ERR");
 
     } else {
 		// 11 char wide data
-        sprintf(data,"%-03d,%-02d,%-04d",DHT11.temperature,DHT11.humidity,ldr);
+        sprintf(buffer,"%-03d,%-02d,%-04d",DHT11.temperature,DHT11.humidity,ldr);
     }
 
-    data[11] = '\0' ;
+    buffer[11] = '\0' ;
 }
 
 // param n is to ensure n*10 ms delay
 void micro_delay(int n) {
     for(int i = 0 ; i < n ; i++) {
         delayMicroseconds(10000);
-        // pat watchdog every 100*10 ms
-        // if((i > 100) && (i %100 == 0 )) wdt_reset();
+        // pat watchdog?
     }
 }
 

    if(timeStatus()!= timeSet) {
        // CLK error
        char error[] = "clock error" ;
		char line2[]= "***" ;
        Serial.println(error);
		lcd_print(error,line2);	
        for(;;);
    }

    // initialize SD card
    pinMode(SS, OUTPUT);
    if (!SD.begin(chipSelect)) {
        Serial.println("Card failed, or not present");
        // don't do anything more:
        while (1) ;
    }
    
	// LCD
	
    Alarm.timerRepeat(10, log_data);

}

void lcd_print(char* line1, char* line2) {

    pinMode(LCD_PIN, OUTPUT);
    digitalWrite(LCD_PIN, HIGH);
    lcd.begin(16,2);       
    lcd.clear();
    // line1
    lcd.setCursor(0,0);
    lcd.print(line1); 
    lcd.setCursor(0,1);
    lcd.print(line2); 
}

void log_data() {

    char ts[18] ;
	char data[12] ;
	get_sensor_data(ts,data);

    dataFile = SD.open("yuktix.log", O_CREAT | O_WRITE | O_APPEND);
    if (! dataFile) {
      Serial.println("error opening yuktix.log");
      // Wait forever since we cant write data
      while (1) ;
    }

    // write to SD card
    dataFile.print(ts);
    dataFile.print(",");
    dataFile.println(data);
	micro_delay(20);
    dataFile.close();  
    micro_delay(20);

    int hh = hour();
    int mm = minute();
	char line2[6] ;
	sprintf(line2,"%02d:%02d",hh,mm);
	
	lcd_print(line2,data);

}

void get_sensor_data(char* ts, char* data) {

    // timestamp
    int hh = hour();


void setup() {

    #if defined(SERIAL_DEBUG)
    Serial.begin(9600);
    #endif

    // DS1307 CLK 
    setSyncProvider(RTC.get);   
