#include <LiquidCrystal.h>
#include <Servo.h>
// =======================
// Konfigurasi LCD
// =======================
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// =======================
// Konfigurasi Pin
// =======================
const int o2Pin = A0; // Input sensor O₂ (potensiometer)
const int rpmPin = A1; // Input simulasi RPM (potensiometer)
const int servoPin = 6; // Output servo
Servo limiterServo; // Objek servo
// =======================
// Variabel Global
// =======================
float o2Voltage = 0;
int o2Value = 0;
int rpmValue = 0;
int rpm = 0;
int servoAngle = 0;
String mixtureStatus;
void setup() {
lcd.begin(16, 2);
limiterServo.attach(servoPin);
lcd.print("ECU SIMULATOR");
lcd.setCursor(0, 1);
lcd.print("Init System...");
delay(2000);
lcd.clear();
}
void loop() {
// =======================
// BACA SENSOR O₂
// =======================
o2Value = analogRead(o2Pin);
o2Voltage = o2Value * (5.0 / 1023.0);
if (o2Voltage < 0.3) mixtureStatus = "LEAN";
else if (o2Voltage <= 0.7) mixtureStatus = "IDEAL";
else mixtureStatus = "RICH";
// =======================
// BACA PULSER (SIMULASI RPM)
// =======================
rpmValue = analogRead(rpmPin);
rpm = map(rpmValue, 0, 1023, 0, 8000); // Skala 0–8000 RPM
// =======================
// KONTROL SERVO
// =======================
if (rpm < 1000) servoAngle = 0;
else if (rpm >= 1000 && rpm <= 5000) {
// Linear antara 1000 → 45°, 5000 → 90°
servoAngle = map(rpm, 1000, 5000, 45, 90);
}
else servoAngle = 90;
limiterServo.write(servoAngle);
// =======================
// TAMPILKAN DATA
// =======================
lcd.setCursor(0, 0);
lcd.print("RPM:");
lcd.print(rpm);
lcd.print(" "); // hapus sisa karakter lama
lcd.setCursor(0, 1);
lcd.print("O2:");
lcd.print(o2Voltage, 2);
lcd.print("V ");
lcd.print(mixtureStatus);
delay(300);
}