You did not put this line where I told you to
int lastAnalogValue = 0;
The way you have it the value gets reset to 0 every time loop() repeats. Either move it to the top of the program to make it a global variable or change the present line to
static int lastAnalogValue = 0;
See how much easier it is to read your code when it is indented consistently. Use the AutoFormat tool.
// 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
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 && lastAnalogValue<threshold){
digitalWrite(ledPin, HIGH);
Serial.println("Fenster öffnet");
delay(4000);
digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin, LOW);
Serial.println("Fenster öffnet nicht");
}
if (temperature < threshold && lastAnalogValue>threshold) {
digitalWrite(ledPin1, HIGH);
Serial.println("Fenster schließt");
delay(4000);
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 temperature
...R