Skip to content

Instantly share code, notes, and snippets.

@ansarid
Created October 24, 2017 02:25
Show Gist options
  • Save ansarid/afbec706301429508f127e8df1e6444b to your computer and use it in GitHub Desktop.
Save ansarid/afbec706301429508f127e8df1e6444b to your computer and use it in GitHub Desktop.
// Enviromental Managment System
// Pins:
// LCD - 2,3,4,5,6,7
// DHT11/22 - 8
// Sprayer - 9
// Vent Servo - 10
// Fan - 11
// Buzzer - 12
// Moisture 1 - A0
// Moisture 2 - A1
// Moisture 3 - A2
// Moisture 4 - A3
// Button - A4
// Pot - A5
#include <Arduino.h>;
#include <LiquidCrystal.h>
#include <DHT.h>
#include <Servo.h>
#define DHTTYPE DHT22
#define DHTPIN 36
Servo myservo;
int LIGHT1 = 46;
int LIGHT2 = 44;
// int PUMP = 48;
int moistureSensor1 = A0;
int moistureSensor2 = A1;
int moistureSensor3 = A2;
int moistureSensor4 = A3;
int moistureSensor5 = A4;
int moistureSensor1Val = 0;
int moistureSensor2Val = 0;
int moistureSensor3Val = 0;
int moistureSensor4Val = 0;
int moistureSensor5Val = 0;
int aveargeMoisture = 1000;
const int buzzer = 12;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(53, 51, 49, 47, 45, 43);
int setHumidity = 50; //What I want the humidity to be.
//int settemperature = 36; //What I want the temperature to be. (C)
double potValueOld = 0;
double potValueNew = 0;
double humidity = 0;
double hRestoreTime = 0;
double temperatureC = 0;
double temperatureF = 0;
bool seeDHT = false;
int setupLCD() {
lcd.clear();
lcd.print("Temp: ** C ** F");
lcd.setCursor(0, 1);
lcd.print("Humidity: **%");
lcd.setCursor(8, 0);
lcd.print((char)223);
lcd.setCursor(13, 0);
lcd.print((char)223);
}
int setupServos() {
myservo.attach(12);
if (myservo.read() != 0) {
Serial.print("Servo not at zero!\nAttempting to set servo to zero! - ");
Serial.print(myservo.read());
myservo.write(0);
if (myservo.read() != 0) {
Serial.print("/nError setting servo to zero! - ");
Serial.print(myservo.read());
}
else {
Serial.print("\nServo set to zero! - ");
Serial.print(myservo.read());
}
myservo.detach();
myservo.attach(10);
if (myservo.read() != 128) {
Serial.print("Servo not at zero!\nAttempting to set servo to zero! - ");
Serial.print(myservo.read());
myservo.write(128);
if (myservo.read() != 128) {
Serial.print("/nError setting servo to zero! - ");
Serial.print(myservo.read());
}
else {
Serial.print("\nServo set to zero! - ");
Serial.print(myservo.read());
}
myservo.detach();
}
else {
Serial.print("Servo already set to zero! - ");
Serial.print(myservo.read());
}
}
}
int noDHT() {
seeDHT = false;
while (isnan(temperatureF) && isnan(temperatureC) && isnan(humidity)) {
seeDHT = true;
lcd.clear();
lcd.print("ERROR: DHT11 not");
lcd.setCursor(0, 1);
lcd.print("detected!");
Serial.println("ERROR: DHT11 not detected!");
for (int i = 0; i != 1; i = 1 ) {
tone(buzzer, 2000);
delay(500);
noTone(buzzer);
delay(500);
}
humidity = dht.readHumidity();
temperatureC = dht.readTemperature();
temperatureF = dht.readTemperature(true);
if (!isnan(temperatureF) && !isnan(temperatureC) && !isnan(humidity)) {
seeDHT = true;
setupLCD();
}
}
}
int checkDHT() {
if (isnan(temperatureF) && isnan(temperatureC) && isnan(humidity)) {
noDHT();
}
}
int showHumidity() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity Set at:");
lcd.setCursor(7, 1);
lcd.print(int(setHumidity));
lcd.setCursor(9, 1);
lcd.print("%");
delay(3000);
}
/*
int changeHumidity() {
if (potValueNew != = potValueOld) {
}
// lcd.setCursor(9,1);
// lcd.print("%");
// delay(3000);
}
*/
int logData() {
humidity = dht.readHumidity();
temperatureC = dht.readTemperature();
temperatureF = dht.readTemperature(true);
lcd.setCursor(6, 0);
lcd.print(int(temperatureC));
Serial.print("Temperature(C): ");
Serial.println(temperatureC);
delay(100);
lcd.setCursor(11, 0);
lcd.print(int(temperatureF));
Serial.print("Temperature(F): ");
Serial.println(temperatureF);
delay(100);
lcd.setCursor(11, 1);
lcd.print(int(humidity)); \
Serial.print("Humidity(%): ");
Serial.println(humidity);
delay(100);
}
/*
int checkPot(){
if(analogRead(A0) != setHumidity){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Humidity Set at:");
lcd.setCursor(0,1);
lcd.print(setHumidity);
delay(3000);
}
}
*/
int checkHumidity() {
if (humidity < setHumidity) {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Humidity");
lcd.setCursor(7, 1);
lcd.print("LOW");
delay(1000);
Serial.println("Humidity Low!");
setupLCD();
spray();
}
}
int spray() {
myservo.attach(9);
myservo.write(180);
delay(1000);
myservo.write(0);
for ( double i = 0 ; i < 60000 ; i++) {
while (humidity < setHumidity) {
logData();
hRestoreTime++;
delay(1000);
myservo.detach();
}
Serial.println("Humidity Restored in ");
Serial.print(int(hRestoreTime));
Serial.print(" seconds");
hRestoreTime = 0;
return;
}
}
int getDHT() {
checkDHT();
logData();
}
int getSoilMoisture() {
moistureSensor1Val = analogRead(moistureSensor1);
Serial.print("Moisture Sensor 1: " );
Serial.println(moistureSensor1Val);
moistureSensor2Val = analogRead(moistureSensor2);
Serial.print("Moisture Sensor 2: " );
Serial.println(moistureSensor2Val);
moistureSensor3Val = analogRead(moistureSensor3);
Serial.print("Moisture Sensor 3: " );
Serial.println(moistureSensor3Val);
moistureSensor4Val = analogRead(moistureSensor4);
Serial.print("Moisture Sensor 4: " );
Serial.println(moistureSensor4Val);
moistureSensor5Val = analogRead(moistureSensor5);
Serial.print("Moisture Sensor 5: " );
Serial.println(moistureSensor5Val);
}
//int water() {
// if (aveargeMoisture > 1000){
// digitalWrite(PUMP,LOW);
// }
//}
void setup() {
pinMode(LIGHT1, OUTPUT);
pinMode(LIGHT2, OUTPUT);
// pinMode(PUMP, OUTPUT);
pinMode(buzzer, OUTPUT);
tone(buzzer, 1000);
delay(1000);
noTone(buzzer);
Serial.begin(9600);
lcd.begin(16, 2);
// potValue = analogRead(A0);
// setHumidity = int(potValue * 0.09889);
showHumidity();
dht.begin();
myservo.attach(9);
setupLCD();
delay(500);
Serial.println("Setting up!");
setupServos();
Serial.print("\nHumidity set to: ");
Serial.print(setHumidity);
delay(1000);
Serial.print("\n\n===================================================================================\n\n");
}
void loop() {
getDHT();
// water();
getSoilMoisture();
delay(1000); //Sample Rate of DHT11
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment