#include <Fuzzy.h>
#define SENSOR_PIN A0 // Su seviyesi sensörü pini
#define TEMP_PIN A1 // Potansiyometre pini (sıcaklık)
#define PUMP_PIN 8 // Pompa pini
Fuzzy *fuzzy = new Fuzzy();
void setupFuzzy() {
// Su Seviyesi (Water Level) Girdi Değişkeni
FuzzyInput *waterLevel = new FuzzyInput(1);
FuzzySet *low = new FuzzySet(0, 0, 100, 200); // Low
FuzzySet *medium = new FuzzySet(200, 400, 400, 600); // Medium
FuzzySet *high = new FuzzySet(600, 800, 1000, 1000); // High
waterLevel->addFuzzySet(low);
waterLevel->addFuzzySet(medium);
waterLevel->addFuzzySet(high);
fuzzy->addFuzzyInput(waterLevel);
// Sıcaklık (Temperature) Girdi Değişkeni
FuzzyInput *temperature = new FuzzyInput(2);
FuzzySet *cold = new FuzzySet(0, 0, 20, 30); // Cold
FuzzySet *warm = new FuzzySet(30, 40, 40, 60); // Warm
FuzzySet *hot = new FuzzySet(60, 70, 80, 80); // Hot
temperature->addFuzzySet(cold);
temperature->addFuzzySet(warm);
temperature->addFuzzySet(hot);
fuzzy->addFuzzyInput(temperature);
// Pompa Çalışma Süresi Çıktı Değişkeni
FuzzyOutput *pumpDuration = new FuzzyOutput(1);
FuzzySet *shortDuration = new FuzzySet(2000, 5000, 5000, 10000); // Short
FuzzySet *mediumDuration = new FuzzySet(10000, 15000, 15000, 20000); // Medium
FuzzySet *longDuration = new FuzzySet(20000, 25000, 25000, 30000); // Long
pumpDuration->addFuzzySet(shortDuration);
pumpDuration->addFuzzySet(mediumDuration);
pumpDuration->addFuzzySet(longDuration);
fuzzy->addFuzzyOutput(pumpDuration);
// Fuzzy Kurallar
// Kural 1: Su seviyesi Low ve sıcaklık Cold => Pompa süresi Long
FuzzyRuleAntecedent *ifLowAndCold = new FuzzyRuleAntecedent();
ifLowAndCold->joinWithAND(low, cold);
FuzzyRuleConsequent *thenLong = new FuzzyRuleConsequent();
thenLong->addOutput(longDuration);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifLowAndCold, thenLong);
fuzzy->addFuzzyRule(fuzzyRule1);
// Kural 2: Su seviyesi Medium ve sıcaklık Warm => Pompa süresi Medium
FuzzyRuleAntecedent *ifMediumAndWarm = new FuzzyRuleAntecedent();
ifMediumAndWarm->joinWithAND(medium, warm);
FuzzyRuleConsequent *thenMedium = new FuzzyRuleConsequent();
thenMedium->addOutput(mediumDuration);
FuzzyRule *fuzzyRule2 = new FuzzyRule(2, ifMediumAndWarm, thenMedium);
fuzzy->addFuzzyRule(fuzzyRule2);
// Kural 3: Su seviyesi High ve sıcaklık Hot => Pompa süresi Short
FuzzyRuleAntecedent *ifHighAndHot = new FuzzyRuleAntecedent();
ifHighAndHot->joinWithAND(high, hot);
FuzzyRuleConsequent *thenShort = new FuzzyRuleConsequent();
thenShort->addOutput(shortDuration);
FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifHighAndHot, thenShort);
fuzzy->addFuzzyRule(fuzzyRule3);
// Ek Kurallar Eklenebilir...
}
void setup() {
pinMode(PUMP_PIN, OUTPUT);
Serial.begin(9600);
setupFuzzy();
}
void loop() {
int waterLevelValue = analogRead(SENSOR_PIN); // Su seviyesini oku
// Potansiyometreden sıcaklık değerini oku ve ölçekle
int rawTempValue = analogRead(TEMP_PIN);
int temperatureValue = map(rawTempValue, 0, 1023, 0, 80); // 0-80 aralığına ölçeklendir
// Fuzzy girişleri ayarla
fuzzy->setInput(1, waterLevelValue);
fuzzy->setInput(2, temperatureValue);
fuzzy->fuzzify();
// Pompa çalışma süresini bulanık mantık ile hesapla
int pumpDurationValue = fuzzy->defuzzify(1);
Serial.print("Su Seviyesi: ");
Serial.print(waterLevelValue);
Serial.print(" - Sıcaklık: ");
Serial.print(temperatureValue);
Serial.print(" - Pompa Süresi: ");
Serial.println(pumpDurationValue);
// Pompa kontrolü
digitalWrite(PUMP_PIN, HIGH); // Pompa çalışıyor
delay(pumpDurationValue); // Belirlenen süre kadar bekle
digitalWrite(PUMP_PIN, LOW); // Pompa duruyor
delay(2000); // 2 saniye bekle
}