#include <Animately.h>
#include <Core/Timeline.h>
#include <Adafruit_Sensor.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h> // servo library
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // A5 to SCL , A4 to SDA
#define LED1 2//
#define LED2 3 //
#define LED3 4 // MOTOR LED
#define LED4 5
#define RELAY 6 // motor relay high = off , low = on
#define DHTPIN1 7
#define BUZZER 8
#define SERVOMOTOR 9
#define TRIG 10
#define ECHO 11
#define SOILMOIST1 A0
#define RAINFALL A1
DHT HT (DHTPIN1, DHT11); // creating the object
Servo myservo;
//define global varibles used for timing
const long eventInterval_LED1 = 1000;
const long eventInterval_LED2 = 1000;
const long eventInterval_LED3 = 1000;
const long eventInterval_LED4 = 1000;
unsigned long previousTime_1 =0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 =0;
unsigned long previousTime_4 =0;
// define gloabl varibales for system parameters
float tankLvlCM;
float duration_us;
float soilMos1;
float tempC;
float rainfallVal;
int motorpos = 0;
int minwatertankLvl= 16; // THE HEIGHT OF THE TANK IS 20 CM SO and dont want water to get below 4 cm so when the sensor see that is more tha n 16 cm measure water level to low
int rainfallThreashhold= 50;
int soilMoisturemin = 25; // when measured mositure is above this value then action
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // Initialise Serial debug output
// put your setup code here, to run once:
HT.begin(); //Initialise the DHT sensor , starting the object
//delay(500);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(RELAY,OUTPUT);
pinMode(BUZZER,OUTPUT);
pinMode(RAINFALL,INPUT);
pinMode(TRIG, OUTPUT); // Relay Module PIN D8
pinMode(ECHO, INPUT);
myservo.attach(SERVOMOTOR); // Servo PIN D9
digitalWrite(RELAY, LOW); // Relay Normally High for OFF condition
digitalWrite(TRIG, LOW); // trig normally low
digitalWrite (BUZZER, LOW); // buzzer pin normally low
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("SMART IRRAGATION SYSTEM ");
lcd.setCursor(0, 1);
lcd.print("IS ACTIVATED!");
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentTime= millis();
measureDistance() ;
indiLED1();
measureTemperature ();
indiLED2();
measureSoilmositure();
indiLED3();
measureRainfall ();
indiLED4();
if (tankLvlCM < minwatertankLvl )
{
if (rainfallVal < rainfallThreashhold )
{
if (soilMos1< soilMoisturemin) {
if (tempC >= 4.4 && tempC < 21) {
waterpumplowtime();
}
else if ( tempC >= 21) {
waterpumphightime();
}
else {
return; // return back to the start of void loop to get new measurement values
}
}
else
{
return; // return back to the start of void loop to get new measurement values
}
}
else
{
return; // return back to the start of void loop to get new measurement values
}
}
else {
lowWaterlevelnotify();
}
return;
}
//declaring indicating led functions
void indiLED1(){
// using millis to create a delay of 8 seconds
digitalWrite (LED1,HIGH );
Serial.print("distance: ");
Serial.print(tankLvlCM);
Serial.println("cm");
lcd.setCursor(0,0);
lcd.print("Current Water");
lcd.setCursor(0,1);
lcd.print("Level: ");
lcd.setCursor(8,1);
lcd.print("tankLvlCM");
delay(2000);
lcd.clear();
digitalWrite (LED1,LOW );
}
void indiLED2(){
digitalWrite (LED2,HIGH );
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(tempC);
lcd.print((char)223);
lcd.print("C");
Serial.println("temperature:");
Serial.println(tempC);
delay(2000); // here i want to delay the system so that the lcd stays on for 5 seconds and the text printed on the lcd stays on for 5 seconds ,
lcd.clear();
digitalWrite (LED2,LOW );
}
void indiLED3(){
digitalWrite (LED3,HIGH );
lcd.setCursor(0,0);
lcd.print("Soil Moisture:");
lcd.setCursor(0,1);
lcd.print(soilMos1);
//lcd.print("%");
Serial.println(soilMos1);
delay(2000); // here i want to delay the system so that the lcd stays on for 5 seconds and the text printed on the lcd stays on for 5 seconds ,
lcd.clear();
digitalWrite (LED3,LOW);
}
void indiLED4(){
digitalWrite (LED4,HIGH);
lcd.setCursor(0,0);
lcd.print("Rainfall Level: ");
lcd.setCursor(0,1);
lcd.print(rainfallVal);
Serial.println(rainfallVal);
delay(2000); // here i want to delay the system so that the lcd stays on for 5 seconds and the text printed on the lcd stays on for 5 seconds ,
lcd.clear();
digitalWrite (LED4,LOW);
}
void measureDistance() {
// this first measurment is getting distance of the water level in the water tank
digitalWrite (LED1, HIGH );
digitalWrite ( TRIG, HIGH ); // getting distance of the sensor
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration_us = pulseIn(ECHO, HIGH);
tankLvlCM == 0.017 * duration_us;
}
//declaring
void measureTemperature () {
tempC= HT.readTemperature(DHTPIN1);
}
void measureSoilmositure () {
soilMos1= analogRead(SOILMOIST1);
soilMos1 = map(soilMos1,0,1024,0,100);
}
void measureRainfall () {
rainfallVal = analogRead(RAINFALL);
rainfallVal = map(rainfallVal, 0, 1023, 0, 100);
}
void lowWaterlevelnotify(){
do {
digitalWrite (BUZZER, HIGH);
lcd.setCursor(0,0);
lcd.print("Water Tank Level to low ! ");
}while (tankLvlCM >= minwatertankLvl);
}
void waterpumplowtime(){
digitalWrite(RELAY, HIGH); //relay goes high so that the water pump turns on
lcd.setCursor(0,0);
lcd.print("MOTOR STATUS:ON");
// add in a timer herefor 15 sec
for (motorpos = 0; motorpos <= 180; motorpos += 1)
{ // goes from 0 degrees to 180 degrees // in steps of 1 degree
myservo.write(motorpos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (motorpos = 180; motorpos >= 0; motorpos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(motorpos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(RELAY, LOW);
}
void waterpumphightime() {
digitalWrite(RELAY, HIGH); //relay goes high so that the water pump turns on
lcd.setCursor(0,0);
lcd.print("MOTOR STATUS:ON");
// add in a timer here for 30 seconds
for (motorpos = 0; motorpos <= 180; motorpos += 1)
{ // goes from 0 degrees to 180 degrees // in steps of 1 degree
myservo.write(motorpos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (motorpos = 180; motorpos >= 0; motorpos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(motorpos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
digitalWrite(RELAY, LOW);
}
in my code in each indicating led function i call i use a large delay which i think is messing up my system , i want to try to use the millis () function and see if that would help but im having trouble trying to implement it iinto my code