#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
#include <Servo.h> 
#include <Keypad.h>
#include <HX711_ADC.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1);


#define ntc_pin A2      // Pin, to which the voltage divider is connected
#define vd_power_pin 12        // 5V for the voltage divider
#define nominal_resistance 100       //Nominal resistance at 25⁰C
#define nominal_temeprature 0   // temperature for nominal resistance (almost always 25⁰ C)
#define samplingrate 5    // Number of samples
#define beta 3950  // The beta coefficient or the B value of the thermistor (usually 3000-4000) check the datasheet for the accurate value.
#define Rref 1000   //Value of  resistor used for the voltage divider
int samples = 0;



int buzzer = 13;
int GASA0 = A0;
int solenoid=3;
int fan=4;
int gasvalue;

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3


unsigned long startTime=0;
unsigned long targetTime=0;
unsigned long currentTime=0;
int angle = 0;

int sensorPin = 20;
int val = 0;
int whitsles = 0;
int i = 0;
int j = 0;
int tea =0;
int k;

//initializes an instance of the Keypad class
Keypad customKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
Servo servo_test;



void setup() {

pinMode(vd_power_pin,OUTPUT);

lcd.init();
  lcd.backlight(); 
  pinMode(11,OUTPUT);
  pinMode(sensorPin, INPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(solenoid, OUTPUT);
 pinMode(fan, OUTPUT);
 pinMode(buzzer, OUTPUT);
  Serial.begin (9600);
  lcd.begin(16, 2);
  servo_test.attach(11);
  lcd.print("welcome to"); 
 lcd.setCursor(1,1);
 lcd.print("smart gas stave");
   delay(2000); 
}


void loop () {

                  uint8_t k;
                  float average;
                  samples = 0;
  mainLoop :
  
         char customKey = customKeypad.getKey();

        
         whitsles = 0;
         i = 0;
         j = 0; 
  int analogSensor = analogRead(GASA0);
  int gasvalue=(analogSensor-50)/10;
  
  lcd.setCursor(0,0);
  lcd.print("GAS Level:");
  lcd.setCursor(10,0);
  lcd.print(gasvalue);
  lcd.setCursor(12,0);
  lcd.print("%");

  
  // Checks if it has reached the threshold value
  if (gasvalue >= 10)
  {
    lcd.setCursor(0,1);
  lcd.print("DANGER");
    digitalWrite(buzzer, HIGH);
    digitalWrite(solenoid, HIGH);
    digitalWrite(fan, HIGH);
  }
  else
  {
  lcd.setCursor(0,1);
  lcd.print("NORMAL");
    digitalWrite(buzzer, LOW);
    digitalWrite(solenoid, LOW);
    digitalWrite(fan, LOW);
  }
  delay(1000);

  
         if(customKey){
            if(customKey == 'A'){
                lcd.clear();      
                lcd.print("Mode A :");
                lcd.setCursor(4,1);
                lcd.print("Counter");
                delay(2000);
                lcd.clear();
                goto Counter;
            }

            if (customKey == 'B'){

                lcd.clear();      
                lcd.print("Mode B :");
                lcd.setCursor(4,1);
                lcd.print("timer");
                delay(2000);
                lcd.clear();
                goto timer;
            }

            if (customKey == 'C'){

                lcd.clear();      
                lcd.print("Mode C :");
                lcd.setCursor(4,1);
                lcd.print("temperature");
                delay(2000);
                lcd.clear();
                goto temperature;
            }

            if (customKeypad.getKey()=='*'){

              lcd.clear();
              lcd.print("|EDAI PROJECT|");
              lcd.setCursor(1,1);
              lcd.print("|GROUP : R12|");
              delay(3000);
              lcd.clear();
              goto mainLoop;
    
          }

            if (customKeypad.getKey()=='#'){

              lcd.clear();
              lcd.print("Programed By...");
              lcd.setCursor(1,1);
              lcd.print(" Sushrut :-)");
              delay(3000);
              lcd.clear();
              goto mainLoop;
    
          }

         }


          lcd.clear();
          goto mainLoop;

    Counter:

         
         lcd.clear();  
         lcd.print("Enter Whitsles : ");
         delay(100);
         char temp = customKeypad.getKey();
servo_test.write(0);
        if (temp =='D'){
              
              
              goto Servo_Test;
            
        }

        if (temp =='1'){

              lcd.clear();
              whitsles = 1;
              lcd.print("Whitsles = ");
              lcd.print(whitsles);
              delay(2000);
              goto Runner;
        }

        if (temp =='2'){

              lcd.clear();
              whitsles = 2;
              lcd.print("Whitsles = ");
              lcd.print(whitsles);
              delay(2000);
              goto Runner;
        }

        if (temp =='3'){

              lcd.clear();
              whitsles = 3;
              lcd.print("Whitsles = ");
              lcd.print(whitsles);
              delay(2000);
              goto Runner;
        }

        if (temp =='4'){

              lcd.clear();
              whitsles = 4;
              lcd.print("Whitsles = ");
              lcd.print(whitsles);
              delay(2000);
              goto Runner;
        }

        if (temp =='5'){

              lcd.clear();
              whitsles = 5;
              lcd.print("Whitsles = ");
              lcd.print(whitsles);
              delay(2000);
              goto Runner;
        }

        if (temp =='6'){

              lcd.clear();
              whitsles = 6;
              lcd.print("Whitsles = ");
              lcd.print(whitsles);
              delay(2000);
              goto Runner;
        }

        if (temp =='7'){

              lcd.clear();
              whitsles = 7;
              lcd.print("Whitsles = ");
              lcd.print(whitsles);
              delay(2000);
              goto Runner;
        }

        if (temp =='8'){

              lcd.clear();
              whitsles = 8;
              lcd.print("Whitsles = ");
              lcd.print(whitsles);
              delay(2000);
              goto Runner;
        }

        if (temp =='9'){

              lcd.clear();
              whitsles = 9;
              lcd.print("Whitsles = ");
              lcd.print(whitsles);
              delay(2000);
              goto Runner;
        }

        lcd.clear();
        goto Counter;


    Servo_Test :

              lcd.clear();
              lcd.print("Turning off...");
              servo_test.write(90);     
              delay(3000);
              lcd.clear();
              lcd.print("Exiting...");
              delay(2000);
              lcd.clear();
              goto mainLoop;


     timer :

             
              
              lcd.clear();  
              lcd.print("enter the time");

              delay(100);
              char minit = customKeypad.waitForKey();
               lcd.clear();
               lcd.print("minutes ");
              lcd.print(minit);
              String numberString = String(minit);
              while (minit != '#')
               {
                   minit = customKeypad.waitForKey();
                   
                   if (minit != NO_KEY && minit != '#')
                    {
                         numberString += minit;
                         lcd.clear();
                         lcd.print(numberString);
                    }
               }


                lcd.clear();
               lcd.print("seconds ");
               char sec = customKeypad.waitForKey();
              
              lcd.print(sec);
              String secon = String(sec);
              while (sec != '#')
               {
                   sec = customKeypad.waitForKey();
                   
                   if (minit != NO_KEY && sec != '#')
                    {
                         secon += sec;
                         lcd.clear();
                         lcd.print(secon);
                    }
               }

               
   servo_test.write(0);
       lcd.clear();
               lcd.print("time to cook");
               lcd.setCursor(0, 1);
               lcd.print(numberString);
               lcd.print(":");
               lcd.print(secon);
               delay(2000);
  
              if (numberString != NO_KEY && secon != NO_KEY) {

                   
                   long int time = numberString.toInt();
                   long int par = secon.toInt();

                   int mn=0;

                   int delayTime = (time) * 60;
                   delayTime = delayTime + par;
                   lcd.clear();
                   lcd.print("total time:");
                   lcd.print(delayTime);
                   i = delayTime;
                   while(i!= 0)
                   {
                     lcd.clear();
                          lcd.setCursor(0, 0);
                         lcd.print("Delay: ");
                         lcd.print( i );
                        lcd.setCursor(0,1);
                         lcd.print(" sec");
                     
                         i=i-1;
                         delay(1000);
                         if (customKeypad.getKey()=='D'){

                               goto Servo_Test;
                               }  
                   }
    
                  
               }
               goto Servo_Test;


          
          

     Runner :

         if (customKeypad.getKey()=='D'){

              goto Servo_Test;
        }

        lcd.clear();
        lcd.print("Counting...");
        lcd.setCursor(1,1);
        lcd.print(i);
        lcd.setCursor(9,1);
        


        if(i==whitsles){

          goto Servo_Test;
        }

        val = analogRead(A1);
     
        if(val > 900){

          i=i+1;
          lcd.clear();
          lcd.print("Whitsle Detected...");
          if(i!=whitsles)
          {
          delay(10000);
          }
        }

        delay(200);
        goto Runner;
        


     temperature :
             servo_test.write(0);
             lcd.clear();  
             lcd.print(" milk=1: water=2:");
             delay(100);
             char vale = customKeypad.waitForKey();
             lcd.clear();
             lcd.print(vale);
             delay(2000);
             if(vale=='1')
             {
              servo_test.write(0);
                   goto milk;
            
             }
             if(vale=='2')
             {
              servo_test.write(0);
                   goto water;
             }
    milk :
            if (customKeypad.getKey()=='D'){

              goto Servo_Test;
             }

                     samples=0;

                  digitalWrite(vd_power_pin,HIGH);
                  for (k=0; k< samplingrate; k++) {
                  samples += analogRead(ntc_pin);
                  delay(10);
                      }
                   digitalWrite(vd_power_pin,LOW);
                   average = 0;
                   average = samples/ samplingrate;
 
                   average = 1023 / average - 1;
                   average = Rref/ average;
                   Serial.print("Thermistor resistance ");
                   Serial.println(average);
           
                     lcd.clear();
                     lcd.print(average);
                     delay(2000);

                     if (average < 185) {
                            lcd.clear(); 
                            lcd.print("flame is on");
                            delay(2000); 
                         }
                         else{
                                goto Servo_Test;
                         }
                     goto milk;


    water :
              if (customKeypad.getKey()=='D'){

              goto Servo_Test;
                }
                samples=0;

                  digitalWrite(vd_power_pin,HIGH);
                  for (k=0; k< samplingrate; k++) {
                  samples += analogRead(ntc_pin);
                  delay(10);
                      }
                   digitalWrite(vd_power_pin,LOW);
                   average = 0;
                   average = samples/ samplingrate;
 
                   average = 1023 / average - 1;
                   average = Rref/ average;
                   Serial.print("Thermistor resistance ");
                   Serial.println(average);
           
                 

            
                    // tea = analogRead(A0);
                     lcd.clear();
                     lcd.print(average);
                     delay(2000);
                     if (average < 210) {
                            lcd.clear(); 
                            lcd.print("flame is on");
                            delay(2000); 
                         }
                         else{
                                goto Servo_Test;
                        }
                     goto water;
                    
            

}