#include <Fuzzy.h>
#include <DHT.h>
#include <ESP32Servo.h>
// Definisikan pin untuk DHT11 dan Servo
#define DHTPIN 2 // Pin untuk DHT11
#define SERVO_PIN 18 // Pin untuk Servo
// Inisialisasi objek DHT
DHT dht(DHTPIN, DHT11);
// Instansiasi objek Fuzzy
Fuzzy *fuzzy = new Fuzzy();
Servo myServo; // Objek Servo
void setup() {
// Set Serial output
Serial.begin(9600);
// Mulai sensor DHT
dht.begin();
// Inisialisasi Servo
myServo.attach(SERVO_PIN);
// Instantiating a FuzzyInput object for temperature
FuzzyInput *temperature = new FuzzyInput(1);
// FuzzySet untuk suhu (dengan overlap lebih rapi)
FuzzySet *cold = new FuzzySet(0, 20, 22, 25); // Dingin
FuzzySet *medium = new FuzzySet(23, 25, 27, 30); // Sedang
FuzzySet *hot = new FuzzySet(28, 30, 35, 40); // Panas
// Menambahkan FuzzySet ke FuzzyInput
temperature->addFuzzySet(cold);
temperature->addFuzzySet(medium);
temperature->addFuzzySet(hot);
// Menambahkan FuzzyInput ke Fuzzy
fuzzy->addFuzzyInput(temperature);
// Instantiating a FuzzyOutput object for servo angle
FuzzyOutput *angle = new FuzzyOutput(1);
// FuzzySet untuk sudut servo
FuzzySet *small = new FuzzySet(0, 30, 30, 60); // Sudut kecil
FuzzySet *mediumAngle = new FuzzySet(30, 60, 60, 90); // Sudut sedang
FuzzySet *large = new FuzzySet(60, 90, 90, 120); // Sudut besar
// Menambahkan FuzzySet ke FuzzyOutput
angle->addFuzzySet(small);
angle->addFuzzySet(mediumAngle);
angle->addFuzzySet(large);
// Menambahkan FuzzyOutput ke Fuzzy
fuzzy->addFuzzyOutput(angle);
// Aturan Fuzzy
// IF suhu = dingin THEN sudut = besar
FuzzyRuleAntecedent *ifCold = new FuzzyRuleAntecedent();
ifCold->joinSingle(cold);
FuzzyRuleConsequent *thenLarge = new FuzzyRuleConsequent();
thenLarge->addOutput(large);
FuzzyRule *rule1 = new FuzzyRule(1, ifCold, thenLarge);
fuzzy->addFuzzyRule(rule1);
// IF suhu = sedang THEN sudut = sedang
FuzzyRuleAntecedent *ifMedium = new FuzzyRuleAntecedent();
ifMedium->joinSingle(medium);
FuzzyRuleConsequent *thenMedium = new FuzzyRuleConsequent();
thenMedium->addOutput(mediumAngle);
FuzzyRule *rule2 = new FuzzyRule(2, ifMedium, thenMedium);
fuzzy->addFuzzyRule(rule2);
// IF suhu = panas THEN sudut = kecil
FuzzyRuleAntecedent *ifHot = new FuzzyRuleAntecedent();
ifHot->joinSingle(hot);
FuzzyRuleConsequent *thenSmall = new FuzzyRuleConsequent();
thenSmall->addOutput(small);
FuzzyRule *rule3 = new FuzzyRule(3, ifHot, thenSmall);
fuzzy->addFuzzyRule(rule3);
}
void loop() {
// Membaca suhu dari DHT11
float suhu = dht.readTemperature();
// Cek apakah pembacaan suhu valid
if (isnan(suhu)) {
Serial.println("Gagal membaca suhu dari DHT11!");
delay(2000); // Tunggu sebentar sebelum mencoba lagi
return;
}
// Menampilkan suhu ke Serial Monitor
Serial.print("Suhu: ");
Serial.println(suhu);
// Set suhu sebagai input fuzzy
fuzzy->setInput(1, suhu);
// Melakukan fuzzifikasi
fuzzy->fuzzify();
// Melakukan defuzzifikasi
float output = fuzzy->defuzzify(1);
// Menjaga output dalam range 0-180 derajat
output = constrain(output, 0, 180);
// Mengatur sudut servo
myServo.write(output);
// Menampilkan sudut servo ke Serial Monitor
Serial.print("Sudut Servo: ");
Serial.println(output);
// Tunggu 5 detik sebelum pembacaan berikutnya
delay(2000);
}