#include <Fuzzy.h>
Fuzzy *fuzzy = new Fuzzy();
const int tempPin = A0;
const int outputPin = 3;
void setup() {
pinMode(tempPin, INPUT);
pinMode(outputPin, OUTPUT);
Serial.begin(9600);
// Define Temperature input fuzzy sets
FuzzyInput *temperature = new FuzzyInput(1);
FuzzySet *dingin = new FuzzySet(0, 18, 18, 20);
temperature->addFuzzySet(dingin);
FuzzySet *hangat = new FuzzySet(18, 25, 25, 30);
temperature->addFuzzySet(hangat);
FuzzySet *panas = new FuzzySet(25, 30, 30, 50);
temperature->addFuzzySet(panas);
fuzzy->addFuzzyInput(temperature);
// Define Fan Speed output fuzzy sets
FuzzyOutput *fanSpeed = new FuzzyOutput(1);
FuzzySet *lambat = new FuzzySet(0, 1, 1, 5);
fanSpeed->addFuzzySet(lambat);
FuzzySet *sedang = new FuzzySet(3, 10, 10, 15);
fanSpeed->addFuzzySet(sedang);
FuzzySet *cepat = new FuzzySet(10, 15, 15, 20);
fanSpeed->addFuzzySet(cepat);
fuzzy->addFuzzyOutput(fanSpeed);
// Define Fuzzy Rules
FuzzyRuleAntecedent *ifTempDingin = new FuzzyRuleAntecedent();
ifTempDingin->joinSingle(dingin);
FuzzyRuleConsequent *thenSpeedLambat = new FuzzyRuleConsequent();
thenSpeedLambat->addOutput(lambat);
FuzzyRule *fuzzyRule01 = new FuzzyRule(1, ifTempDingin, thenSpeedLambat);
fuzzy->addFuzzyRule(fuzzyRule01);
FuzzyRuleAntecedent *ifTempHangat = new FuzzyRuleAntecedent();
ifTempHangat->joinSingle(hangat);
FuzzyRuleConsequent *thenSpeedSedang = new FuzzyRuleConsequent();
thenSpeedSedang->addOutput(sedang);
FuzzyRule *fuzzyRule02 = new FuzzyRule(2, ifTempHangat, thenSpeedSedang);
fuzzy->addFuzzyRule(fuzzyRule02);
FuzzyRuleAntecedent *ifTempPanas = new FuzzyRuleAntecedent();
ifTempPanas->joinSingle(panas);
FuzzyRuleConsequent *thenSpeedCepat = new FuzzyRuleConsequent();
thenSpeedCepat->addOutput(cepat);
FuzzyRule *fuzzyRule03 = new FuzzyRule(3, ifTempPanas, thenSpeedCepat);
fuzzy->addFuzzyRule(fuzzyRule03);
}
void loop() {
int tempInput = analogRead(tempPin);
Serial.println("\n\n\nEntrance: ");
Serial.print("\t\t\tTemperature: ");
Serial.println(tempInput);
fuzzy->setInput(1, tempInput);
fuzzy->fuzzify();
float output = fuzzy->defuzzify(1);
analogWrite(outputPin, output);
Serial.println("Output: ");
Serial.print("\t\t\tFan Speed: ");
Serial.println(output);
delay(2000);
}