#include <Fuzzy.h>
#include <FuzzyComposition.h>
#include <FuzzyInput.h>
#include <FuzzyIO.h>
#include <FuzzyOutput.h>
#include <FuzzyRule.h>
#include <FuzzyRuleAntecedent.h>
#include <FuzzyRuleConsequent.h>
#include <FuzzySet.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Inisialisasi LCD dengan alamat I2C 0x27 dan ukuran 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Mendefinisikan pin untuk sensor dan LED
#define LDR A0
#define TRIGGER 4
#define ECHO 3
#define LED1 A1
// Membuat objek fuzzy untuk sistem inferensi fuzzy
Fuzzy *fuzzy = new Fuzzy();
void setup() {
// Memulai komunikasi serial dengan kecepatan 9600 bps
Serial.begin(9600);
// Mengatur mode pin
pinMode(LDR, INPUT);
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
// Inisialisasi LCD
lcd.init();
// Membuat fuzzy set untuk input jarak
FuzzySet *small = new FuzzySet(0, 0, 0, 120);
FuzzySet *mid = new FuzzySet(60, 120, 120, 180);
FuzzySet *big = new FuzzySet(120, 180, 180, 240);
FuzzySet *verybig = new FuzzySet(180, 240, 400, 400);
// Membuat fuzzy set untuk input nilai LDR
FuzzySet *lowldr = new FuzzySet(0, 0, 0, 100);
FuzzySet *midldr = new FuzzySet(60, 200, 500, 700);
FuzzySet *highldr = new FuzzySet(400 ,700, 1023, 1023);
// Membuat fuzzy set untuk output kecerahan LED
FuzzySet *off = new FuzzySet(155, 230, 255, 255);
FuzzySet *lowb = new FuzzySet(55, 100, 155, 200);
FuzzySet *midb = new FuzzySet(0, 0, 25, 100);
FuzzySet *highb = new FuzzySet(0, 0, 0, 0);
// Menambahkan fuzzy set untuk input jarak ke dalam sistem fuzzy
FuzzyInput *distance = new FuzzyInput(1);
distance->addFuzzySet(small);
distance->addFuzzySet(mid);
distance->addFuzzySet(big);
fuzzy->addFuzzyInput(distance);
// Menambahkan fuzzy set untuk input nilai LDR ke dalam sistem fuzzy
FuzzyInput *ldr = new FuzzyInput(2);
ldr->addFuzzySet(lowldr);
ldr->addFuzzySet(midldr);
ldr->addFuzzySet(highldr);
fuzzy->addFuzzyInput(ldr);
// Menambahkan fuzzy set untuk output kecerahan LED ke dalam sistem fuzzy
FuzzyOutput *brightness = new FuzzyOutput(1);
brightness->addFuzzySet(off);
brightness->addFuzzySet(lowb);
brightness->addFuzzySet(midb);
brightness->addFuzzySet(highb);
fuzzy->addFuzzyOutput(brightness);
// Menyusun aturan fuzzy
// Jika jarak kecil dan LDR rendah, maka kecerahan LED tinggi
FuzzyRuleAntecedent *ifDistanceSmallAndLdrIsLow = new FuzzyRuleAntecedent();
ifDistanceSmallAndLdrIsLow->joinWithAND(small, lowldr);
FuzzyRuleConsequent *thenBrightnessHigh = new FuzzyRuleConsequent();
thenBrightnessHigh->addOutput(highb);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifDistanceSmallAndLdrIsLow, thenBrightnessHigh);
fuzzy->addFuzzyRule(fuzzyRule1);
// Jika jarak kecil dan LDR tinggi, maka kecerahan LED mati
FuzzyRuleAntecedent *ifDistanceSmallAndLdrIsHigh = new FuzzyRuleAntecedent();
ifDistanceSmallAndLdrIsHigh->joinWithAND(small, highldr);
FuzzyRuleConsequent *thenBrightnessOff = new FuzzyRuleConsequent();
thenBrightnessOff->addOutput(off);
FuzzyRule *fuzzyRule2 = new FuzzyRule(2, ifDistanceSmallAndLdrIsHigh, thenBrightnessOff);
fuzzy->addFuzzyRule(fuzzyRule2);
// Jika jarak sedang, maka kecerahan LED sedang
FuzzyRuleAntecedent *ifDistanceMid = new FuzzyRuleAntecedent();
ifDistanceMid->joinSingle(mid);
FuzzyRuleConsequent *thenBrightnessMidb = new FuzzyRuleConsequent();
thenBrightnessMidb->addOutput(midb);
FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifDistanceMid, thenBrightnessMidb);
fuzzy->addFuzzyRule(fuzzyRule3);
// Jika jarak besar, maka kecerahan LED rendah
FuzzyRuleAntecedent *ifDistanceBig = new FuzzyRuleAntecedent();
ifDistanceBig->joinSingle(big);
FuzzyRuleConsequent *thenBrightnessLow = new FuzzyRuleConsequent();
thenBrightnessLow->addOutput(lowb);
FuzzyRule* fuzzyRule4 = new FuzzyRule(4, ifDistanceBig, thenBrightnessLow);
fuzzy->addFuzzyRule(fuzzyRule4);
// Jika jarak sangat besar, maka kecerahan LED mati
FuzzyRuleAntecedent *ifDistanceVeryBig = new FuzzyRuleAntecedent();
ifDistanceVeryBig->joinSingle(verybig);
FuzzyRule* fuzzyRule5 = new FuzzyRule(5, ifDistanceVeryBig, thenBrightnessOff);
fuzzy->addFuzzyRule(fuzzyRule5);
}
// Fungsi untuk menghitung jarak menggunakan sensor ultrasonik
int distance() {
digitalWrite(TRIGGER, LOW);
delayMicroseconds(5);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
long pulse = pulseIn(ECHO, HIGH) / 2;
return pulse * 10 / 292;
}
// Fungsi untuk membaca nilai LDR
int brightness() {
return analogRead(LDR);
}
void loop() {
int Dis = distance(); // Menghitung jarak
int Bri = brightness(); // Membaca nilai LDR
// Memasukkan nilai jarak dan LDR ke sistem fuzzy
fuzzy->setInput(1, Dis);
fuzzy->setInput(2, Bri);
fuzzy->fuzzify(); // Melakukan fuzzifikasi
// Melakukan defuzzifikasi dan mendapatkan output kecerahan LED
int output = fuzzy->defuzzify(1);
Bri = (Bri*-1)+1015;
analogWrite(LED1, output); // Mengatur kecerahan LED
Serial.print(" Dis:");
Serial.print(Dis);
Serial.print(" Bri:");
Serial.print(Bri);
Serial.print(" LED:");
Serial.print(output);
Serial.println();
// Menampilkan hasil pada LCD
lcd.setCursor(0, 0);
lcd.print("Dis:");
lcd.print(Dis);
lcd.print(" ");
lcd.print("Bri:");
lcd.print(Bri);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("LED St:");
if (output > 130){
lcd.print("ON (");
lcd.print(output);
lcd.print(") ");
}
else {
lcd.print("OFF (");
lcd.print(output);
lcd.print(") ");
}
// Menunggu 100 ms sebelum mengulang loop
delay(100);
}