   Serial.println();
 #endif
 
-  delay((unsigned long)15*60*1000 - 2*timeWithCurrent); // 15 min de pause
+  delay(A_Values_Period - 2*timeWithCurrent);
+
+#endif
 }
 
 void requestEvent()
-{
+{ 
+  switch (last_command){
+
+  case COMMAND_READ_SENSORS:
+    writeSensorsAsBytes();
+    break;
+
+  case COMMAND_GET_PERIOD:
+    writePeriodAsBytes();
+    break;
+  }
+
+  last_command = -1;
+}
+
+
+void receiveEvent(int len){
+  if(Wire.available()){
+    last_command = Wire.read(); 
+  }
+
+  switch (last_command){
+  case COMMAND_SET_PERIOD:
+    A_Values_Period = readLong();
+    writeLongInEEPROM(1, A_Values_Period);
+    asm volatile ("  jmp 0");
+    break;
+  
+  case COMMAND_SET_ADRESS:
+    i2cAdress = readLong();
+    writeLongInEEPROM(5, i2cAdress);
+    asm volatile ("  jmp 0");
+    break;
+  }
+}
+
+/* Replies to request */
+
+// COMMAND_READ_SENSORS
+void writeSensorsAsBytes(){
   int bufferSize = sizeof(int)*(sensor_SIZE)+sizeof(long);
   byte buffer[bufferSize];
 


const int sensorPins[][2] = {
  {
    A0,4          }
  ,{
    A1,3          }
  ,{
    A2,2          }
  ,{
    A3,1          }
};

const int sensor_SIZE = ARRAY_LENGTH(sensorPins);
unsigned int A_Values[sensor_SIZE];
unsigned long A_values_timestamp;

const int addressPins[] = {
  6,7,8,9};

void setup()
{  
#ifndef HARDCODEDADRESS
  int i2cAddress = 0;
  for (int i=0;i<ARRAY_LENGTH(addressPins);i++){
    pinMode(addressPins[i], INPUT_PULLUP);

    if (digitalRead(addressPins[i]) == HIGH){
      i2cAddress += (1<<i);      
    }

    pinMode(addressPins[i], OUTPUT);
    digitalWrite(addressPins[i], LOW);
  }

  Wire.begin(i2cAddress);
#else
  Wire.begin(HARDCODEDADRESS);
#endif

  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);

#ifdef LOG_SERIAL
  Serial.begin(9600);
#endif
}

void loop()
{
  for (int i=0;i<sensor_SIZE;i++){

    pinMode(sensorPins[i][0], INPUT_PULLUP);

#include <Wire.h>
#include <EEPROM.h>

#define ARRAY_LENGTH(x)  (sizeof(x) / sizeof((x)[0]))

//#define LOG_SERIAL
//#define HARDCODEDADRESS 6
//#define FAKESENSORSWITHCOUNTERS

int i2cAdress = 3;
//TODO 
//
// store read frequency in EEPROM
