#include <Fuzzy.h>
#include <DHT.h>
#include <Servo.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 the 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
FuzzySet *cold = new FuzzySet(0, 20, 20, 25); // Dingin
FuzzySet *medium = new FuzzySet(20, 25, 25, 30); // Sedang
FuzzySet *hot = new FuzzySet(25, 30, 30, 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
FuzzySet *small = new FuzzySet(0, 30, 30, 60); // Kecil
FuzzySet *mediumAngle = new FuzzySet(30, 60, 60, 90); // Sedang
FuzzySet *large = new FuzzySet(60, 90, 90, 100); // 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();
// Menampilkan suhu ke Serial Monitor
Serial.print("Suhu: ");
Serial.println(suhu);
// Set suhu sebagai input
fuzzy->setInput(1, suhu);
// Melakukan fuzzifikasi
fuzzy->fuzzify();
// Melakukan defuzzifikasi
float output = fuzzy->defuzzify(1);
// Mengatur sudut servo
myServo.write(output);
// Menampilkan sud ut servo ke Serial Monitor
Serial.print("Sudut AC: ");
Serial.println(output);
// Tunggu 5 detik sebelum pembacaan berikutnya
delay(5000);
}