So I just don't forget to mention that I'm absolute beginner working and learning code and arduino for less then a month for now, and mostly when I learn and search, I'm looking for working examples so I would understand what each line does and how.
So when I google around I find useful info, but I get stuck implementing it or using it in my sketch.
Maybe I'm missing some basics probably, but I'm trying to catch up.
So far this is what I came up with.
I want to control 1 fan on 1 relay and to set up min and max range for temperature and humidity
#include <Wire.h>
#include "DHT.h"
#include "LiquidCrystal.h"
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#define DHTPIN 8
#define DHTTYPE DHT22
DHT sensor(DHTPIN, DHTTYPE);
const int vent = 12; // ventilation relay pin
const int humy = 11; // humidifier relay pin
//values to turn off and on ventilation based on temperature and RH
float maxTemp = 28.50;
float minTemp = 26.80;
float maxHumy = 55.00;
float minHumy = 53.00;
//values to turn off and on humidifier based on RH
float maxHumy1 = 48.00;
float minHumy1 = 45.00;
void setup() {
lcd.begin(16, 2);
sensor.begin();
digitalWrite(vent, HIGH);
pinMode(vent, OUTPUT);
digitalWrite(humy, HIGH);
pinMode(humy, OUTPUT);
}
void loop() {
lcd.clear();
float t = sensor.readTemperature(); //reading the temperature from the sensor
float h = sensor.readHumidity(); //reading the humidity from the sensor
h = map(h, 21.8, 91.2, 15.6, 77.6); //calibration I did to match my hygrometers
// Checking if the sensor is sending values or not
if (isnan(t) || isnan(h)) {
lcd.print("Failed");
return;
}
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(t);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("H:");
lcd.print(h);
lcd.print("%");
//control for ventilation
if (t >= maxTemp) {
digitalWrite(vent, LOW);
lcd.setCursor(9, 0);
lcd.print("V:On");
}
else if (t <= minTemp) {
digitalWrite(vent, HIGH);
lcd.setCursor(9, 0);
lcd.print("V:Off");
}
//controll for humidifier
if (h <= minHumy1) {
digitalWrite(humy, LOW);
lcd.setCursor(9, 1);
lcd.print("H:On");
}
else if (h >= maxHumy1) {
digitalWrite(humy, HIGH);
lcd.setCursor(9, 1);
lcd.print("H:Off");
}
}
double map(double x, double in_min, double in_max, double out_min, double out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
I tried it like this, but then it only triggers at max values(maxTemp and maxHumy)
//control for ventilation
if (t >= maxTemp || h >=maxHumy) {
digitalWrite(vent, LOW);
lcd.setCursor(9, 0);
lcd.print("V:On");
}
else if (t <= minTemp || h <=minHumy) {
digitalWrite(vent, HIGH);
lcd.setCursor(9, 0);
lcd.print("V:Off");
}
Also I have trouble displaying correctly digitalwrite when it is in the range, it actualy doesnt display at all, but above max or under min its all ok.
That you would say I need boolean there and when I searched bool I got confused even more, as how do you add or set 1 pin for one bool, and 2nd pin for other bool ?
EDIT: This post was changed due to bad/wrong expresion.