 //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[];

  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, HIGH);
  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;
  
  //Temperatursensor initialisieren
  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 < NUMITEMS(menuEntrys); i++)  
    readTemperature(true);
  //---------------------------------------------------------------
  checkForErrors();
  delay(2000);
  if(errors > 0)
    fsmMain.immediateTransitionTo(stateError);
}

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 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 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(){

bool isHeating;

void setup()   {
  //LCD Setup
  lcd.InitLCD();
  lcd.clrScr();
  lcd.setFont(SmallFont);
