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

// I2C LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Servo motor configuration
Servo servo1;
Servo servo2;

// Relay pin
const int relayPin = 9;

// Analog input pins
const int A1 = A0;
const int A2 = A1;

void setup() {
  // Initialize LCD
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);
  
  // Attach servo motors to pins
  servo1.attach(6);
  servo2.attach(7);
  
  // Set relay pin as output
  pinMode(relayPin, OUTPUT);
  
  // Set up analog input pins
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
}

void loop() {
  // Display starting message
  lcd.clear();
  lcd.print("Safety Helmet");
  delay(5000);
  
  // Display "Ready to Start"
  lcd.clear();
  lcd.print("Ready to Start");
  
  // Check for inputs
  if (digitalRead(A1) == HIGH) {
    lcd.clear();
    lcd.print("Wear Safety Helmet");
    digitalWrite(relayPin, HIGH); // Turn on relay
  }
  else {
    lcd.clear();
    lcd.print("Fuel Off");
    lcd.setCursor(0, 1);
    lcd.print("Engine Off");
    digitalWrite(relayPin, LOW); // Turn off relay
  }
  
  if (digitalRead(A2) == HIGH) {
    lcd.clear();
    lcd.print("Helmet Status OK");
    // Move servo motors from 0 to 45 degrees
    for (int angle = 0; angle <= 45; angle++) {
      servo1.write(angle);
      servo2.write(angle);
      delay(15);
    }
    lcd.clear();
    lcd.print("Fuel On");
    lcd.setCursor(0, 1);
    lcd.print("Engine On");
  }
  
  // Check if any input is low
  if (digitalRead(A1) == LOW || digitalRead(A2) == LOW) {
    lcd.clear();
    lcd.print("Fuel Off");
    lcd.setCursor(0, 1);
    lcd.print("Engine Off");
    digitalWrite(relayPin, LOW); // Turn off relay
    // Return servo motors to initial position
    for (int angle = 45; angle >= 0; angle--) {
      servo1.write(angle);
      servo2.write(angle);
      delay(15);
    }
  }
  
  delay(1000);
}
NOCOMNCVCCGNDINLED1PWRRelay Module