#include <GravityTDS.h> #include <EEPROM.h> #include <Bounce2.h> #include <DHT.h> #include <LiquidCrystal_I2C.h> #define setBUTTON 12 #define upBUTTON 8 #define downBUTTON 13 #define DHTPIN 7 #define DHTTYPE DHT11 #define TdsSensorPin A1
Bounce set; Bounce up; Bounce down;
GravityTDS gravityTds; DHT dht(DHTPIN, DHTTYPE); LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
byte HsetpointHIGH = 55; byte HsetpointLOW = 50; byte FANspeed = 70; float temperature = 25,tdsValue = 0; int Relaystate; int outputValue; int sensorValue = 0; int ledState; int Sensor; unsigned long previousMillis1; unsigned long previousMillis2; unsigned long previousMillis3; static byte prevdownButton; static byte prevupButton; static byte prevsetButton; static byte prevSetMode; byte setMode; const int Hrelay = 5; const int Mosfet = 3; const int sensorWater = A0; const int waterklep = 6; const int led = 9; const int PIR = 2; const int Display = 10; long OnTime = 0; long OffTime = 10000;
void setup() {
lcd.init(); // initialize the lcd lcd.backlight(); lcd.begin(16, 2); dht.begin(); digitalWrite (Display, LOW); digitalWrite (Hrelay, LOW); digitalWrite (waterklep, HIGH); analogWrite(led,255); set.attach (setBUTTON); set.interval(30); up.attach (upBUTTON); up.interval(30); down.attach (downBUTTON); down.interval(30);
pinMode (setBUTTON, INPUT_PULLUP); pinMode (upBUTTON, INPUT_PULLUP); pinMode (downBUTTON, INPUT_PULLUP); pinMode (waterklep, OUTPUT); pinMode (Hrelay, OUTPUT); pinMode (PIR, INPUT_PULLUP); HsetpointHIGH = EEPROM.read(0); HsetpointLOW = EEPROM.read(1); FANspeed = EEPROM.read(2);
gravityTds.setPin(TdsSensorPin); gravityTds.setAref(5.0); //reference voltage on ADC, default 5.0V on Arduino UNO gravityTds.setAdcRange(1024); //1024 for 10bit ADC;4096 for 12bit ADC gravityTds.begin(); //initialization
}
void loop() {
PIR, Sensor; if (PIR == 0){Sensor = LOW;} if (PIR == 1){Sensor = HIGH;} if (Sensor == HIGH){ void On(); }
if (Sensor == LOW) { void Off(); }
void On(){ unsigned long currentMillis = millis();
set.update(); up.update(); down.update(); sensorValue = analogRead(sensorWater); unsigned long h = dht.readHumidity(); float t = dht.readTemperature();
analogWrite(Mosfet, outputValue);
//temperature = readTemperature(); //add your temperature sensor and read it gravityTds.setTemperature(t); // set the temperature and execute temperature compensation gravityTds.update(); //sample and calculate tdsValue = gravityTds.getTdsValue(); // then get the value
if (isnan(h) || isnan(t)) { lcd.clear(); lcd.setCursor(1,0); lcd.print("Failed to read "); lcd.setCursor(0,1); lcd.print("from DHT sensor!"); delay(2000); lcd.clear(); return; }
if (h<HsetpointHIGH) {Relaystate = LOW; outputValue = map(FANspeed, 0, 100, 126, 140);} if (h>HsetpointLOW) {Relaystate = HIGH; analogWrite(Mosfet,0);} digitalWrite (Hrelay, Relaystate); sensorValue = analogRead(sensorWater); if (sensorValue<=20){ledState = LOW;} if (sensorValue>=21){ledState = HIGH;} if((ledState == HIGH) && (currentMillis - previousMillis1 >= OnTime)) { ledState = LOW; // Turn it off analogWrite(led,0); previousMillis1 = currentMillis; // Remember the time digitalWrite(waterklep, ledState); // Update the actual LED } else if ((ledState == LOW) && (currentMillis - previousMillis1 >= OffTime)) { ledState = HIGH; // turn it on analogWrite(led,255); previousMillis1 = currentMillis; // Remember the time digitalWrite(waterklep, ledState); // Update the actual LED }
if ( setMode != 0) { if (currentMillis - previousMillis2 >= 5000) { setMode = 0; previousMillis2 = currentMillis; } }
if (set.fell()) { setMode++; } prevsetButton = setBUTTON; if (setMode >= 4) { setMode = 0; } if (setMode != prevSetMode) { lcd.clear(); prevSetMode = setMode; }
switch (setMode) { case 0://normal lcd.setCursor(0,0); lcd.print("Hum:"); lcd.setCursor(5,0); lcd.print(h); lcd.setCursor(7,0); lcd.print("%"); lcd.setCursor(0,1); lcd.print("Temp:"); lcd.setCursor(7,1); lcd.print(t); lcd.setCursor(12,1); lcd.print((char)223); lcd.print("C"); lcd.setCursor(9,0); lcd.print(tdsValue,0); lcd.setCursor(12,0); lcd.print("ppm"); previousMillis2 = currentMillis; break; case 1://edit temp HsetpointLOW = constrain(HsetpointLOW, 00, 100); if ('upButton' != prevupButton) { if (up.fell()) { HsetpointLOW = HsetpointLOW + 1; } prevupButton = 'upButton'; } if ('downButton' != prevdownButton) { if (down.fell()) { HsetpointLOW = HsetpointLOW - 1; } prevdownButton = 'downButton'; } lcd.setCursor(0, 0); lcd.print("Set Humidity ON:"); lcd.setCursor(7, 1); lcd.print(HsetpointLOW); lcd.setCursor(10,1); lcd.print("%"); EEPROM.write(1, HsetpointLOW); break; case 2://edit humidity HsetpointHIGH = constrain(HsetpointHIGH, 00, 100); if ('upButton' != prevupButton) { if (up.fell()) { HsetpointHIGH = HsetpointHIGH + 1; } prevupButton = 'upButton'; } if ('downButton' != prevdownButton) { if (down.fell()) { HsetpointHIGH = HsetpointHIGH - 1; } prevdownButton = 'downButton'; } lcd.setCursor(0, 0); lcd.print("Set Humidity OFF:"); lcd.setCursor(7, 1); lcd.print(HsetpointHIGH); lcd.setCursor(10,1); lcd.print("%"); EEPROM.write(0, HsetpointHIGH); break;
case 3://edit FANspeed FANspeed = constrain(FANspeed, 0, 100); if ('upButton' != prevupButton) { if (up.fell()) { FANspeed = FANspeed + 10; } prevupButton = 'upButton'; } if ('downButton' != prevdownButton) { if (down.fell()) { FANspeed = FANspeed - 10; } prevdownButton = 'downButton'; } lcd.setCursor(0, 0); lcd.print("Set FANspeed:"); lcd.setCursor(7, 1); lcd.print(FANspeed); lcd.setCursor(9,1); lcd.print("%");
EEPROM.write(2, FANspeed); break; } }
void Off(){ PIR, Sensor; digitalWrite (Display, HIGH); digitalWrite (Hrelay, HIGH); digitalWrite (waterklep, HIGH); analogWrite(led,0); outputValue,0 } } |