 int last_call_hour;
 int last_message_hour;
 
-int monPeriod1Len = 600;                              // 10 minutes
-int monPeriod2Len = 36000;                             // 60 minutes
-int monPeriod3Len = 21600;                            // 12 hours
-
-
-long count_t1,        count_t2,       count_t3;
-long startOfPeriod1,  startOfPeriod2, startOfPeriod3;
+int monPeriodLen[3] = {600, 3600, 21600};                              // 10 minutes // 60 minutes // 12 hours
 
 
 float gsm_signal;
-float tt1m[4],      tt2m[4],      tt3m[4];
-float tt1mLast[4],  tt2mLast[4],  tt3mLast[4];
-float tt1mTrend[4], tt2mTrend[4], tt3mTrend[4];
-int   tt1m_avg[4],  tt2m_avg[4],  tt3m_avg[4];
+float tempSumm[3][4];
+float tempTrend[3][4];
+int   tempAvgCur[3][4];
+int   tempAvgPrev[3][4];
+int   measuresCount[3];
+int   measuresPerPeriod[3];
+long  startOfPeriod[3];
 
 
 boolean call_done;

  sensors.begin();                                //sensors initialize;

  // initial value setting
  startOfPeriod1 = now();
  startOfPeriod2 = now();
  startOfPeriod3 = now();
  call_done = false;
  // mode select
  armed = false;                                  //GSM activity

void updateStats(boolean debug)
{

  for (int period = 0; period < 3; period++)
  {
    for (int sensor = 0; sensor < sizeof(thermalSensor) / sizeof(DeviceAddress); sensor++)
    {
      if (now() - startOfPeriod[period] <= monPeriodLen[period])
      {
        measuresCount[period]++;
        tempSumm[period][sensor] = tempSumm[period][sensor] + sensors.getTempC(thermalSensor[sensor]); // Calcluating summ of temperature vaules for average calculation
      }
      else
      {
        startOfPeriod[period] = now();
        tempAvgPrev[period][sensor] = tempAvgCur[period][sensor];

        tempAvgCur[period][sensor] = round(tempSumm[period][sensor] / measuresCount[period]); // Calculating average value for period 1;
        tempTrend[period][sensor] = (tempAvgCur[period][sensor] - tempAvgPrev[period][sensor]) / measuresCount[period];

        tempSumm[period][sensor] = 0;
        measuresCount[period] = 0;
        if (debug)
        {
          Serial.print("[DBG] TREND "); Serial.print(sensor); Serial.print(":"); Serial.println(tempTrend[period][sensor]);
          makeForecast(sensor, true);
        }
      }
      measuresCount[period] = 1;


    }
  }
  if (debug) {

    for (int sensor = 0; sensor < sizeof(thermalSensor) / sizeof(DeviceAddress); sensor++)
    {
      Serial.print("TEMP"); Serial.print(sensor); Serial.print(":");
      Serial.print(sensors.getTempC(thermalSensor[sensor]));
      for (int period = 0; period < 3; period++)
      {
        Serial.print("/"); Serial.print(tempAvgCur[period][sensor]);
      }
      Serial.println();
    }
  }
