   kACK = 001,
   kARDUINO_READY = 002, 
   kERR = 003,
-
   kSEND_CMDS_END, // Mustnt delete this line
 };
 

  cmdMessenger.attach(kARDUINO_READY, arduino_ready);
  cmdMessenger.attach(unknownCmd);
  cmdMessenger.attach(4, pump_msg);
  arduino_ready();
}

unsigned long previousMillis;
void loop () {
  unsigned long currentMillis = millis();
  cmdMessenger.feedinSerialData();
  //  if (currentMillis - previousMillis > 100) {
  //    previousMillis = currentMillis;
  //}
}
  

  cmdMessenger.sendCmd(kERR,"Unknown command");
}

int measureTemperature() {
  int index = analogRead(A0) - adcSubstract;
  if (index < 0)
  {
    index = 0;
  }
  if (index>adcMax)
  {
    index = adcMax;
  }
  int temperature = pgm_read_word(&temperatureTable[index]);
  return temperature;
}

unsigned long windowStartTime;
boolean inDutyCycleMode;
boolean heaterIsOn;

void heaterOn() {
  digitalWrite(heaterPin, HIGH);
  heaterIsOn = HIGH;
}

void heaterOff() {
  digitalWrite(heaterPin, LOW);
  heaterIsOn = LOW;
}

void controlHeater() {
  if (dutyCycle == 0) {
    heaterOff();
    inDutyCycleMode = LOW;
  }
  else if (dutyCycle == 100) {
    heaterOn();
    inDutyCycleMode = LOW;
  }
  else {
    // If you just entered duty cycle mode
    // start first window
    if (!inDutyCycleMode) {
      windowStartTime = millis();
    }

    // 5min is 300'000 ms    
    unsigned long windowSize = 300000;
    
    // The onTime is windowSize * dutyCycle and since
    // dutyCycle is in percent we divide by 100
    unsigned long onTime = dutyCycle * (windowSize / 100);
    
    // Shift window time if  needed
    if (millis() - windowStartTime > windowSize)
      windowStartTime += windowSize;

    if (millis() - windowStartTime < onTime) {
      /* if (!heaterIsOn) */
      /* 	Serial << "Heater on " << millis()/1000 << "\n"; */
      heaterOn();
    }
    else {
      /* if (heaterIsOn) */
      /* 	Serial << "Heater off" << millis()/1000 << "\n"; */
      heaterOff();
    }
    
    inDutyCycleMode = HIGH;
  }
}

void setup() {
  pinMode(pumpPin, OUTPUT);
  pinMode(heaterPin, OUTPUT);

  dutyCycle = 0;

  Serial.begin(57600);
  cmdMessenger.print_LF_CR();
