#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Pin definisi
const int SolenoidGasPin = 8; // Solenoid untuk gas
const int SolenoidBensinPin = 9; // Solenoid untuk bensin
const int relayACPin = 10; // Relay AC
const int buzzerPin = 11; // Pin untuk buzzer
const int gasSensorPin = A0; // Sensor kebocoran gas
const int tempSensorPin = A1; // Sensor suhu mesin (LM35 atau NTC)
const int buttonModePin = 2; // Tombol untuk perpindahan manual/otomatis
const int clutchSwitchPin = 3; // Tombol untuk deteksi pedal kopling (untuk idle AC)
const int rpmSensorPin = A2; // Pin untuk sensor RPM (misalnya CKP sensor)
// Servo control
Servo servoMixer; // Servo untuk kontrol campuran udara dan gas
Servo servoIdleAC; // Servo untuk idle AC
Servo servoIdleRPM; // Servo untuk idle RPM
// Variables
bool isAutoMode = true; // Mode otomatis default
float tempThreshold = 90.0; // Threshold suhu mesin (misalnya 90°C)
int gasThreshold = 300; // Threshold untuk kebocoran gas (sesuaikan nilai)
// LCD I2C 4x20
LiquidCrystal_I2C lcd(0x27, 20, 4); // Alamat I2C dan ukuran LCD
// Fungsi untuk membaca suhu mesin
float readEngineTemp() {
int tempValue = analogRead(tempSensorPin); // Baca nilai dari sensor suhu
float voltage = (tempValue / 1024.0) * 5.0; // Konversi ke tegangan
float temperature = voltage * 100.0; // Konversi ke derajat Celsius (untuk LM35)
return temperature;
}
// Fungsi untuk deteksi kebocoran gas
void checkGasLeakage() {
int gasValue = analogRead(gasSensorPin);
if (gasValue > gasThreshold) {
// Kebocoran gas terdeteksi
digitalWrite(SolenoidGasPin, LOW); // Matikan solenoid gas
digitalWrite(SolenoidBensinPin, LOW); // Matikan solenoid bensin
digitalWrite(relayACPin, LOW); // Matikan AC
digitalWrite(buzzerPin, HIGH); // Aktifkan buzzer
// Tampilkan di LCD
lcd.setCursor(0, 2);
lcd.print("Gas Leak Detected! ");
} else {
// Sistem gas normal
digitalWrite(buzzerPin, LOW); // Matikan buzzer
lcd.setCursor(0, 2);
lcd.print("Gas System Normal ");
}
}
// Fungsi untuk memantau suhu mesin
void checkEngineTemp() {
float engineTemp = readEngineTemp();
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(engineTemp);
lcd.print(" C ");
if (engineTemp > tempThreshold) {
// Mesin panas
digitalWrite(relayACPin, LOW); // Matikan AC
lcd.setCursor(0, 3);
lcd.print("AC Off: Overheat ");
} else {
lcd.setCursor(0, 3);
lcd.print("AC System Active ");
}
}
// Fungsi untuk perpindahan manual/otomatis bahan bakar
void handleFuelSwitch() {
if (digitalRead(buttonModePin) == HIGH) {
isAutoMode = !isAutoMode; // Switch mode
}
if (isAutoMode) {
lcd.setCursor(0, 0);
lcd.print("Mode: Auto ");
digitalWrite(SolenoidGasPin, HIGH); // Aktifkan gas
digitalWrite(SolenoidBensinPin, LOW); // Matikan bensin
} else {
lcd.setCursor(0, 0);
lcd.print("Mode: Manual ");
digitalWrite(SolenoidGasPin, LOW); // Matikan gas
digitalWrite(SolenoidBensinPin, HIGH); // Aktifkan bensin
}
}
// Fungsi untuk kontrol servo campuran gas dan udara
void controlMixerAirGas() {
int rpm = analogRead(rpmSensorPin); // Baca sensor RPM
int mixerAngle = map(rpm, 0, 1023, 0, 180); // Sesuaikan dengan kebutuhan
servoMixer.write(mixerAngle); // Gerakkan servo sesuai RPM
}
// Fungsi untuk idle AC kontrol
void handleIdleAC() {
if (digitalRead(clutchSwitchPin) == HIGH) {
// Jika AC aktif dan idle AC perlu diatur
servoIdleAC.write(90); // Set servo idle AC
lcd.setCursor(0, 1);
lcd.print("Idle AC Active ");
} else {
servoIdleAC.write(0); // Idle AC tidak aktif
lcd.setCursor(0, 1);
lcd.print("Idle AC Inactive ");
}
}
// Fungsi utama loop
void loop() {
handleFuelSwitch(); // Periksa mode manual/otomatis
checkGasLeakage(); // Periksa kebocoran gas
checkEngineTemp(); // Periksa suhu mesin
controlMixerAirGas(); // Kontrol campuran udara dan gas
handleIdleAC(); // Kontrol idle AC
delay(500); // Delay untuk pembacaan sensor
}
void setup() {
// Pin setup
pinMode(SolenoidGasPin, OUTPUT);
pinMode(SolenoidBensinPin, OUTPUT);
pinMode(relayACPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonModePin, INPUT_PULLUP);
pinMode(clutchSwitchPin, INPUT_PULLUP);
// Inisialisasi servo
servoMixer.attach(6); // Servo untuk campuran gas/udara
servoIdleAC.attach(7); // Servo untuk idle AC
servoIdleRPM.attach(5); // Servo untuk idle RPM (bila diperlukan)
// Inisialisasi LCD
lcd.begin(20, 4); // Inisialisasi LCD I2C 4x20
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Fuel Control System");
delay(1000); // Tunggu 1 detik sebelum mulai
}