Baik, saya dapat membantu Anda dengan contoh kode untuk sistem pengendali suhu ruangan menggunakan 2 sensor DHT11, RTC (Real-Time Clock), I2C, motor driver untuk kipas, dan relay untuk elemen pemanas berdasarkan aturan fuzzy Mamdani.#include <Arduino.h>
#include <FuzzyRule.h>
#include <FuzzyRuleConsequent.h>
#include <FuzzyRuleAntecedent.h>
#include <FuzzySet.h>
#include <FuzzyEngine.h>
#include <FuzzyComposition.h>
#include <FuzzyInput.h>
#include <FuzzyOutput.h>
#include <FuzzyIO.h>
// Definisikan variabel input dan output
FuzzyInput* temperature1 = new FuzzyInput(1);
FuzzyInput* temperature2 = new FuzzyInput(2);
FuzzyOutput* fan_speed = new FuzzyOutput(1);
FuzzyOutput* heater = new FuzzyOutput(2);
// Definisikan fungsi keanggotaan untuk variabel input
temperature1->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::LOW, 0, 0, 20));
temperature1->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::MEDIUM, 10, 20, 30));
temperature1->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::HIGH, 20, 30, 40));
temperature1->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::VERY_HIGH, 30, 40, 40));
temperature2->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::LOW, 0, 0, 20));
temperature2->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::MEDIUM, 10, 20, 30));
temperature2->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::HIGH, 20, 30, 40));
temperature2->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::VERY_HIGH, 30, 40, 40));
// Definisikan fungsi keanggotaan untuk variabel output
fan_speed->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::VERY_LOW, 0, 0, 100));
fan_speed->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::LOW, 0, 100, 200));
fan_speed->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::MEDIUM, 100, 200, 300));
fan_speed->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::HIGH, 200, 300, 400));
fan_speed->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::VERY_HIGH, 300, 400, 400));
heater->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::OFF, 0, 0, 50));
heater->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::LOW, 0, 50, 100));
heater->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::MEDIUM, 50, 100, 150));
heater->addMembershipFunction(new FuzzyMembershipFunction(FuzzyTermType::HIGH, 100, 150, 200));
// Definisikan aturan fuzzy
FuzzyRule* rule1 = new FuzzyRule(1, IF(temperature1 IS LOW) AND (temperature2 IS LOW) THEN (fan_speed IS LOW) AND (heater IS HIGH));
FuzzyRule* rule2 = new FuzzyRule(2, IF(temperature1 IS MEDIUM) AND (temperature2 IS MEDIUM) THEN (fan_speed IS MEDIUM) AND (heater IS MEDIUM));
FuzzyRule* rule3 = new FuzzyRule(3, IF(temperature1 IS HIGH) AND (temperature2 IS HIGH) THEN (fan_speed IS HIGH) AND (heater IS LOW));
FuzzyRule* rule4 = new FuzzyRule(4, IF(temperature1 IS VERY_HIGH) AND (temperature2 IS VERY_HIGH) THEN (fan_speed IS VERY_HIGH) AND (heater IS OFF));
// Buat mesin fuzzy dan tambahkan aturan
FuzzyEngine* fuzzy = new FuzzyEngine();
fuzzy->addFuzzyInput(temperature1);
fuzzy->addFuzzyInput(temperature2);
fuzzy->addFuzzyOutput(fan_speed);
fuzzy->addFuzzyOutput(heater);
fuzzy->addFuzzyRule(rule1);
fuzzy->addFuzzyRule(rule2);
fuzzy->addFuzzyRule(rule3);
fuzzy->addFuzzyRule(rule4);
void setup() {
Serial.begin(9600);
}
void loop() {
// Baca sensor suhu
int temp1 = 25; // Ganti dengan nilai sensor suhu 1
int temp2 = 28; // Ganti dengan nilai sensor suhu 2
// Evaluasi sistem fuzzy
temperature1->setInput(temp1);
temperature2->setInput(temp2);
fuzzy->evaluate();
// Dapatkan nilai output
int fan_speed_value = fan_speed->defuzzify();
int heater_value = heater->defuzzify();
// Cetak hasil
Serial.print("Suhu 1: ");
Serial.print(temp1);
Serial.print(", Suhu 2: ");
Serial.print(temp2);
Serial.print(", Kecepatan Kipas: ");
Serial.print(fan_speed_value);
Serial.print(", Pemanas: ");
Serial.println(heater_value);
// Tunda sebelum loop berikutnya
delay(1000);
}
Ini adalah contoh pseudocode yang dapat Anda gunakan sebagai panduan:
```
// Inisialisasi pin-pin dan library
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include <RTClib.h>
#include <FuzzyRule.h>
#include <FuzzyComposition.h>
#include <Fuzzyinference.h>
#include <FuzzyIO.h>
#define DHTPIN1 4
#define DHTPIN2 5
#define RELAY_PIN 12
#define MOTOR_PIN 13
DHT_Unified dht1(DHTPIN1, DHT11);
DHT_Unified dht2(DHTPIN2, DHT11);
RTC_DS1307 rtc;
// Inisialisasi Fuzzy Logic
FuzzyInput* temperature1 = new FuzzyInput(1);
FuzzyInput* temperature2 = new FuzzyInput(2);
FuzzyOutput* fan_speed = new FuzzyOutput(1);
FuzzyOutput* heater = new FuzzyOutput(2);
// Fungsi Membership
void initFuzzyLogic() {
// Definisikan membership function untuk input dan output
// ...
// Definisikan aturan fuzzy
FuzzyRule* rule1 = new FuzzyRule(1, IF(temperature1 IS LOW) AND (temperature2 IS LOW) THEN (fan_speed IS LOW) AND (heater IS HIGH));
FuzzyRule* rule2 = new FuzzyRule(2, IF(temperature1 IS MEDIUM) AND (temperature2 IS MEDIUM) THEN (fan_speed IS MEDIUM) AND (heater IS MEDIUM));
FuzzyRule* rule3 = new FuzzyRule(3, IF(temperature1 IS HIGH) AND (temperature2 IS HIGH) THEN (fan_speed IS HIGH) AND (heater IS LOW));
FuzzyRule* rule4 = new FuzzyRule(4, IF(temperature1 IS VERY_HIGH) AND (temperature2 IS VERY_HIGH) THEN (fan_speed IS VERY_HIGH) AND (heater IS OFF));
// Tambahkan aturan ke fuzzy inference engine
fuzzy->addFuzzyRule(rule1);
fuzzy->addFuzzyRule(rule2);
fuzzy->addFuzzyRule(rule3);
fuzzy->addFuzzyRule(rule4);
}
void setup() {
// Inisialisasi sensor, RTC, motor driver, dan relay
dht1.begin();
dht2.begin();
rtc.begin();
pinMode(RELAY_PIN, OUTPUT);
pinMode(MOTOR_PIN, OUTPUT);
// Inisialisasi Fuzzy Logic
initFuzzyLogic();
}
void loop() {
// Baca suhu dari sensor DHT11
sensors_event_t event1, event2;
dht1.temperature().getEvent(&event1);
dht2.temperature().getEvent(&event2);
// Dapatkan waktu saat ini dari RTC
DateTime now = rtc.now();
// Jalankan logika fuzzy
temperature1->setInput(event1.temperature);
temperature2->setInput(event2.temperature);
fuzzy->evaluate();
int fan_speed_value = fan_speed->getOutput()->getValue();
int heater_value = heater->getOutput()->getValue();
// Kontrol motor dan relay berdasarkan output fuzzy
analogWrite(MOTOR_PIN, fan_speed_value);
digitalWrite(RELAY_PIN, heater_value);
// Tunggu beberapa detik sebelum mengambil data lagi
delay(5000);
}
```
Kode di atas adalah contoh dasar untuk sistem pengendali suhu ruangan menggunakan 2 sensor DHT11, RTC, I2C, motor driver untuk kipas, dan relay untuk elemen pemanas berdasarkan aturan fuzzy Mamdani. Anda mungkin perlu menyesuaikan kode ini sesuai dengan kebutuhan proyek Anda, seperti mendefinsikan fungsi keanggotaan dan aturan fuzzy yang sesuai. Selain itu, Anda juga perlu mengintegrasikan kode ini dengan modul ESP32 dan komponen lainnya yang digunakan.