#include <LiquidCrystal_I2C.h>
#include <Fuzzy.h>
#define LCD_I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(LCD_I2C_ADDR, LCD_COLUMNS, LCD_LINES);
int iTempature = 0;
int iLight = 0;
int iMoisture = 0;
float dTempature = 0;
float dLight = 0;
float dMoisture = 0;
float sense = 0;
// Fuzzy
Fuzzy *fuzzy = new Fuzzy();
//FuzzyInput Temperature membership functions
FuzzySet *bad_t = new FuzzySet(0,0,0,5);
FuzzySet *avg_t = new FuzzySet(0,5,5,10);
FuzzySet *good_t = new FuzzySet(5,10,10,10);
//FuzzyInput Light membership functions
FuzzySet *bad_l = new FuzzySet(0,0,0,5);
FuzzySet *avg_l = new FuzzySet(0,5,5,10);
FuzzySet *good_l = new FuzzySet(5,10,10,10);
//FuzzyInput Moisture membership functions
FuzzySet *bad_m = new FuzzySet(0,0,0,5);
FuzzySet *avg_m = new FuzzySet(0,5,5,10);
FuzzySet *good_m = new FuzzySet(5,10,10,10);
//FuzzyInput sense membership functions
FuzzySet *bad_s = new FuzzySet(0,0,0,5);
FuzzySet *avg_s = new FuzzySet(0,5,5,10);
FuzzySet *good_s = new FuzzySet(5,10,10,10);
void setup() {
Serial.begin(115200);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
//Liguistic variable Tempature
FuzzyInput *tempature = new FuzzyInput(1);
tempature->addFuzzySet(bad_t);
tempature->addFuzzySet(avg_t);
tempature->addFuzzySet(good_t);
fuzzy->addFuzzyInput(tempature);
//Liguistic variable Light
FuzzyInput *light = new FuzzyInput(2);
light->addFuzzySet(bad_l);
light->addFuzzySet(avg_l);
light->addFuzzySet(good_l);
fuzzy->addFuzzyInput(light);
//Liguistic variable Moisture
FuzzyInput *moisture = new FuzzyInput(3);
moisture->addFuzzySet(bad_m);
moisture->addFuzzySet(avg_m);
moisture->addFuzzySet(good_m);
fuzzy->addFuzzyInput(moisture);
//Liguistic variable Sense
FuzzyOutput *sense = new FuzzyOutput(1);
sense->addFuzzySet(bad_s);
sense->addFuzzySet(avg_s);
sense->addFuzzySet(good_s);
fuzzy->addFuzzyOutput(sense);
// Need fixing :(
//Building a fuzzy rules
//Bad Sense
FuzzyRuleAntecedent *ifTemaptureBadAndLightBadAndMoistureBad = new FuzzyRuleAntecedent();
ifTemaptureBadAndLightBadAndMoistureBad->joinWithAND(bad_t,bad_l,bad_m);
FuzzyRuleConsequent *thenSenseBad = new FuzzyRuleConsequent();
thenSenseBad->addOutput(bad_s);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1,ifTemaptureBadAndLightBadAndMoistureBad,thenSenseBad);
fuzzy->addFuzzyRule(fuzzyRule1);
//Avg Sense
FuzzyRuleAntecedent *ifserviceAvgAndFoodAvg = new FuzzyRuleAntecedent();
ifserviceAvgAndFoodAvg->joinWithAND(avg_s,avg_f);
FuzzyRuleConsequent *thenTipsAvg = new FuzzyRuleConsequent();
thenTipsAvg->addOutput(avg_t);
FuzzyRule *fuzzyRule2 = new FuzzyRule(2,ifserviceAvgAndFoodAvg,thenTipsAvg);
fuzzy->addFuzzyRule(fuzzyRule2);
//Good Sense
FuzzyRuleAntecedent *ifserviceGoodAndFoodGood = new FuzzyRuleAntecedent();
ifserviceGoodAndFoodGood->joinWithAND(good_s,good_f);
FuzzyRuleConsequent *thenTipsGood = new FuzzyRuleConsequent();
thenTipsGood->addOutput(good_t);
FuzzyRule *fuzzyRule3 = new FuzzyRule(3,ifserviceGoodAndFoodGood,thenTipsGood);
fuzzy->addFuzzyRule(fuzzyRule3);
}
void loop() {
// put your main code here, to run repeatedly:
iService = analogRead(A0);
iFood = analogRead(A1);
dService = (float)iService/102.3;
dFood = (float)iFood/102.3;
//Fuzzy usage
fuzzy->setInput(1,dService);
fuzzy->setInput(2,dFood);
fuzzy->fuzzify();
tips = fuzzy->defuzzify(1);
Serial.print("Service: ");
Serial.print(String(dService,2));
Serial.print("Food: ");
Serial.print(String(dFood,2));
Serial.print("Tips: ");
Serial.println(String(tips,2));
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("S: " + String(dService,2) + " F: " + String(dFood, 2));
lcd.setCursor(0,1);
lcd.print ("Tips: " + String(tips,2));
delay(1000);
}