Code Monkey home page Code Monkey logo

Comments (13)

alvesoaj avatar alvesoaj commented on May 23, 2024

I take a quick look in the code, It seems ok. Please, print the 't' input too, to see if it is changing along the time.

from efll.

ryanescorial avatar ryanescorial commented on May 23, 2024

Thank you for your help. I'm testing now with 3 inputs and 1 output. By the way, is there any plot or graph available on this lib? Thanks.

from efll.

alvesoaj avatar alvesoaj commented on May 23, 2024

No, there are no plot or graph available, the aim of this library is to be very simple to run on micro-systems. To do that, just print the data in the terminal and format it using pyplot or other tool.

from efll.

ryanescorial avatar ryanescorial commented on May 23, 2024

Hi Sir,

Thank you so much for your assistance and information. But I now stumble with my code.
It works fine with random inputs but when I tested it with actual sensors, I failed to receive the output.

#include <DHT.h>
#include <DHT_U.h>
#include <Fuzzy.h>

#define DHTPIN 2 //d2 pin
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

float sm;
float humid;
float temp;
int sensorPin = A0;

// Fuzzy
Fuzzy *fuzzy = new Fuzzy();

// FuzzyInput - Soil Moisture

FuzzySet *dry = new FuzzySet(-87, -60, -60, -34);
FuzzySet *normal = new FuzzySet(-44, -17, -17, 9);
FuzzySet *adeWet = new FuzzySet(0, 26, 26, 52);
FuzzySet *saturated = new FuzzySet(44, 66, 66, 87);

// FuzzyInput - Humidity
FuzzySet *low = new FuzzySet(0, 15, 15, 30);
FuzzySet *medium = new FuzzySet(25, 40, 40, 55);
FuzzySet *high = new FuzzySet(50, 65, 65, 80);
FuzzySet *exhigh = new FuzzySet(75, 88, 88, 100);

// FuzzyInput - Temperature
FuzzySet *cold = new FuzzySet(-30, -5, -5, 20);
FuzzySet *normal1 = new FuzzySet(10, 30, 30, 50);
FuzzySet *hot = new FuzzySet(40, 60, 60, 80);
FuzzySet *vhot = new FuzzySet(70, 95, 95, 120);

// FuzzyOutput - Pump
FuzzySet *ten = new FuzzySet(0, 10, 10, 20);
FuzzySet *tfive = new FuzzySet(15, 30, 30, 45);
FuzzySet *fifty = new FuzzySet(40, 55, 55, 70);
FuzzySet *sfive = new FuzzySet(65, 75, 75, 85);
FuzzySet *full = new FuzzySet(80, 90, 90, 100);

void setup()
{
// Set the Serial output
Serial.begin(9600);
// Set a random seed
randomSeed(analogRead(0));
Serial.print("Data from Sensors \n");
pinMode(DHTPIN, INPUT);//DHT sensor
dht.begin();

// FuzzyInput
FuzzyInput *soilM = new FuzzyInput(1);

soilM->addFuzzySet(dry);
soilM->addFuzzySet(normal);
soilM->addFuzzySet(adeWet);
soilM->addFuzzySet(saturated);
fuzzy->addFuzzyInput(soilM);

// FuzzyInput
FuzzyInput *humid = new FuzzyInput(2);

humid->addFuzzySet(low);
humid->addFuzzySet(medium);
humid->addFuzzySet(high);
humid->addFuzzySet(exhigh);
fuzzy->addFuzzyInput(humid);

// FuzzyInput
FuzzyInput *temp = new FuzzyInput(3);

temp->addFuzzySet(cold);
temp->addFuzzySet(normal1);
temp->addFuzzySet(hot);
temp->addFuzzySet(vhot);
fuzzy->addFuzzyInput(temp);

// FuzzyOutput
FuzzyOutput *valve = new FuzzyOutput(1);

valve->addFuzzySet(ten);
valve->addFuzzySet(tfive);
valve->addFuzzySet(fifty);
valve->addFuzzySet(sfive);
valve->addFuzzySet(full);
fuzzy->addFuzzyOutput(valve);

// Building FuzzyRule - 1
FuzzyRuleAntecedent *hlowAndtvhot = new FuzzyRuleAntecedent();
hlowAndtvhot->joinWithAND(low, vhot);
FuzzyRuleAntecedent *smdry1 = new FuzzyRuleAntecedent();
smdry1->joinSingle(dry);
FuzzyRuleAntecedent *ifsmdryAndhlowAndvhot = new FuzzyRuleAntecedent();
ifsmdryAndhlowAndvhot->joinWithAND(hlowAndtvhot, smdry1);
FuzzyRuleConsequent *thenValveFull = new FuzzyRuleConsequent();
thenValveFull->addOutput(full);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifsmdryAndhlowAndvhot, thenValveFull);
fuzzy->addFuzzyRule(fuzzyRule1);

// Building FuzzyRule - 2
FuzzyRuleAntecedent *hlowAndthot = new FuzzyRuleAntecedent();
hlowAndthot->joinWithAND(low, hot);
FuzzyRuleAntecedent *ifsmdryAndhlowAndthot = new FuzzyRuleAntecedent();
ifsmdryAndhlowAndthot->joinWithAND(hlowAndthot, smdry1);
FuzzyRuleConsequent *thenValveSfive2 = new FuzzyRuleConsequent();
thenValveSfive2->addOutput(sfive);
FuzzyRule *fuzzyRule2 = new FuzzyRule(1, ifsmdryAndhlowAndthot, thenValveSfive2);
fuzzy->addFuzzyRule(fuzzyRule2);

// Building FuzzyRule - 11

FuzzyRuleAntecedent *smsaturated11 = new FuzzyRuleAntecedent();
smsaturated11->joinSingle(saturated);
FuzzyRuleAntecedent *ifsmsaturated11AndhlowAndthot11 = new FuzzyRuleAntecedent();
ifsmsaturated11AndhlowAndthot11->joinWithAND(hlowAndthot, smsaturated11);
FuzzyRuleConsequent *thenValveTfive11 = new FuzzyRuleConsequent();
thenValveTfive11->addOutput(tfive);
FuzzyRule *fuzzyRule11 = new FuzzyRule(1, ifsmsaturated11AndhlowAndthot11, thenValveTfive11);
fuzzy->addFuzzyRule(fuzzyRule11);

// Building FuzzyRule - 8
FuzzyRuleAntecedent *smadeWet8 = new FuzzyRuleAntecedent();
smadeWet8->joinSingle(adeWet);
FuzzyRuleAntecedent *ifsmadeWetAndhlowAndthot8 = new FuzzyRuleAntecedent();
ifsmadeWetAndhlowAndthot8->joinWithAND(hlowAndthot, smadeWet8);
FuzzyRuleConsequent *thenValveTfive8 = new FuzzyRuleConsequent();
thenValveTfive8->addOutput(tfive);
FuzzyRule *fuzzyRule8 = new FuzzyRule(1, ifsmadeWetAndhlowAndthot8, thenValveTfive8);
fuzzy->addFuzzyRule(fuzzyRule8);

// Building FuzzyRule -3
FuzzyRuleAntecedent *hlowAndtnormal = new FuzzyRuleAntecedent();
hlowAndtnormal->joinWithAND(low, normal1);
FuzzyRuleAntecedent *ifsmdryAndhlowAndtnormal = new FuzzyRuleAntecedent();
ifsmdryAndhlowAndtnormal->joinWithAND(hlowAndtnormal, smdry1);
FuzzyRuleConsequent *thenValveSfive3 = new FuzzyRuleConsequent();
thenValveSfive3->addOutput(sfive);
FuzzyRule *fuzzyRule3 = new FuzzyRule(1, ifsmdryAndhlowAndtnormal, thenValveSfive3);
fuzzy->addFuzzyRule(fuzzyRule3);

// Building FuzzyRule -4
FuzzyRuleAntecedent *hlowAndtcold = new FuzzyRuleAntecedent();
hlowAndtcold->joinWithAND(low, cold);
FuzzyRuleAntecedent *ifsmdryAndhlowAndtcold = new FuzzyRuleAntecedent();
ifsmdryAndhlowAndtcold->joinWithAND(hlowAndtcold, smdry1);
FuzzyRuleConsequent *thenValveFifty = new FuzzyRuleConsequent();
thenValveFifty->addOutput(fifty);
FuzzyRule *fuzzyRule4 = new FuzzyRule(1, ifsmdryAndhlowAndtcold, thenValveFifty);
fuzzy->addFuzzyRule(fuzzyRule4);

// Building FuzzyRule -13
FuzzyRuleAntecedent *smsaturated13 = new FuzzyRuleAntecedent();
smsaturated13->joinSingle(saturated);
FuzzyRuleAntecedent *ifsmsaturated13AndhlowAndtcold13 = new FuzzyRuleAntecedent();
ifsmsaturated13AndhlowAndtcold13->joinWithAND(hlowAndtcold, smsaturated13);
FuzzyRuleConsequent *thenValveTen13 = new FuzzyRuleConsequent();
thenValveTen13->addOutput(ten);
FuzzyRule *fuzzyRule13 = new FuzzyRule(1, ifsmsaturated13AndhlowAndtcold13, thenValveTen13);
fuzzy->addFuzzyRule(fuzzyRule13);

// Building FuzzyRule -5
FuzzyRuleAntecedent *hmediumAndtcold = new FuzzyRuleAntecedent();
hmediumAndtcold->joinWithAND(medium, cold);
FuzzyRuleAntecedent *smnormal = new FuzzyRuleAntecedent();
smnormal->joinSingle(normal);
FuzzyRuleAntecedent *ifsmnormalAndhmediumAndtcold = new FuzzyRuleAntecedent();
ifsmnormalAndhmediumAndtcold->joinWithAND(hmediumAndtcold, smnormal);
FuzzyRuleConsequent *thenValveSfive5 = new FuzzyRuleConsequent();
thenValveSfive5->addOutput(sfive);
FuzzyRule *fuzzyRule5 = new FuzzyRule(1, ifsmnormalAndhmediumAndtcold, thenValveSfive5);
fuzzy->addFuzzyRule(fuzzyRule5);

// Building FuzzyRule -6
FuzzyRuleAntecedent *hhighAndtnormal6 = new FuzzyRuleAntecedent();
hhighAndtnormal6->joinWithAND(high, normal1);
FuzzyRuleAntecedent *ifsmnormalAndhhighAndtnormal6 = new FuzzyRuleAntecedent();
ifsmnormalAndhhighAndtnormal6->joinWithAND(hhighAndtnormal6, smnormal);
FuzzyRuleConsequent *thenValveFifty6 = new FuzzyRuleConsequent();
thenValveFifty6->addOutput(fifty);
FuzzyRule *fuzzyRule6 = new FuzzyRule(1, ifsmnormalAndhhighAndtnormal6, thenValveFifty6);
fuzzy->addFuzzyRule(fuzzyRule6);

// Building FuzzyRule -7
FuzzyRuleAntecedent *hexhighAndthot7 = new FuzzyRuleAntecedent();
hexhighAndthot7->joinWithAND(exhigh, hot);
FuzzyRuleAntecedent *ifsmnormalAndhexhighAndthot7 = new FuzzyRuleAntecedent();
ifsmnormalAndhexhighAndthot7->joinWithAND(hexhighAndthot7, smnormal);
FuzzyRuleConsequent *thenValveSfive7 = new FuzzyRuleConsequent();
thenValveSfive7->addOutput(sfive);
FuzzyRule *fuzzyRule7 = new FuzzyRule(1, ifsmnormalAndhexhighAndthot7, thenValveSfive7);
fuzzy->addFuzzyRule(fuzzyRule7);

// Building FuzzyRule -17
FuzzyRuleAntecedent *ifsmsaturated17AndhexhighAndthot17 = new FuzzyRuleAntecedent();
ifsmsaturated17AndhexhighAndthot17->joinWithAND(hexhighAndthot7, smsaturated11);
FuzzyRuleConsequent *thenValveTfive17 = new FuzzyRuleConsequent();
thenValveTfive17->addOutput(tfive);
FuzzyRule *fuzzyRule17 = new FuzzyRule(1, ifsmsaturated17AndhexhighAndthot17, thenValveTfive17);
fuzzy->addFuzzyRule(fuzzyRule17);

// Building FuzzyRule -9
FuzzyRuleAntecedent *hmediumAndthot9 = new FuzzyRuleAntecedent();
hmediumAndthot9->joinWithAND(medium, hot);
FuzzyRuleAntecedent *ifsmadeWetAndhmediumAndthot9 = new FuzzyRuleAntecedent();
ifsmadeWetAndhmediumAndthot9->joinWithAND(hmediumAndthot9, smadeWet8);
FuzzyRuleConsequent *thenValveTfive9 = new FuzzyRuleConsequent();
thenValveTfive9->addOutput(tfive);
FuzzyRule *fuzzyRule9 = new FuzzyRule(1, ifsmadeWetAndhmediumAndthot9, thenValveTfive9);
fuzzy->addFuzzyRule(fuzzyRule9);

// Building FuzzyRule -12

FuzzyRuleAntecedent *ifsmsaturated12AndhmediumAndthot12 = new FuzzyRuleAntecedent();
ifsmsaturated12AndhmediumAndthot12->joinWithAND(hmediumAndthot9, smsaturated11);
FuzzyRuleConsequent *thenValveTfive12 = new FuzzyRuleConsequent();
thenValveTfive12->addOutput(tfive);
FuzzyRule *fuzzyRule12 = new FuzzyRule(1, ifsmsaturated12AndhmediumAndthot12, thenValveTfive12);
fuzzy->addFuzzyRule(fuzzyRule12);

// Building FuzzyRule -10
FuzzyRuleAntecedent *hhighAndthot10 = new FuzzyRuleAntecedent();
hhighAndthot10->joinWithAND(high, hot);
FuzzyRuleAntecedent *ifsmadeWetAndhhighAndthot10 = new FuzzyRuleAntecedent();
ifsmadeWetAndhhighAndthot10->joinWithAND(hhighAndthot10, smadeWet8);
FuzzyRuleConsequent *thenValveTfive10 = new FuzzyRuleConsequent();
thenValveTfive10->addOutput(tfive);
FuzzyRule *fuzzyRule10 = new FuzzyRule(1, ifsmadeWetAndhhighAndthot10, thenValveTfive10);
fuzzy->addFuzzyRule(fuzzyRule10);

// Building FuzzyRule -14
FuzzyRuleAntecedent *hexhighAndtcold14 = new FuzzyRuleAntecedent();
hexhighAndtcold14->joinWithAND(exhigh, cold);
FuzzyRuleAntecedent *ifsmsaturated14AndhexhighAndtcold14 = new FuzzyRuleAntecedent();
ifsmsaturated14AndhexhighAndtcold14->joinWithAND(hexhighAndtcold14, smsaturated11);
FuzzyRuleConsequent *thenValveTen14 = new FuzzyRuleConsequent();
thenValveTen14->addOutput(ten);
FuzzyRule *fuzzyRule14 = new FuzzyRule(1, ifsmsaturated14AndhexhighAndtcold14, thenValveTen14);
fuzzy->addFuzzyRule(fuzzyRule14);

// Building FuzzyRule -16

FuzzyRuleAntecedent *ifsmsaturated16AndhexhighAndtcold16 = new FuzzyRuleAntecedent();
ifsmsaturated16AndhexhighAndtcold16->joinWithAND(hexhighAndtcold14, smsaturated11);
FuzzyRuleConsequent *thenValveTen16 = new FuzzyRuleConsequent();
thenValveTen16->addOutput(ten);
FuzzyRule *fuzzyRule16 = new FuzzyRule(1, ifsmsaturated16AndhexhighAndtcold16, thenValveTen16);
fuzzy->addFuzzyRule(fuzzyRule16);

// Building FuzzyRule -15
FuzzyRuleAntecedent *hhighAndtcold15 = new FuzzyRuleAntecedent();
hhighAndtcold15->joinWithAND(high, cold);
FuzzyRuleAntecedent *ifsmadeWet15AndhhighAndtcold15 = new FuzzyRuleAntecedent();
ifsmadeWet15AndhhighAndtcold15->joinWithAND(hhighAndtcold15, smadeWet8);
FuzzyRuleConsequent *thenValveTen15 = new FuzzyRuleConsequent();
thenValveTen15->addOutput(ten);
FuzzyRule *fuzzyRule15 = new FuzzyRule(1, ifsmadeWet15AndhhighAndtcold15, thenValveTen15);
fuzzy->addFuzzyRule(fuzzyRule15);

}

void loop()
{
// get random input
// sm = random(-87, 87);
// humid = random(0, 100);
// temp = random(-30, 120);

sm = analogRead(sensorPin);
sm = map(sm,550,10,0,100);
humid = dht.readHumidity();
temp = dht.readTemperature();

Serial.print("Soil Moisture: ");
Serial.print(sm);
Serial.print("%");
Serial.print(", Humidity: ");
Serial.print(humid);
Serial.print("%");
Serial.print(", and Temperature: ");
Serial.println(temp);
Serial.print("C");

fuzzy->setInput(1, sm);
fuzzy->setInput(2, humid);
fuzzy->setInput(3, temp);

fuzzy->fuzzify();
float output1 = fuzzy->defuzzify(1);

Serial.print("\t\t\t\nPumping: ");
Serial.print(output1);
Serial.print("\n");

delay(10000);

}

output problems

from efll.

alvesoaj avatar alvesoaj commented on May 23, 2024

from efll.

ryanescorial avatar ryanescorial commented on May 23, 2024

Yes, the output in random inputs are correct but when it comes to actual sensors, it always gives zero percent result. BTW, I'm using Arduino Uno.

from efll.

alvesoaj avatar alvesoaj commented on May 23, 2024

Hello sorry for late reply, I am in vacations. It is very very strange, I wish to test your code and I will do it as soon as possible, I am going back to my home in the next week. I have some temperature and moisture sensors to reproduce your problem.

from efll.

ryanescorial avatar ryanescorial commented on May 23, 2024

Hi, I am also wondering about it. I can't trace the real problem if it is in the circuits or codes. I will wait for your feedback about your testing. Thank you so much for taking time in my problem.

from efll.

alvesoaj avatar alvesoaj commented on May 23, 2024

from efll.

vinayakfaraday avatar vinayakfaraday commented on May 23, 2024

can we provide digital data instead of analog data from sensors. For example you can take BH1750 light sensor which gives digital data. Will the library work?

from efll.

alvesoaj avatar alvesoaj commented on May 23, 2024

Yes, it should work!

from efll.

ryanescorial avatar ryanescorial commented on May 23, 2024

Hi, do you have any update about your testing?

from efll.

alvesoaj avatar alvesoaj commented on May 23, 2024

Hello @ryanescorial, sorry for a very late response, after my vacation i found looots of work.

I tested your code, I found the "question". There are a lack of FuzzyRules to cover all possibilities, I modified your code to check which FuzzeRules is fired in each interaction, take a look! I made small modifications in your code (The sensor version and I included a unique identifier to each FuzzyRule)

#include <DHT.h>
#include <DHT_U.h>
#include <Fuzzy.h>

#define DHTPIN 2      //d2 pin
#define DHTTYPE DHT11 // it was DHT22

DHT dht(DHTPIN, DHTTYPE);

float sm;
float humid;
float temp;
int sensorPin = A0;

// Fuzzy
Fuzzy *fuzzy = new Fuzzy();

// FuzzyInput - Soil Moisture
FuzzySet *dry = new FuzzySet(-87, -60, -60, -34);
FuzzySet *normal = new FuzzySet(-44, -17, -17, 9);
FuzzySet *adeWet = new FuzzySet(0, 26, 26, 52);
FuzzySet *saturated = new FuzzySet(44, 66, 66, 87);

// FuzzyInput - Humidity
FuzzySet *low = new FuzzySet(0, 15, 15, 30);
FuzzySet *medium = new FuzzySet(25, 40, 40, 55);
FuzzySet *high = new FuzzySet(50, 65, 65, 80);
FuzzySet *exhigh = new FuzzySet(75, 88, 88, 100);

// FuzzyInput - Temperature
FuzzySet *cold = new FuzzySet(-30, -5, -5, 20);
FuzzySet *normal1 = new FuzzySet(10, 30, 30, 50);
FuzzySet *hot = new FuzzySet(40, 60, 60, 80);
FuzzySet *vhot = new FuzzySet(70, 95, 95, 120);

// FuzzyOutput - Pump
FuzzySet *ten = new FuzzySet(0, 10, 10, 20);
FuzzySet *tfive = new FuzzySet(15, 30, 30, 45);
FuzzySet *fifty = new FuzzySet(40, 55, 55, 70);
FuzzySet *sfive = new FuzzySet(65, 75, 75, 85);
FuzzySet *full = new FuzzySet(80, 90, 90, 100);

void setup()
{
  // Set the Serial output
  Serial.begin(9600);
  // Set a random seed
  randomSeed(analogRead(0));
  Serial.print("Data from Sensors \n");
  pinMode(DHTPIN, INPUT); //DHT sensor
  dht.begin();

  // FuzzyInput
  FuzzyInput *soilM = new FuzzyInput(1);

  soilM->addFuzzySet(dry);
  soilM->addFuzzySet(normal);
  soilM->addFuzzySet(adeWet);
  soilM->addFuzzySet(saturated);
  fuzzy->addFuzzyInput(soilM);

  // FuzzyInput
  FuzzyInput *humid = new FuzzyInput(2);

  humid->addFuzzySet(low);
  humid->addFuzzySet(medium);
  humid->addFuzzySet(high);
  humid->addFuzzySet(exhigh);
  fuzzy->addFuzzyInput(humid);

  // FuzzyInput
  FuzzyInput *temp = new FuzzyInput(3);

  temp->addFuzzySet(cold);
  temp->addFuzzySet(normal1);
  temp->addFuzzySet(hot);
  temp->addFuzzySet(vhot);
  fuzzy->addFuzzyInput(temp);

  // FuzzyOutput
  FuzzyOutput *valve = new FuzzyOutput(1);

  valve->addFuzzySet(ten);
  valve->addFuzzySet(tfive);
  valve->addFuzzySet(fifty);
  valve->addFuzzySet(sfive);
  valve->addFuzzySet(full);
  fuzzy->addFuzzyOutput(valve);

  // Building FuzzyRule - 1
  FuzzyRuleAntecedent *hlowAndtvhot = new FuzzyRuleAntecedent();
  hlowAndtvhot->joinWithAND(low, vhot);
  FuzzyRuleAntecedent *smdry1 = new FuzzyRuleAntecedent();
  smdry1->joinSingle(dry);
  FuzzyRuleAntecedent *ifsmdryAndhlowAndvhot = new FuzzyRuleAntecedent();
  ifsmdryAndhlowAndvhot->joinWithAND(hlowAndtvhot, smdry1);
  FuzzyRuleConsequent *thenValveFull = new FuzzyRuleConsequent();
  thenValveFull->addOutput(full);
  FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifsmdryAndhlowAndvhot, thenValveFull);
  fuzzy->addFuzzyRule(fuzzyRule1);

  // Building FuzzyRule - 2
  FuzzyRuleAntecedent *hlowAndthot = new FuzzyRuleAntecedent();
  hlowAndthot->joinWithAND(low, hot);
  FuzzyRuleAntecedent *ifsmdryAndhlowAndthot = new FuzzyRuleAntecedent();
  ifsmdryAndhlowAndthot->joinWithAND(hlowAndthot, smdry1);
  FuzzyRuleConsequent *thenValveSfive2 = new FuzzyRuleConsequent();
  thenValveSfive2->addOutput(sfive);
  FuzzyRule *fuzzyRule2 = new FuzzyRule(2, ifsmdryAndhlowAndthot, thenValveSfive2);
  fuzzy->addFuzzyRule(fuzzyRule2);

  // Building FuzzyRule - 11
  FuzzyRuleAntecedent *smsaturated11 = new FuzzyRuleAntecedent();
  smsaturated11->joinSingle(saturated);
  FuzzyRuleAntecedent *ifsmsaturated11AndhlowAndthot11 = new FuzzyRuleAntecedent();
  ifsmsaturated11AndhlowAndthot11->joinWithAND(hlowAndthot, smsaturated11);
  FuzzyRuleConsequent *thenValveTfive11 = new FuzzyRuleConsequent();
  thenValveTfive11->addOutput(tfive);
  FuzzyRule *fuzzyRule11 = new FuzzyRule(11, ifsmsaturated11AndhlowAndthot11, thenValveTfive11);
  fuzzy->addFuzzyRule(fuzzyRule11);

  // Building FuzzyRule - 8
  FuzzyRuleAntecedent *smadeWet8 = new FuzzyRuleAntecedent();
  smadeWet8->joinSingle(adeWet);
  FuzzyRuleAntecedent *ifsmadeWetAndhlowAndthot8 = new FuzzyRuleAntecedent();
  ifsmadeWetAndhlowAndthot8->joinWithAND(hlowAndthot, smadeWet8);
  FuzzyRuleConsequent *thenValveTfive8 = new FuzzyRuleConsequent();
  thenValveTfive8->addOutput(tfive);
  FuzzyRule *fuzzyRule8 = new FuzzyRule(8, ifsmadeWetAndhlowAndthot8, thenValveTfive8);
  fuzzy->addFuzzyRule(fuzzyRule8);

  // Building FuzzyRule -3
  FuzzyRuleAntecedent *hlowAndtnormal = new FuzzyRuleAntecedent();
  hlowAndtnormal->joinWithAND(low, normal1);
  FuzzyRuleAntecedent *ifsmdryAndhlowAndtnormal = new FuzzyRuleAntecedent();
  ifsmdryAndhlowAndtnormal->joinWithAND(hlowAndtnormal, smdry1);
  FuzzyRuleConsequent *thenValveSfive3 = new FuzzyRuleConsequent();
  thenValveSfive3->addOutput(sfive);
  FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifsmdryAndhlowAndtnormal, thenValveSfive3);
  fuzzy->addFuzzyRule(fuzzyRule3);

  // Building FuzzyRule -4
  FuzzyRuleAntecedent *hlowAndtcold = new FuzzyRuleAntecedent();
  hlowAndtcold->joinWithAND(low, cold);
  FuzzyRuleAntecedent *ifsmdryAndhlowAndtcold = new FuzzyRuleAntecedent();
  ifsmdryAndhlowAndtcold->joinWithAND(hlowAndtcold, smdry1);
  FuzzyRuleConsequent *thenValveFifty = new FuzzyRuleConsequent();
  thenValveFifty->addOutput(fifty);
  FuzzyRule *fuzzyRule4 = new FuzzyRule(4, ifsmdryAndhlowAndtcold, thenValveFifty);
  fuzzy->addFuzzyRule(fuzzyRule4);

  // Building FuzzyRule -13
  FuzzyRuleAntecedent *smsaturated13 = new FuzzyRuleAntecedent();
  smsaturated13->joinSingle(saturated);
  FuzzyRuleAntecedent *ifsmsaturated13AndhlowAndtcold13 = new FuzzyRuleAntecedent();
  ifsmsaturated13AndhlowAndtcold13->joinWithAND(hlowAndtcold, smsaturated13);
  FuzzyRuleConsequent *thenValveTen13 = new FuzzyRuleConsequent();
  thenValveTen13->addOutput(ten);
  FuzzyRule *fuzzyRule13 = new FuzzyRule(13, ifsmsaturated13AndhlowAndtcold13, thenValveTen13);
  fuzzy->addFuzzyRule(fuzzyRule13);

  // Building FuzzyRule -5
  FuzzyRuleAntecedent *hmediumAndtcold = new FuzzyRuleAntecedent();
  hmediumAndtcold->joinWithAND(medium, cold);
  FuzzyRuleAntecedent *smnormal = new FuzzyRuleAntecedent();
  smnormal->joinSingle(normal);
  FuzzyRuleAntecedent *ifsmnormalAndhmediumAndtcold = new FuzzyRuleAntecedent();
  ifsmnormalAndhmediumAndtcold->joinWithAND(hmediumAndtcold, smnormal);
  FuzzyRuleConsequent *thenValveSfive5 = new FuzzyRuleConsequent();
  thenValveSfive5->addOutput(sfive);
  FuzzyRule *fuzzyRule5 = new FuzzyRule(5, ifsmnormalAndhmediumAndtcold, thenValveSfive5);
  fuzzy->addFuzzyRule(fuzzyRule5);

  // Building FuzzyRule -6
  FuzzyRuleAntecedent *hhighAndtnormal6 = new FuzzyRuleAntecedent();
  hhighAndtnormal6->joinWithAND(high, normal1);
  FuzzyRuleAntecedent *ifsmnormalAndhhighAndtnormal6 = new FuzzyRuleAntecedent();
  ifsmnormalAndhhighAndtnormal6->joinWithAND(hhighAndtnormal6, smnormal);
  FuzzyRuleConsequent *thenValveFifty6 = new FuzzyRuleConsequent();
  thenValveFifty6->addOutput(fifty);
  FuzzyRule *fuzzyRule6 = new FuzzyRule(6, ifsmnormalAndhhighAndtnormal6, thenValveFifty6);
  fuzzy->addFuzzyRule(fuzzyRule6);

  // Building FuzzyRule -7
  FuzzyRuleAntecedent *hexhighAndthot7 = new FuzzyRuleAntecedent();
  hexhighAndthot7->joinWithAND(exhigh, hot);
  FuzzyRuleAntecedent *ifsmnormalAndhexhighAndthot7 = new FuzzyRuleAntecedent();
  ifsmnormalAndhexhighAndthot7->joinWithAND(hexhighAndthot7, smnormal);
  FuzzyRuleConsequent *thenValveSfive7 = new FuzzyRuleConsequent();
  thenValveSfive7->addOutput(sfive);
  FuzzyRule *fuzzyRule7 = new FuzzyRule(7, ifsmnormalAndhexhighAndthot7, thenValveSfive7);
  fuzzy->addFuzzyRule(fuzzyRule7);

  // Building FuzzyRule -17
  FuzzyRuleAntecedent *ifsmsaturated17AndhexhighAndthot17 = new FuzzyRuleAntecedent();
  ifsmsaturated17AndhexhighAndthot17->joinWithAND(hexhighAndthot7, smsaturated11);
  FuzzyRuleConsequent *thenValveTfive17 = new FuzzyRuleConsequent();
  thenValveTfive17->addOutput(tfive);
  FuzzyRule *fuzzyRule17 = new FuzzyRule(17, ifsmsaturated17AndhexhighAndthot17, thenValveTfive17);
  fuzzy->addFuzzyRule(fuzzyRule17);

  // Building FuzzyRule -9
  FuzzyRuleAntecedent *hmediumAndthot9 = new FuzzyRuleAntecedent();
  hmediumAndthot9->joinWithAND(medium, hot);
  FuzzyRuleAntecedent *ifsmadeWetAndhmediumAndthot9 = new FuzzyRuleAntecedent();
  ifsmadeWetAndhmediumAndthot9->joinWithAND(hmediumAndthot9, smadeWet8);
  FuzzyRuleConsequent *thenValveTfive9 = new FuzzyRuleConsequent();
  thenValveTfive9->addOutput(tfive);
  FuzzyRule *fuzzyRule9 = new FuzzyRule(9, ifsmadeWetAndhmediumAndthot9, thenValveTfive9);
  fuzzy->addFuzzyRule(fuzzyRule9);

  // Building FuzzyRule -12

  FuzzyRuleAntecedent *ifsmsaturated12AndhmediumAndthot12 = new FuzzyRuleAntecedent();
  ifsmsaturated12AndhmediumAndthot12->joinWithAND(hmediumAndthot9, smsaturated11);
  FuzzyRuleConsequent *thenValveTfive12 = new FuzzyRuleConsequent();
  thenValveTfive12->addOutput(tfive);
  FuzzyRule *fuzzyRule12 = new FuzzyRule(12, ifsmsaturated12AndhmediumAndthot12, thenValveTfive12);
  fuzzy->addFuzzyRule(fuzzyRule12);

  // Building FuzzyRule -10
  FuzzyRuleAntecedent *hhighAndthot10 = new FuzzyRuleAntecedent();
  hhighAndthot10->joinWithAND(high, hot);
  FuzzyRuleAntecedent *ifsmadeWetAndhhighAndthot10 = new FuzzyRuleAntecedent();
  ifsmadeWetAndhhighAndthot10->joinWithAND(hhighAndthot10, smadeWet8);
  FuzzyRuleConsequent *thenValveTfive10 = new FuzzyRuleConsequent();
  thenValveTfive10->addOutput(tfive);
  FuzzyRule *fuzzyRule10 = new FuzzyRule(10, ifsmadeWetAndhhighAndthot10, thenValveTfive10);
  fuzzy->addFuzzyRule(fuzzyRule10);

  // Building FuzzyRule -14
  FuzzyRuleAntecedent *hexhighAndtcold14 = new FuzzyRuleAntecedent();
  hexhighAndtcold14->joinWithAND(exhigh, cold);
  FuzzyRuleAntecedent *ifsmsaturated14AndhexhighAndtcold14 = new FuzzyRuleAntecedent();
  ifsmsaturated14AndhexhighAndtcold14->joinWithAND(hexhighAndtcold14, smsaturated11);
  FuzzyRuleConsequent *thenValveTen14 = new FuzzyRuleConsequent();
  thenValveTen14->addOutput(ten);
  FuzzyRule *fuzzyRule14 = new FuzzyRule(14, ifsmsaturated14AndhexhighAndtcold14, thenValveTen14);
  fuzzy->addFuzzyRule(fuzzyRule14);

  // Building FuzzyRule -16

  FuzzyRuleAntecedent *ifsmsaturated16AndhexhighAndtcold16 = new FuzzyRuleAntecedent();
  ifsmsaturated16AndhexhighAndtcold16->joinWithAND(hexhighAndtcold14, smsaturated11);
  FuzzyRuleConsequent *thenValveTen16 = new FuzzyRuleConsequent();
  thenValveTen16->addOutput(ten);
  FuzzyRule *fuzzyRule16 = new FuzzyRule(16, ifsmsaturated16AndhexhighAndtcold16, thenValveTen16);
  fuzzy->addFuzzyRule(fuzzyRule16);

  // Building FuzzyRule -15
  FuzzyRuleAntecedent *hhighAndtcold15 = new FuzzyRuleAntecedent();
  hhighAndtcold15->joinWithAND(high, cold);
  FuzzyRuleAntecedent *ifsmadeWet15AndhhighAndtcold15 = new FuzzyRuleAntecedent();
  ifsmadeWet15AndhhighAndtcold15->joinWithAND(hhighAndtcold15, smadeWet8);
  FuzzyRuleConsequent *thenValveTen15 = new FuzzyRuleConsequent();
  thenValveTen15->addOutput(ten);
  FuzzyRule *fuzzyRule15 = new FuzzyRule(15, ifsmadeWet15AndhhighAndtcold15, thenValveTen15);
  fuzzy->addFuzzyRule(fuzzyRule15);
}

void loop()
{
  // get random input
  // sm = random(-87, 87);
  // humid = random(0, 100);
  // temp = random(-30, 120);

  sm = map(analogRead(sensorPin), 550, 10, 0, 100);
  humid = dht.readHumidity();
  temp = dht.readTemperature();

  Serial.print("Soil Moisture: ");
  Serial.print(sm);
  Serial.print("%");
  Serial.print(", Humidity: ");
  Serial.print(humid);
  Serial.print("%");
  Serial.print(", and Temperature: ");
  Serial.print(temp);
  Serial.println("C");

  fuzzy->setInput(1, sm);
  fuzzy->setInput(2, humid);
  fuzzy->setInput(3, temp);

  fuzzy->fuzzify();
  float output1 = fuzzy->defuzzify(1);

  Serial.print("\t\t\t\nPumping: ");
  Serial.print(output1);
  Serial.print("\n");

  Serial.print("Fired Rules: ");
  for (size_t i = 1; i <= 17; i++)
  {
    if (fuzzy->isFiredRule(i) == 1)
    {
      Serial.print(i);
      Serial.print(", ");
    }
  }
  Serial.print("\n");

  delay(10000);
}

I hope it still can help you!

from efll.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.