#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);  

#define extractor 6
#define fan       8
#define machine   7
#define gasSensor 0
#define rled      5
#define bled      3
#define gled      4

float  data;
String operatingMode;
const unsigned int threshold=25;
const unsigned int maxlimit=90;

void setup() 
{ 
  Serial.begin(9600);
  Serial.println("system initialize");
  //pinMode(buzzer,OUTPUT);
  pinMode(fan,OUTPUT);
  pinMode(machine,OUTPUT);
  pinMode(extractor,OUTPUT);
  pinMode(rled,OUTPUT);
  pinMode(bled,OUTPUT);
  pinMode(gled,OUTPUT);
  //digitalWrite(buzzer,LOW);
  digitalWrite(extractor,LOW);
  digitalWrite(machine,LOW);
  digitalWrite(gled,LOW);
  digitalWrite(rled,LOW);
  digitalWrite(bled,LOW);
  lcd.init();
  lcd.backlight();
  welcomeDisplay();
  delay(1000);
  digitalWrite(bled,HIGH);
  digitalWrite(fan,HIGH);

}

void loop() 
{
  home_display();
  digitalWrite(machine,HIGH);
  data=map(analogRead(gasSensor),0,1023,0,100);
  if(data>=threshold)
  {
    digitalWrite(gled,HIGH);
    digitalWrite(rled,LOW);
    digitalWrite(extractor,HIGH);
    if(data>=maxlimit)
    {
      digitalWrite(gled,LOW);
      digitalWrite(rled,HIGH);
      digitalWrite(extractor,HIGH);
      digitalWrite(machine,LOW);
      operatingMode="HOOD OVER LOADED";
    }
  }
  else
  {
    digitalWrite(extractor,LOW);
    digitalWrite(gled,HIGH);
    digitalWrite(rled,LOW);
  }
  delay(500);
}

/*
void Buzzer(uint8_t num)
{
  for(uint8_t j=0;j<num;j++)
  {
    digitalWrite(buzzer,HIGH);
    delay(500);
    digitalWrite(buzzer,LOW);
    delay(250);
  }
}*/

void welcomeDisplay()
{
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("   WELDING FUMES    "); 
     lcd.setCursor(0,1);
     lcd.print("  EXTRACTION HOOD   "); 
     delay(2000);
     lcd.setCursor(0,2);
     lcd.print("INITIALIZING SYSTEM ");
     lcd.setCursor(0,3);
     lcd.print("PLEASE WAIT ........");
}

void home_display()
{
     lcd.clear();
     lcd.setCursor(0,0);
     lcd.print("   WELDING FUMES    "); 
     lcd.setCursor(0,1);
     lcd.print("  EXTRACTION HOOD   ");  
     lcd.setCursor(0,2);
     lcd.print("FUMES LEVEL(%):"); 
     lcd.setCursor(15,2);
     lcd.print(data,2);  
     lcd.setCursor(0,3);
     lcd.print(operatingMode);  
}

void alert_Display(uint8_t num)
 {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(">>> !ALERT! <<<<");
    lcd.setCursor(0,1);
    if(num==0){lcd.print("FUMES CONCENTRATION IS TOO HIGH");}
    if(num==1){lcd.print("MACHINE STOPPED FOR SAFETY");}
 }
WELDING FUMES EXTRACTION HOOD
FUMES EXTRACT
WELDING MACHINE
EXTRACTOR FAN
GAS SENSOR
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module