#include <Servo.h>
Servo myservo;  // create servo object to control a servo
//
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//
int potpin = A0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
//
const int Delay = 500; // delay function 
const int LED = 25;   // LED pin number 
const int ServoPin = 2;  // Servo pin number
const int interruptPin18 = 18; 
volatile bool isButtonPressed18 = false;
//
      
void setup() 
{
  myservo.attach(ServoPin);  // servo connected to pin 2
  //
  Serial.begin(9600);
  //
  lcd.init();
  lcd.backlight();
  lcd.setCursor(3, 0);
  lcd.print("VALVE OPEN");
  //
   pinMode(LED, OUTPUT);
   //
   pinMode(interruptPin18, INPUT_PULLUP);
   attachInterrupt(digitalPinToInterrupt(interruptPin18), button_pressed18, CHANGE);
   //
}

void loop() 
{
 ValveControl();
  lcdShow();               // waits for the servo to get there
}


void button_pressed18()
{
   Serial.println("Button Pressed");
   isButtonPressed18 = true;
}

void ValveControl()
{
//
if(isButtonPressed18 == true)
 {
  val = 0;           // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);   
  ledblink();
  delay(Delay); 
 }//

else
 {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);   
  digitalWrite(LED, LOW);               // sets the servo position according to the scaled value
  delay(Delay); 
 }
}
//
void ledblink()
{
  digitalWrite(LED, LOW);               // sets the servo position according to the scaled value
  delay(Delay); 
  digitalWrite(LED, HIGH);               // sets the servo position according to the scaled value
  delay(Delay); 
}
//
void lcdShow()
{
  lcd.clear();
  lcd.setCursor(3, 0);
if (isButtonPressed18) 
  lcd.print("VALVE DOWN");
else 
  lcd.print("VALVE OPEN");
  lcd.setCursor(6, 1);
  lcd.print((val*100)/180);
  lcd.print("%");  
delay(Delay); 
}