bool plusButtonCurrentState = HIGH;
bool plusButtonLastState = HIGH;
unsigned long plusButtonDownMillis = 0;
unsigned long plusButtonRepeatMillis = 0;
unsigned long plusButtonHoldTime = 0;
bool plusButtonFired = false;

bool minusButtonCurrentState = HIGH;
bool minusButtonLastState = HIGH;
unsigned long minusButtonDownMillis = 0;
unsigned long minusButtonRepeatMillis = 0;
unsigned long minusButtonHoldTime = 0;
bool minusButtonFired = false;

bool modeButtonCurrentState = HIGH;
bool modeButtonLastState = HIGH;
unsigned long modeButtonDownMillis = 0;
unsigned long modeButtonHoldTime = 0;
bool modeButtonFired = false;
bool modeButtonHoldFired = false;


void plusButtonAction() {
  setTempStepUp();
  extendDisplayTimeout();
}

void minusButtonAction() {
  setTempStepDown();
  extendDisplayTimeout();
}

void modeButtonAction() {
  extendDisplayTimeout();
  toggleHeatingMode();
}

void modeButtonHoldAction() {
  Serial.println("switch off/on");
  extendDisplayTimeout();
  toggleThermostatOnOff();
}

void checkButtonStates() {

  // Buttons einlesen

  // plus button
  plusButtonCurrentState = digitalRead(PIN_BUTTON_PLUS);
  if (plusButtonCurrentState == LOW && plusButtonLastState == HIGH) { // button was unpressed and is millis() pressed
    plusButtonDownMillis = millis();
    plusButtonLastState = plusButtonCurrentState;
  }
  else if (plusButtonCurrentState == LOW && plusButtonLastState == LOW) { // button is held
    plusButtonHoldTime = millis() - plusButtonDownMillis;
    if ( plusButtonHoldTime > 50 ) {
      plusButtonLastState = plusButtonCurrentState;
      if (!displayActive && !plusButtonFired ) {
        enableDisplay();
        plusButtonFired = true;
      }
      //      else if (displayActive && !plusButtonFired ) {
      //        plusButtonAction();
      //        plusButtonFired = true;
      //      }
    }
    if ( plusButtonHoldTime > 1250 ) {
      // button is held longer - repeating
      if ( (millis() - plusButtonRepeatMillis) > 300 ) {
        plusButtonAction();
        plusButtonRepeatMillis = millis();
      }
    }
  }
  else if (plusButtonCurrentState == HIGH && plusButtonLastState == LOW) { // button is released again
    plusButtonLastState = plusButtonCurrentState;

    if (displayActive && !plusButtonFired) {
      plusButtonAction();
    }
    plusButtonFired = false;
  }




  // minus button
  minusButtonCurrentState = digitalRead(PIN_BUTTON_MINUS);
  if (minusButtonCurrentState == LOW && minusButtonLastState == HIGH) { // button was unpressed and is millis() pressed
    minusButtonDownMillis = millis();
    minusButtonLastState = minusButtonCurrentState;
  }
  else if (minusButtonCurrentState == LOW && minusButtonLastState == LOW) { // button is held
    minusButtonHoldTime = millis() - minusButtonDownMillis;
    if ( minusButtonHoldTime > 50 ) {
      minusButtonLastState = minusButtonCurrentState;
      if (!displayActive && !minusButtonFired ) {
        enableDisplay();
        minusButtonFired = true;
      }
      //      else if (displayActive && !minusButtonFired ) {
      //        minusButtonAction();
      //        minusButtonFired = true;
      //      }
    }
    if ( minusButtonHoldTime > 1250 ) {
      // button is held longer - repeating
      if ( (millis() - minusButtonRepeatMillis) > 300 ) {
        minusButtonAction();
        minusButtonRepeatMillis = millis();
      }
    }
  }
  else if (minusButtonCurrentState == HIGH && minusButtonLastState == LOW) { // button is released again
    minusButtonLastState = minusButtonCurrentState;

    if (displayActive && !minusButtonFired) {
      minusButtonAction();
    }
    minusButtonFired = false;
  }




  // mode button
  modeButtonCurrentState = digitalRead(PIN_BUTTON_MODE);
  if (modeButtonCurrentState == LOW && modeButtonLastState == HIGH) { // button was unpressed and is millis() pressed
    modeButtonDownMillis = millis();
    modeButtonLastState = modeButtonCurrentState;
  }
  else if (modeButtonCurrentState == LOW && modeButtonLastState == LOW) { // button is held
    modeButtonHoldTime = millis() - modeButtonDownMillis;
    if ( modeButtonHoldTime > 50 ) {
      modeButtonLastState = modeButtonCurrentState;
      if (!displayActive && !modeButtonFired ) {
        enableDisplay();
        modeButtonFired = true;
      }
      //      else if (displayActive && !modeButtonFired ) {
      //        modeButtonAction();
      //        modeButtonFired = true;
      //      }
    }
    if ( modeButtonHoldTime > 2000 && !modeButtonHoldFired ) {
      // button is held longer
      modeButtonHoldAction();
      modeButtonHoldFired = true;
    }
  }
  else if (modeButtonCurrentState == HIGH && modeButtonLastState == LOW) { // button is released again
    modeButtonLastState = modeButtonCurrentState;
    if (displayActive && !modeButtonHoldFired && !modeButtonFired) {
      modeButtonAction();
    }
    modeButtonFired = false;
    modeButtonHoldFired = false;
  }

}

