#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#define relay 13
#define MAX_FLOWTIME 180 // seconds
#define MIN_FLOWTIME 30 // seconds
#define MAX 1022
#define MIN 5
int volumePin = A0;
long duration;
long interval = 3000;
long oldDuration;
void setup()
{
Serial.begin(9600);
pinMode(relay, OUTPUT);
lcd.init();
lcd.backlight();
}
void water() {
static bool state;
static unsigned long time;
if((millis() — time) > (state ? duration : interval))
{
state = !state;
digitalWrite(relay, state);
time = millis();
Serial.println(duration);
}
}
void lcd_print() {
lcd.setCursor(0,0);
lcd.print(duration / 1000);
}
void loop() {
duration = map(analogRead(volumePin), MIN, MAX, MIN_FLOWTIME, MAX_FLOWTIME) * 1000;
if (duration != oldDuration){
lcd.clear();
}
lcd_print();
water();
oldDuration = duration;
}