Window opener opens window again and again due to Void loop

See if this works better (or worse) :slight_smile:

// These constants won't change:
const int analogPin = A0;    // pin that the sensor is attached to
const int analogPin1 = A1;    // pin that the sensor is attached to
const int ledPin = 9;       // pin that the LED is attached to
const int ledPin1 = 8;       // pin that the LED is attached
const int threshold = 20;   // Temperatur threshold
const int threshold1 = 145;   // an arbitrary threshold level that's in the range of the analog input
bool windowOpen = false;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
   pinMode(ledPin1, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);
  int analogValue1 = analogRead(analogPin1);
  int lastAnalogValue = 0;     // previous state of the button
  //Temperatur in Spannung umrechnen
  float voltage = (analogValue1/1024.0) * 5.0;
  //Temperatur in °C ausrechnen
  float temperature = (voltage - .5) * 100;

  // if the analog value is high enough, turn on the LED:
  if (temperature > threshold && windowOpen == false){
    if (lastAnalogValue<threshold){
    digitalWrite(ledPin, HIGH);
       Serial.println("Fenster öffnet");
    delay(4000);
    windowOpen = true;
    digitalWrite(ledPin, LOW);
   }
  } else {
    digitalWrite(ledPin, LOW);
     Serial.println("Fenster öffnet nicht");
  }
 if (temperature < threshold && windowOpen == true) {
  if (lastAnalogValue>threshold){
    digitalWrite(ledPin1, HIGH);
       Serial.println("Fenster schließt");
    delay(4000);
  windowOpen = false;
    digitalWrite(ledPin1, LOW);
  }
     } else {
       digitalWrite(ledPin1, LOW);
        Serial.println("Fenster schließt nicht");
        }
  // print the analog value:
 Serial.println("Dunkelheit ADC");
  Serial.println(analogValue);
  Serial.println("Temperatur ADC");
    Serial.println(analogValue1);
   Serial.println("Temperatur in °C");
    Serial.println(temperature);
      Serial.println("--------------------------------------------------------------  10s Delay");
  delay(10000);        // delay in between reads for stability
  lastAnalogValue=temperature;
 }
 // Je heller, desto niedriger ist der Dunkelheit- Wert
 // Je wärmer,desto höher temperature1