 //Fehler festlegen
 #define ERROR_NO_THERMOMETER 1
 
-int errors = 0;
-
-//Display einrichten 
+int errors = 0, lastErrors = 0;
+
+//Display einrichten
 //Old LCD5110 lcd(8, 9, 10, 11, 12);
 LCD5110 lcd(PIN_LCD_SCLK, PIN_LCD_MOSI, PIN_LCD_DC, PIN_LCD_RST, PIN_LCD_SCE);
 extern uint8_t SmallFont[];


//Main FSM und States definieren
State stateMenu = State(enterMenu, menu, leaveMenu);
State stateError = State(error);
State stateMaischen = State(enterMaischen, maischen, leaveMaischen);
State stateKochen = State(enterKochen, kochen, leaveKochen);
State stateHeizen = State(enterHeizen, heizen, leaveHeizen);

  printRow(PROG_NAME" "PROG_VERSION, LEFT, 16);
  printRow(COPYRIGHT, LEFT, 32);
  printRow(COPYRIGHT2, RIGHT, 40);

#if SERIAL != N
  Serial.begin(9600);
#endif

  pinMode(PIN_BG_LIGHT, OUTPUT);
  pinMode(PIN_HEATER, OUTPUT);
  digitalWrite(PIN_BG_LIGHT, LOW);
  digitalWrite(PIN_HEATER, HIGH);

  //Encoder und Interrupt Setup
  encoder = new ClickEncoder(PIN_ROTARY_1, PIN_ROTARY_2, PIN_ROTARY_BUTTON, 4);
  encoder->setAccelerationEnabled(false);
  Timer1.initialize(1000);
  Timer1.attachInterrupt(timerIsr);

  //Temperaturen und Heizung initialisieren
  setSollTemp(MIN_TEMP);
  isHeating = false;

 initThermometer();

  checkForErrors();
  delay(2000);
  if (errors > 0) {
    fsmMain.immediateTransitionTo(stateError);
    alarm();
  } else {
    fsmMain.immediateTransitionTo(stateMenu);
  }
}

void loop() {
  fsmMain.update();

  ClickEncoder::Button b = encoder->getButton();
  if (b != ClickEncoder::Open) {
    if ( b == ClickEncoder::Held ) {
      fsmMain.transitionTo(stateMenu);
    }
  }

  readTemperature(false);
  checkHeatingStatus(false);
  addTemperature(false);

  if (alertMillis != 0)
    doAlert();
}

void initThermometer(){
  sensors.begin();
  sensors.getAddress(thermometer, 0);
  // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  sensors.setResolution(thermometer, 9);
  //Init Temp array
  for (int i = 0; i < TMP_SMOOTH; i++)
    readTemperature(true);
}

void enterMenu() {
  encoder->setAccelerationEnabled(false);
  //selectedMenuEntry = 0;
  //menuOffset = 0;
  setSollTemp(MIN_TEMP);
  checkHeatingStatus(true);
  clrScr(true, false);
  printRow(PROG_NAME" "PROG_VERSION, CENTER, 0);
  forceFirstDisplay();
}

void leaveMenu() {
  encoder->setAccelerationEnabled(true);
}

void enterError() {
  setSollTemp(MIN_TEMP);
  checkHeatingStatus(true);
  clrScr(true, true);
  printRow("--Fehler--", CENTER, 0);
  forceFirstDisplay();
}

void enterMaischen() {
  encoder->setAccelerationEnabled(true);
  fsmMaischen.immediateTransitionTo(stateEnterEinmaischTemp);
  clrScr(true, false);
  printRow("--Maischen--", CENTER, 0);
}

void leaveMaischen() {
  setSollTemp(MIN_TEMP);
}

void enterKochen() {
  encoder->setAccelerationEnabled(true);
  setSollTemp(100);
  checkHeatingStatus(true);
  fsmKochen.immediateTransitionTo(stateEnterKochzeit);
  clrScr(true, false);
  printRow("--Kochen--", CENTER, 0);
}

void leaveKochen() {
  setSollTemp(MIN_TEMP);
}

void enterHeizen() {
  encoder->setAccelerationEnabled(true);
  setSollTemp(temp);
  clrScr(true, false);
  printRow("--Heizen--", CENTER, 0);
  forceFirstDisplay();
}

void leaveHeizen() {
  setSollTemp(MIN_TEMP);
}

void enterTimer() {
  encoder->setAccelerationEnabled(true);
  fsmTimer.immediateTransitionTo(stateEnterTimerTime);
  clrScr(true, false);
  printRow("--Timer--", CENTER, 0);
}

/*void enterSettings(){
