   readTemp(ptr);
   readHum(ptr);
   readLum(ptr);
-  readSwitch(ptr);
-  
+    
 }
 
-void writeSensors(byte* ptr){
-  writeSwitch(ptr);
-  readSensors(ptr);
-
-}
 
 void readTemp(byte* ptr){
   float t = dht.readTemperature();
-  if (!isnan(t)){
-    EEPROM.write(2,(byte)t);
+  if ((!isnan(t)) && (t != sensorValues[2])){
+    sensorValues[2] = (int)t;
+    isUpdate = 1;
+    Serial.println("tupdate");
   }
-  else{
-    EEPROM.write(32,13);
-  }
+  
 
 
 }
 void readHum(byte* ptr){
   float h = dht.readHumidity();
-  if (!isnan(h)){
-    EEPROM.write(3,(byte)h);    
-    
+  if ((!isnan(h)) && (h != sensorValues[3])){
+      sensorValues[3] = (int)h;  
+      isUpdate = 1;
+      Serial.println("hupdate");
   }
-  else{
-    EEPROM.write(32,14);
-          
-  }
+  
 }
 void readLum(byte* ptr){
-  EEPROM.write(4,analogRead(lightsSnsorPin));
-  
+  int l = analogRead(lightsSnsorPin);
+  if(l!=sensorValues[4]){
+     sensorValues[4] = l;
+     isUpdate = 1; 
+     Serial.println("lupdate");
+  } 
 
 }
 
-void readSwitch(byte* ptr){
-  for(int i = 0; i < sizeof(switchesBytes)-1; i++){
-     EEPROM.write(switchesBytes[i],switchStatus[i]);
-  }
-}
 
-void writeSwitch(byte* ptr){
-  for(int i = 0; i < sizeof(switchesBytes)-1; i++){
-    digitalWrite(switchsPins[i], ptr[switchesBytes[i]]);
-    switchStatus[i] = ptr[switchesBytes[i]];
-    ptr[1] = 255;
-  }
+void writeSwitch(int i){
+    digitalWrite(rwPins[i], sensorValues[rwBytes[i]]);
+        
+  
 }
 
 


#define DHTPIN 2 
#define DHTTYPE DHT11 

RF24 radio(8,9);
DHT dht(DHTPIN, DHTTYPE);
byte incomingByte = 0;

const uint64_t pipes[2] = { 
  0xF0F0F0F001LL, 0xF0F0F0F002LL };
const byte shortAddr = 0x02;
int switchsPins[] = {
  3};
int switchesBytes[] = {
  10};
int switchStatus[] = {
  0};

int lightsSnsorPin = A0;
void setup(void)
{
  Serial.begin(57600);

  dht.begin();
  radio.begin();

  radio.setAutoAck(true);
  radio.setRetries(15,15);
  radio.setPALevel(RF24_PA_MAX);
  radio.openReadingPipe(1,pipes[1]);
  radio.openWritingPipe(pipes[0]);

  radio.startListening();

  for(int i = 0; i < sizeof(switchesBytes)-1; i++){
    pinMode(switchsPins[i], OUTPUT);  
  }



      delay(10);
    }
    if (pipenum==1){
      Serial.println(ptr[0]);
      runCommand(ptr);
    }
  }
  


}

void sendUpdate(){
    isUpdate = 0;
    radio.stopListening();
    radio.write(sensorValues, sizeof(byte[32]));
    radio.startListening();
}




void runCommand(byte *ptr)
{
    if(!ptr[0]){
    sendUpdate();
  }
  else{
    for(int i = 0; i < sizeof(rwBytes)-1; i++){
       if (sensorValues[rwBytes[i]] != ptr[rwBytes[i]]){
           sensorValues[rwBytes[i]] = ptr[rwBytes[i]];
           writeSwitch(i); 
       }
  }
    Serial.println("Write");
    sendUpdate();
  }

}
