#include <Fuzzy.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <Servo.h>
#define ONE_WIRE_BUS 7
#include "DHT.h"
#define DHTPIN 8
#define DHTTYPE DHT22
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
DHT dht(DHTPIN, DHTTYPE);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Servo myservo1;
float t;
float suhu;
float setpoint = 35.0;
float error;
float errorx;
float delerror;
float output1;
Fuzzy *fuzzy = new Fuzzy();
// FuzzyInput
FuzzySet *ernb = new FuzzySet(-1.8, -1.8, -0.8, -0.8);
FuzzySet *erns = new FuzzySet(-1.8, -0.8, -0.8, 0);
FuzzySet *erz = new FuzzySet(-0.8, 0, 0, 0.8);
FuzzySet *erps = new FuzzySet(0, 0.8, 0.8, 1.8);
FuzzySet *erpb = new FuzzySet(0.8, 0.8, 1.8, 1.8);
// FuzzyInput
FuzzySet *dernb = new FuzzySet(-0.18, -0.18, -0.08, -0.08);
FuzzySet *derns = new FuzzySet(-0.18, -0.08, -0.08, 0);
FuzzySet *derz = new FuzzySet(-0.08, 0, 0, 0.08);
FuzzySet *derps = new FuzzySet(0, 0.08, 0.08, 0.18);
FuzzySet *derpb = new FuzzySet(0.08, 0.08, 0.18, 0.18);
// FuzzyOutput
FuzzySet *p = new FuzzySet(4, 5, 5, 6);
FuzzySet *sp = new FuzzySet(19, 20, 20, 21);
FuzzySet *n = new FuzzySet(74, 75, 75, 76);
FuzzySet *sd = new FuzzySet(109, 110, 110, 111);
FuzzySet *d = new FuzzySet(124, 125, 125, 126);
void setup()
{
lcd.begin();
lcd.clear();
lcd.noCursor();
myservo1.attach(9);
myservo1.write(70);
Serial.begin(9600);
dht.begin();
// FuzzyInput error
FuzzyInput *errorsys = new FuzzyInput(1);
errorsys->addFuzzySet(ernb);
errorsys->addFuzzySet(erns);
errorsys->addFuzzySet(erz);
errorsys->addFuzzySet(erps);
errorsys->addFuzzySet(erpb);
fuzzy->addFuzzyInput(errorsys);
// FuzzyInput delta error
FuzzyInput *deltaerror = new FuzzyInput(2);
deltaerror->addFuzzySet(dernb);
deltaerror->addFuzzySet(derns);
deltaerror->addFuzzySet(derz);
deltaerror->addFuzzySet(derps);
deltaerror->addFuzzySet(derpb);
fuzzy->addFuzzyInput(deltaerror);
// FuzzyOutput pwm
FuzzyOutput *pwmout = new FuzzyOutput(1);
pwmout->addFuzzySet(p);
pwmout->addFuzzySet(sp);
pwmout->addFuzzySet(n);
pwmout->addFuzzySet(sd);
pwmout->addFuzzySet(d);
fuzzy->addFuzzyOutput(pwmout);
// Building FuzzyRule 1
FuzzyRuleAntecedent *iferrorsysErnbAnddeltaerrorDernb = new FuzzyRuleAntecedent();
iferrorsysErnbAnddeltaerrorDernb->joinWithAND(ernb, dernb);
FuzzyRuleConsequent *thenPwmoutP= new FuzzyRuleConsequent();
thenPwmoutP->addOutput(p);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, iferrorsysErnbAnddeltaerrorDernb, thenPwmoutP);
fuzzy->addFuzzyRule(fuzzyRule1);
// Building FuzzyRule 2
FuzzyRuleAntecedent *iferrorsysErnbAnddeltaerrorDerns = new FuzzyRuleAntecedent();
iferrorsysErnbAnddeltaerrorDerns->joinWithAND(ernb, derns);
//FuzzyRuleConsequent *thenPwmoutP= new FuzzyRuleConsequent();
thenPwmoutP->addOutput(p);
FuzzyRule *fuzzyRule2 = new FuzzyRule(2, iferrorsysErnbAnddeltaerrorDerns, thenPwmoutP);
fuzzy->addFuzzyRule(fuzzyRule2);
// Building FuzzyRule 3
FuzzyRuleAntecedent *iferrorsysErnbAnddeltaerrorDerz = new FuzzyRuleAntecedent();
iferrorsysErnbAnddeltaerrorDerz->joinWithAND(ernb, derz);
//FuzzyRuleConsequent *thenPwmoutP= new FuzzyRuleConsequent();
thenPwmoutP->addOutput(p);
FuzzyRule *fuzzyRule3 = new FuzzyRule(3, iferrorsysErnbAnddeltaerrorDerz, thenPwmoutP);
fuzzy->addFuzzyRule(fuzzyRule3);
// Building FuzzyRule 4
FuzzyRuleAntecedent *iferrorsysErnbAnddeltaerrorDerps = new FuzzyRuleAntecedent();
iferrorsysErnbAnddeltaerrorDerps->joinWithAND(ernb, derps);
FuzzyRuleConsequent *thenPwmoutSp= new FuzzyRuleConsequent();
thenPwmoutSp->addOutput(sp);
FuzzyRule *fuzzyRule4 = new FuzzyRule(4, iferrorsysErnbAnddeltaerrorDerps, thenPwmoutSp);
fuzzy->addFuzzyRule(fuzzyRule4);
// Building FuzzyRule 5
FuzzyRuleAntecedent *iferrorsysErnbAnddeltaerrorDerpb = new FuzzyRuleAntecedent();
iferrorsysErnbAnddeltaerrorDerpb->joinWithAND(ernb, derpb);
FuzzyRuleConsequent *thenPwmoutN= new FuzzyRuleConsequent();
thenPwmoutN->addOutput(n);
FuzzyRule *fuzzyRule5 = new FuzzyRule(5, iferrorsysErnbAnddeltaerrorDerpb, thenPwmoutN);
fuzzy->addFuzzyRule(fuzzyRule5);
// Building FuzzyRule 6
FuzzyRuleAntecedent *iferrorsysErnsAnddeltaerrorDernb = new FuzzyRuleAntecedent();
iferrorsysErnsAnddeltaerrorDernb->joinWithAND(erns, dernb);
//FuzzyRuleConsequent *thenPwmoutP= new FuzzyRuleConsequent();
thenPwmoutP->addOutput(p);
FuzzyRule *fuzzyRule6 = new FuzzyRule(6, iferrorsysErnsAnddeltaerrorDernb, thenPwmoutP);
fuzzy->addFuzzyRule(fuzzyRule6);
// Building FuzzyRule 7
FuzzyRuleAntecedent *iferrorsysErnsAnddeltaerrorDerns = new FuzzyRuleAntecedent();
iferrorsysErnsAnddeltaerrorDerns->joinWithAND(erns, derns);
//FuzzyRuleConsequent *thenPwmoutP= new FuzzyRuleConsequent();
thenPwmoutP->addOutput(p);
FuzzyRule *fuzzyRule7 = new FuzzyRule(7, iferrorsysErnsAnddeltaerrorDerns, thenPwmoutP);
fuzzy->addFuzzyRule(fuzzyRule7);
// Building FuzzyRule 8
FuzzyRuleAntecedent *iferrorsysErnsAnddeltaerrorDerz = new FuzzyRuleAntecedent();
iferrorsysErnsAnddeltaerrorDerz->joinWithAND(erns, derz);
//FuzzyRuleConsequent *thenPwmoutSp= new FuzzyRuleConsequent();
thenPwmoutSp->addOutput(sp);
FuzzyRule *fuzzyRule8 = new FuzzyRule(8, iferrorsysErnsAnddeltaerrorDerz, thenPwmoutSp);
fuzzy->addFuzzyRule(fuzzyRule8);
// Building FuzzyRule 9
FuzzyRuleAntecedent *iferrorsysErnsAnddeltaerrorDerps = new FuzzyRuleAntecedent();
iferrorsysErnsAnddeltaerrorDerps->joinWithAND(erns, derps);
//FuzzyRuleConsequent *thenPwmoutN= new FuzzyRuleConsequent();
thenPwmoutN->addOutput(n);
FuzzyRule *fuzzyRule9 = new FuzzyRule(9, iferrorsysErnsAnddeltaerrorDerps, thenPwmoutN);
fuzzy->addFuzzyRule(fuzzyRule9);
// Building FuzzyRule 10
FuzzyRuleAntecedent *iferrorsysErnsAnddeltaerrorDerpb = new FuzzyRuleAntecedent();
iferrorsysErnsAnddeltaerrorDerpb->joinWithAND(erns, derpb);
FuzzyRuleConsequent *thenPwmoutSd= new FuzzyRuleConsequent();
thenPwmoutSd->addOutput(sd);
FuzzyRule *fuzzyRule10 = new FuzzyRule(10, iferrorsysErnsAnddeltaerrorDerpb, thenPwmoutSd);
fuzzy->addFuzzyRule(fuzzyRule10);
// Building FuzzyRule 11
FuzzyRuleAntecedent *iferrorsysErzAnddeltaerrorDernb = new FuzzyRuleAntecedent();
iferrorsysErzAnddeltaerrorDernb->joinWithAND(erz, dernb);
//FuzzyRuleConsequent *thenPwmoutP= new FuzzyRuleConsequent();
thenPwmoutP->addOutput(p);
FuzzyRule *fuzzyRule11 = new FuzzyRule(11, iferrorsysErzAnddeltaerrorDernb, thenPwmoutP);
fuzzy->addFuzzyRule(fuzzyRule11);
// Building FuzzyRule 12
FuzzyRuleAntecedent *iferrorsysErzAnddeltaerrorDerns = new FuzzyRuleAntecedent();
iferrorsysErzAnddeltaerrorDerns->joinWithAND(erz, derns);
//FuzzyRuleConsequent *thenPwmoutSp= new FuzzyRuleConsequent();
thenPwmoutSp->addOutput(sp);
FuzzyRule *fuzzyRule12 = new FuzzyRule(12, iferrorsysErzAnddeltaerrorDerns, thenPwmoutSp);
fuzzy->addFuzzyRule(fuzzyRule12);
// Building FuzzyRule 13
FuzzyRuleAntecedent *iferrorsysErzAnddeltaerrorDerz = new FuzzyRuleAntecedent();
iferrorsysErzAnddeltaerrorDerz->joinWithAND(erz, derz);
//FuzzyRuleConsequent *thenPwmoutN= new FuzzyRuleConsequent();
thenPwmoutN->addOutput(n);
FuzzyRule *fuzzyRule13 = new FuzzyRule(13, iferrorsysErzAnddeltaerrorDerz, thenPwmoutSp);
fuzzy->addFuzzyRule(fuzzyRule13);
// Building FuzzyRule 14
FuzzyRuleAntecedent *iferrorsysErzAnddeltaerrorDerps = new FuzzyRuleAntecedent();
iferrorsysErzAnddeltaerrorDerps->joinWithAND(erz, derps);
//FuzzyRuleConsequent *thenPwmoutSD= new FuzzyRuleConsequent();
thenPwmoutSd->addOutput(sd);
FuzzyRule *fuzzyRule14 = new FuzzyRule(14, iferrorsysErzAnddeltaerrorDerps, thenPwmoutSd);
fuzzy->addFuzzyRule(fuzzyRule14);
// Building FuzzyRule 15
FuzzyRuleAntecedent *iferrorsysErzAnddeltaerrorDerpb = new FuzzyRuleAntecedent();
iferrorsysErzAnddeltaerrorDerpb->joinWithAND(erz, derpb);
FuzzyRuleConsequent *thenPwmoutD= new FuzzyRuleConsequent();
thenPwmoutD->addOutput(d);
FuzzyRule *fuzzyRule15 = new FuzzyRule(15, iferrorsysErzAnddeltaerrorDerpb, thenPwmoutD);
fuzzy->addFuzzyRule(fuzzyRule15);
// Building FuzzyRule 16
FuzzyRuleAntecedent *iferrorsysErpsAnddeltaerrorDernb = new FuzzyRuleAntecedent();
iferrorsysErpsAnddeltaerrorDernb->joinWithAND(erps, dernb);
thenPwmoutSp->addOutput(sp);
FuzzyRule *fuzzyRule16 = new FuzzyRule(16, iferrorsysErpsAnddeltaerrorDernb, thenPwmoutSp);
fuzzy->addFuzzyRule(fuzzyRule16);
// Building FuzzyRule 17
FuzzyRuleAntecedent *iferrorsysErpsAnddeltaerrorDerns = new FuzzyRuleAntecedent();
iferrorsysErpsAnddeltaerrorDerns->joinWithAND(erps, derns);
thenPwmoutN->addOutput(n);
FuzzyRule *fuzzyRule17 = new FuzzyRule(17, iferrorsysErpsAnddeltaerrorDerns, thenPwmoutN);
fuzzy->addFuzzyRule(fuzzyRule17);
// Building FuzzyRule 18
FuzzyRuleAntecedent *iferrorsysErpsAnddeltaerrorDerz = new FuzzyRuleAntecedent();
iferrorsysErpsAnddeltaerrorDerz->joinWithAND(erps, derz);
thenPwmoutSd->addOutput(sd);
FuzzyRule *fuzzyRule18 = new FuzzyRule(18, iferrorsysErpsAnddeltaerrorDerz, thenPwmoutSd);
fuzzy->addFuzzyRule(fuzzyRule18);
// Building FuzzyRule 19
FuzzyRuleAntecedent *iferrorsysErpsAnddeltaerrorDerps = new FuzzyRuleAntecedent();
iferrorsysErpsAnddeltaerrorDerps->joinWithAND(erps, derps);
thenPwmoutD->addOutput(d);
FuzzyRule *fuzzyRule19 = new FuzzyRule(19, iferrorsysErpsAnddeltaerrorDerps, thenPwmoutD);
fuzzy->addFuzzyRule(fuzzyRule19);
// Building FuzzyRule 20
FuzzyRuleAntecedent *iferrorsysErpsAnddeltaerrorDerpb = new FuzzyRuleAntecedent();
iferrorsysErpsAnddeltaerrorDerpb->joinWithAND(erps, derpb);
thenPwmoutD->addOutput(d);
FuzzyRule *fuzzyRule20 = new FuzzyRule(20, iferrorsysErzAnddeltaerrorDerpb, thenPwmoutD);
fuzzy->addFuzzyRule(fuzzyRule20);
// Building FuzzyRule 21
FuzzyRuleAntecedent *iferrorsysErpbAnddeltaerrorDernb = new FuzzyRuleAntecedent();
iferrorsysErpbAnddeltaerrorDernb->joinWithAND(erpb, dernb);
thenPwmoutN->addOutput(n);
FuzzyRule *fuzzyRule21 = new FuzzyRule(21, iferrorsysErpbAnddeltaerrorDernb, thenPwmoutN);
fuzzy->addFuzzyRule(fuzzyRule21);
// Building FuzzyRule 22
FuzzyRuleAntecedent *iferrorsysErpbAnddeltaerrorDerns = new FuzzyRuleAntecedent();
iferrorsysErpbAnddeltaerrorDerns->joinWithAND(erpb, derns);
thenPwmoutSd->addOutput(sd);
FuzzyRule *fuzzyRule22= new FuzzyRule(22, iferrorsysErpbAnddeltaerrorDerns, thenPwmoutSd);
fuzzy->addFuzzyRule(fuzzyRule22);
// Building FuzzyRule 23
FuzzyRuleAntecedent *iferrorsysErpbAnddeltaerrorDerz = new FuzzyRuleAntecedent();
iferrorsysErpbAnddeltaerrorDerz->joinWithAND(erpb, derz);
thenPwmoutD->addOutput(d);
FuzzyRule *fuzzyRule23 = new FuzzyRule(23, iferrorsysErpbAnddeltaerrorDerz, thenPwmoutD);
fuzzy->addFuzzyRule(fuzzyRule23);
// Building FuzzyRule 24
FuzzyRuleAntecedent *iferrorsysErpbAnddeltaerrorDerps = new FuzzyRuleAntecedent();
iferrorsysErpbAnddeltaerrorDerps->joinWithAND(erpb, derps);
thenPwmoutD->addOutput(d);
FuzzyRule *fuzzyRule24 = new FuzzyRule(24, iferrorsysErpbAnddeltaerrorDerps, thenPwmoutD);
fuzzy->addFuzzyRule(fuzzyRule24);
// Building FuzzyRule 25
FuzzyRuleAntecedent *iferrorsysErpbAnddeltaerrorDerpb = new FuzzyRuleAntecedent();
iferrorsysErpbAnddeltaerrorDerpb->joinWithAND(erpb, derpb);
thenPwmoutD->addOutput(d);
FuzzyRule *fuzzyRule25 = new FuzzyRule(25, iferrorsysErzAnddeltaerrorDerpb, thenPwmoutD);
fuzzy->addFuzzyRule(fuzzyRule25);
}
void loop()
{
t = dht.readTemperature();
sensors.requestTemperatures();
suhu = sensors.getTempCByIndex(0);
error = setpoint - t;
delerror = error - errorx;
lcd.setCursor(0,0);
lcd.print("D/S= ");
lcd.print(t,1);
lcd.print("/");
lcd.print(suhu,1);
lcd.print(" ");
// Serial.print("Suhu DHT22: ");
// Serial.print(t);
// Serial.print("|| Suhu DS: ");
// Serial.println(suhu);
fuzzy->setInput(1, error);
fuzzy->setInput(2, delerror);
fuzzy->fuzzify();
/*
Serial.print("errorsys: NB-> ");
Serial.print(ernb->getPertinence());
Serial.print(", NS-> ");
Serial.print(erns->getPertinence());
Serial.print(", Z-> ");
Serial.print(erz->getPertinence());
Serial.print(", PS-> ");
Serial.print(erps->getPertinence());
Serial.print(", PB-> ");
Serial.println(erpb->getPertinence());
Serial.print("DELTA error: NB-> ");
Serial.print(dernb->getPertinence());
Serial.print(", NS-> ");
Serial.print(derns->getPertinence());
Serial.print(", Z-> ");
Serial.print(derz->getPertinence());
Serial.print(", PS-> ");
Serial.print(derps->getPertinence());
Serial.print(", PB-> ");
Serial.println(derpb->getPertinence());
*/
output1 = fuzzy->defuzzify(1);
/*
Serial.print("Output: ");
Serial.print("PWMOUT: P-> ");
Serial.print(p->getPertinence());
Serial.print(", SP-> ");
Serial.print(sp->getPertinence());
Serial.print(", N-> ");
Serial.print(n->getPertinence());
Serial.print(", SD-> ");
Serial.print(sd->getPertinence());
Serial.print(", D-> ");
Serial.println(d->getPertinence());
Serial.print("OUTPUT: ");
Serial.println(output1);
*/
lcd.setCursor(0,1);
lcd.print("OUT: ");
lcd.print(output1);
lcd.print(" ");
if(output1 == 0){
lcd.setCursor(13,1);
lcd.print("ON ");
myservo1.write(150);
}
if((output1 > 40)&&(output1 < 200)){
lcd.setCursor(13,1);
lcd.print("ON ");
myservo1.write(150);
}
if((output1 > 10)&&(output1 < 40)){
lcd.setCursor(13,1);
lcd.print("OFF");
myservo1.write(70);
}
errorx = error;
delay(1000);
kirimdata();
}
void kirimdata(){
Serial.print("*");
Serial.print(t * 100.0);
Serial.print(",");
Serial.print(suhu * 100.0);
Serial.print(",");
Serial.print(error * 100.0);
Serial.print(",");
Serial.print(delerror * 100.0);
Serial.println("#");
}