       o_tempminpassed = 1;
       o_tempMills = millis();
     }
-
-  }
-
-  if ( r != o_r ) {
+  }
+
+  if ((millis() - tv_off_Mills) >= ( o_tvOnTime * 60 * 1000 ) && timerOnOff == 0) {
+    turn_onoff_tv(1);
+  }
+
+  if ((millis() - tv_on_Mills) >= ( o_tvOffTime * 60 * 1000 ) && timerOnOff == 1) {
+    turn_onoff_tv(0);
+  }
+
+  if (t != o_t) {
+    tv_off_Mills = millis();
+    o_t = t;
+  }
+
+  if (r != o_r) {
     changelcdicon();
+    alarm_set();
     o_r = r;
-    alarm_set();
-  }
-
-  /*
-   *
-    pinMode(SETUP_IN_PIN, INPUT_PULLUP);
-    pinMode(WRK_MODE_IN_PIN, INPUT_PULLUP);
-    pinMode(DN_IN_PIN, INPUT_PULLUP);
-    pinMode(UP_IN_PIN, INPUT_PULLUP);
-    pinMode(PIR_IN_PIN, INPUT_PULLUP);
-   *
-   */
-  /*
-    int x = digitalRead(WRK_MODE_IN_PIN);
-    Serial.println(x);
-    //getdalastemp();
-    //Serial.println(tempCinside);
-    delay(1000);
-  */
-
-  /*
-    decode_results results;
-
-    if (irrecv.decode(&results)) {
-      dumpInfo(&results);
-
+  }
+
+  if ( pirOnOff == 1 && o_pirOnOff == 0 ) {
+      turn_onoff_tv(1);
+      o_pirOnOff    = 1;
+  }
+
+  if ( o_pirOnOff == 1 &&  pir_Mills > 30000 ) {
+      turn_onoff_tv(0);
+      o_pirOnOff    = 0;
+      pirOnOff      = 0;
+      attachInterrupt(0, PIRCHECKING, RISING);
+
+  }
+}
+
+void turn_onoff_tv(int a)
+{
+  if ( o_wrkMode == 1 && o_startMode == 1 ) {
+     if ( a == 1 && tvPowerStatus == 1) {
+         irSendTv(0);
+         tvPowerStatus = 0;
+         tv_on_Mills = millis();
+         timerOnOff = 1;
+         r = !r;      
+     }
+
+     if ( a == 0 && tvPowerStatus == 0) {
+         irSendTv(1);
+         tvPowerStatus = 1;
+         tv_off_Mills = millis();
+         timerOnOff = 0;
+         r = !r;      
+     }
+  }
+}
+
+void irSendTv(int a)
+{
+  if ( o_offMode == 1 ) {
+    irsend.sendNEC(tv_onoff, 32);
+    delay(300);
+    irrecv.enableIRIn();
+  } else {
+    if ( a == 1) {
+        irsend.sendNEC(tv_input, 32);
+        delay(3000);
+        for ( int i = 0 ; i < o_channelGap ; i++ ) {
+          irsend.sendNEC(tv_left, 32);
+          delay(300);
+        }
+        irsend.sendNEC(tv_enter, 32);
+    } else {
+        irsend.sendNEC(tv_input, 32);
+        delay(3000);
+        for ( int i = 0 ; i < o_channelGap ; i++ ) {
+          irsend.sendNEC(tv_right, 32);
+          delay(300);
+        }
+        irsend.sendNEC(tv_enter, 32);      
     }
-  */
-
-}
+    irrecv.enableIRIn();
+  }
+}
+
 
 void changelcdicon()
 {

  for ( int i = 0; i < ( 3 - length_Temperature ) ; i++ ) {
    lcd.print(" ");
  }
  lcd.print(Temperature, 1);
}


  B01110,
};

byte tvicon[8] =
{
  B10001,
