Hello everyone, I'm making a project where the fan turns on (using a relay) when it reaches the threshold temperature (given with buttons). It's made out of a relay (fan), temperature sensor (DTH11), 2 buttons( for setting the threshold temperature)... anyways, everything works fine, but one thing does not. It's that when you move the threshold temperature up to the current room temp, let's say the room temp is 25, and when I reach 25 with the buttons, the fan turns on, and while the threshold temp is still 25, and the room temp drops to 24, the fan still says on, moving the threshold temp to 24, still on, and when I move the temp under 24, the fan turns off. Moving up to 24 again, the fan turns on, also, cutting the power on the temp sensor (making the temp value "0"), the fan still works...
This seems unlogical. The full code:
//DHT11 Sensor:
#include "DHT.h"
#define DHTPIN 12 // what digital pin we're connected to
#define DHTTYPE DHT11
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
int relay = 7;DHT dht(DHTPIN, DHTTYPE);
int buttonState = 0;#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(relay, OUTPUT);digitalWrite(relay, HIGH);
lcd.begin(16,2);
dht.begin();
}void loop() {
static int stock = 20;int t = dht.readTemperature();
lcd.setCursor(0, 0);
lcd.print("Curr.Temp: ");
lcd.print(t);
lcd.print("C");buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
delay(250);
++stock;
lcd.setCursor(0, 1);
lcd.print("Thrs.Temp: ");
lcd.print(stock);
lcd.print("C");
}buttonState = digitalRead(buttonPin2);
if (buttonState == HIGH) {
delay(250);
--stock;
lcd.setCursor(0, 1);
lcd.print("Zad.Temp: ");
lcd.print(stock);
lcd.print("C");
}if (stock >= t) {
digitalWrite(relay, LOW);
}
if (stock < t) {
digitalWrite(relay, HIGH);
}}
Thanks in advance, and I hope that this is the right forum to ask. :![]()