#include <Arduino.h>
#include <Fuzzy.h>
Fuzzy *fuzzy = new Fuzzy();
void setup() {
Serial.begin(115200);
// --- Fuzzy Input: Temperature ---
FuzzySet *low = new FuzzySet(0, 0, 20, 25);
FuzzySet *medium = new FuzzySet(22, 27, 30, 35);
FuzzySet *high = new FuzzySet(32, 38, 100, 100); // up to 100°C now
FuzzyInput *temperature = new FuzzyInput(1);
temperature->addFuzzySet(low);
temperature->addFuzzySet(medium);
temperature->addFuzzySet(high);
fuzzy->addFuzzyInput(temperature);
// --- Fuzzy Output: Speed (0–100%) ---
FuzzySet *slow = new FuzzySet(0, 0, 30, 40);
FuzzySet *moderate = new FuzzySet(35, 50, 65, 75);
FuzzySet *fast = new FuzzySet(70, 85, 100, 100);
FuzzyOutput *motorSpeed = new FuzzyOutput(1);
motorSpeed->addFuzzySet(slow);
motorSpeed->addFuzzySet(moderate);
motorSpeed->addFuzzySet(fast);
fuzzy->addFuzzyOutput(motorSpeed);
// --- Fuzzy Rules ---
FuzzyRuleAntecedent *ifLow = new FuzzyRuleAntecedent();
ifLow->joinSingle(low);
FuzzyRuleConsequent *thenSlow = new FuzzyRuleConsequent();
thenSlow->addOutput(slow);
fuzzy->addFuzzyRule(new FuzzyRule(1, ifLow, thenSlow));
FuzzyRuleAntecedent *ifMed = new FuzzyRuleAntecedent();
ifMed->joinSingle(medium);
FuzzyRuleConsequent *thenModerate = new FuzzyRuleConsequent();
thenModerate->addOutput(moderate);
fuzzy->addFuzzyRule(new FuzzyRule(2, ifMed, thenModerate));
FuzzyRuleAntecedent *ifHigh = new FuzzyRuleAntecedent();
ifHigh->joinSingle(high);
FuzzyRuleConsequent *thenFast = new FuzzyRuleConsequent();
thenFast->addOutput(fast);
fuzzy->addFuzzyRule(new FuzzyRule(3, ifHigh, thenFast));
}
void loop() {
// 🔧 Set your manual temperature here:
float temp = 80.0; // Change this value manually
fuzzy->setInput(1, temp);
fuzzy->fuzzify();
int speedPercent = fuzzy->defuzzify(1); // Output: 0–100%
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" °C → Motor Speed: ");
Serial.print(speedPercent);
Serial.println("%");
delay(3000); // Run every 3 seconds
}