#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //0X3E for proteus

int Button_1  = 2;
int Button_2  = 3;
int vol       = A0;
int motor     = 5;
int motor_on  = 4;
boolean Motor_trig  = false;
int val_Button_1, val_Button_2;
int val_vol;
int map_vol;
int map_vol_Serial;

unsigned long timer_1 = millis;
void setup() {
  pinMode(Button_1,INPUT);
  pinMode(Button_2,INPUT);
  pinMode(vol,INPUT);
  pinMode(motor,OUTPUT);
  pinMode(motor_on,OUTPUT);
  
  Serial.begin(112500);
  lcd.init();
  lcd.print("Setup Start");
  delay(1000);
}

void loop() {
  val_Button_1  = digitalRead(Button_1);
  val_Button_2  = digitalRead(Button_2);
  val_vol       = analogRead(vol);

  //if(millis()-timer_1 >= 300){

  map_vol = map(val_vol,0,1023,0,255);
  map_vol_Serial  = map(val_vol,0,1023,0,100);
  /*Serial.print("B1 = ");
  Serial.println(val_Button_1);
  Serial.print("B2 = ");
  Serial.println(val_Button_2);
  Serial.print(val_vol);*/
  Serial.print(Motor_trig);

  if (val_Button_1 == 0){
    Motor_trig  = true;
  }
  if (val_Button_2 == 0){
    Motor_trig  = false;
  }

  if (Motor_trig == true){
    Serial.println("ON");
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("ON");
    lcd.setCursor(0,1);
    lcd.print("Speed ");
    lcd.setCursor(5,1);
    lcd.print(map_vol_Serial);
    digitalWrite(motor_on,HIGH);
    analogWrite(motor,map_vol);
  }
  if (Motor_trig == false){
    lcd.clear();
    Serial.println("OFF");
    lcd.setCursor(0,0);
    lcd.println("OFF");
    analogWrite(motor,LOW);
    digitalWrite(motor_on,LOW);

  }
  delay(300);
  //}timer_1 += 300;
}