 }
 
 int alarm=0,lightBulb=0; 
-int lastON=0,r=0; 
+int r=0; 
 unsigned long WIFIupload = -REFRESHupload ,LASTmotion = 0; 
 
 void loop()
 {  
+  Serial.println("Starting new cycle"); 
   
   if(DEBUG) Serial.print("Gas value is "); 
   if(DEBUG) Serial.println(analogRead(GASpin)); 


      strncpy_P(buffer+strlen(buffer), PSTR("&light="), sizeof(buffer)-strlen(buffer)-1);
      ltoa(analogRead(PHOTOpin), buffer+strlen(buffer), 10); 

      strncpy_P(buffer+strlen(buffer), PSTR("&relay="), sizeof(buffer)-strlen(buffer)-1);
      ltoa(lightBulb, buffer+strlen(buffer), 10); 

      strncpy_P(buffer+strlen(buffer), PSTR("&ms="), sizeof(buffer)-strlen(buffer)-1);
      ltoa(millis()/1000, buffer+strlen(buffer), 10); 

  pinMode(PIRsensorPin,INPUT); 
  pinMode(ALARMbutton,INPUT); 
  pinMode(RELAYbutton,INPUT); 
  pinMode(BUZZpin, OUTPUT); 
  Serial.begin(9600); 
  wifiSEND.begin(9600); loadScreen(loadTime); loadTime++; 
  

  lcd.setCursor(0,0); lcd.print("Resetting...             ");
  for( loadTime = 2 ; loadTime < 10; ++loadTime)
    loadScreen(loadTime); 



  
  char bufferr[300]; 
  wifiSEND.sendCommand(F("AT"), bufferr, sizeof(bufferr)); delay(50); 
  if(DEBUG) 
    Serial.println(bufferr);
    
  wifiSEND.sendCommand(F("AT+CWJAP?"), bufferr, sizeof(bufferr));
  delay(2000); 
  if(DEBUG){ 
    Serial.println(bufferr);
    Serial.println(ESP8266_SSID);     
  }

  if(bufferr[0]!=ESP8266_SSID[0] || bufferr[1]!=ESP8266_SSID[1] || bufferr[2]!=ESP8266_SSID[2]){ 
  wifiSEND.sendCommand(F("AT+RST"), bufferr, sizeof(bufferr)); 
  delay(1000); 
  wifiSEND.setupAsWifiStation(ESP8266_SSID, ESP8266_PASS, &Serial); 
  delay(5000); 
  }
  
  lcd.setCursor(0,0); lcd.print("Connecting...             "); 
  for( loadTime = 10 ; loadTime < 16; ++loadTime)
