So, thanks to your help I fixed the comliling error using just the line from the example.
I also changed my sketch so that the temperature value and threshold are in °C.
So it now looks like this:
// 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) {
if (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) {
if (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 temperature1
The only problem is that the window still "opens" again.
Looking at my sketch I realized that if I have two if- statements, I also need two else statements.
But how do I implement them, given that they are completeley seperated from the two if- statements with {} ?