Diagram and i2cdevlib-master Download link
Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SOILWATCH_PIN A0
#define PUMP_PIN 2
#define DOWN_PIN 4
#define UP_PIN 3
#define MIN_TIME_BETWEEN_WATERING 2
#define MOISTURE_SET 0
#define WATER_FOR_SEC 5
LiquidCrystal_I2C lcd(0x3f, 16, 2); // Set the LCD I2C address
void setup()
{
pinMode ( UP_PIN, INPUT );
pinMode ( DOWN_PIN, INPUT );
//Set analog reference to internal 1.1V
analogReference(INTERNAL);
lcd.begin(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.home (); // go home
lcd.print("");
lcd.setCursor ( 0, 1 ); // go to the next line
lcd.print (" Et Discover ");
delay ( 1000 );
}
void loop()
{
lcd.home ();
static int dontwaterfor = MIN_TIME_BETWEEN_WATERING*10;
static int moistureSet = MOISTURE_SET;
static int watering = 0;
int analogValue = analogRead(SOILWATCH_PIN);
int moisture = map(analogValue, 0, 1023, 0, 100);
lcd.print("Moisture: ");
lcd.print(moisture);
lcd.print("% ");
int downButton = digitalRead(DOWN_PIN);
int upButton = digitalRead(UP_PIN);
if(downButton == HIGH)
{
if(moistureSet != 0) moistureSet--;
delay(50);
}
if(upButton)
{
if(moistureSet != 100) moistureSet++;
delay(50);
}
lcd.setCursor ( 0, 1 ); // go to the next line
lcd.print ("MoistureSet: ");
lcd.print (moistureSet);
lcd.print (" ");
if(dontwaterfor >= 1)dontwaterfor--;
if(((moisture < moistureSet) && (!dontwaterfor)) || (watering))
{
if(watering == 0)watering = WATER_FOR_SEC*10;
if(watering > 1){
digitalWrite(PUMP_PIN, HIGH);
}
else
{
digitalWrite(PUMP_PIN, LOW);
dontwaterfor = MIN_TIME_BETWEEN_WATERING*10;
}
watering--;
}
delay (100);
}
please do not enter any spam link in comment box