#include <Fuzzy.h>
#define LEDC_TIMER_12_BIT 12
#define LEDC_BASE_FREQ 5000
#define PUMP_PIN1 18
#define PUMP_PIN2 19
#define POT_PIN 36
Fuzzy *fuzzy = new Fuzzy();
FuzzySet *lowPH1 = new FuzzySet(3.0, 3.5, 4.0, 4.5);
FuzzySet *mediumPH1 = new FuzzySet(4.2, 4.7, 5.2, 5.7);
FuzzySet *highPH1 = new FuzzySet(5.4, 5.9, 6.4, 6.9);
FuzzySet *PHsafe = new FuzzySet(7.0, 7.0, 7.0, 7.0);
FuzzySet *lowPH2 = new FuzzySet(7.1, 7.6, 8.1, 8.6);
FuzzySet *mediumPH2 = new FuzzySet(8.3, 8.8, 9.3, 9.8);
FuzzySet *highPH2 = new FuzzySet(9.5, 10.0, 10.5, 11.0);
FuzzySet *offPWM1 = new FuzzySet(0, 0, 0, 0);
FuzzySet *lowPWM1 = new FuzzySet(256, 512, 1024, 1536);
FuzzySet *mediumPWM1 = new FuzzySet(1280, 1792, 2304, 2816);
FuzzySet *highPWM1 = new FuzzySet(2560, 3072, 3584, 4096);
FuzzySet *offPWM2 = new FuzzySet(0, 0, 0, 0);
FuzzySet *lowPWM2 = new FuzzySet(256, 512, 1024, 1536);
FuzzySet *mediumPWM2 = new FuzzySet(1280, 1792, 2304, 2816);
FuzzySet *highPWM2 = new FuzzySet(2560, 3072, 3584, 4096);
float phValue = 0.0f;
void setup() {
pinMode(POT_PIN, INPUT);
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println("\n\n\n--------------------");
Serial.println("Serial Started .");
ledcAttach(PUMP_PIN1, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT);
ledcAttach(PUMP_PIN2, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT);
FuzzyInput *iNpH = new FuzzyInput(1);
iNpH->addFuzzySet(lowPH1);
iNpH->addFuzzySet(mediumPH1);
iNpH->addFuzzySet(highPH1);
iNpH->addFuzzySet(PHsafe);
iNpH->addFuzzySet(lowPH2);
iNpH->addFuzzySet(mediumPH2);
iNpH->addFuzzySet(highPH2);
fuzzy->addFuzzyInput(iNpH);
FuzzyOutput *pump1Speed = new FuzzyOutput(1);
pump1Speed->addFuzzySet(offPWM1);
pump1Speed->addFuzzySet(lowPWM1);
pump1Speed->addFuzzySet(mediumPWM1);
pump1Speed->addFuzzySet(highPWM1);
fuzzy->addFuzzyOutput(pump1Speed);
FuzzyOutput *pump2Speed = new FuzzyOutput(2);
pump2Speed->addFuzzySet(offPWM2);
pump2Speed->addFuzzySet(lowPWM2);
pump2Speed->addFuzzySet(mediumPWM2);
pump2Speed->addFuzzySet(highPWM2);
fuzzy->addFuzzyOutput(pump2Speed);
FuzzyRuleAntecedent *iflowPH1 = new FuzzyRuleAntecedent(); iflowPH1->joinSingle(lowPH1);
FuzzyRuleAntecedent *ifmediumPH1 = new FuzzyRuleAntecedent(); ifmediumPH1->joinSingle(mediumPH1);
FuzzyRuleAntecedent *ifhighPH1 = new FuzzyRuleAntecedent(); ifhighPH1->joinSingle(highPH1);
FuzzyRuleAntecedent *ifPHsafe = new FuzzyRuleAntecedent(); ifPHsafe->joinSingle(PHsafe);
FuzzyRuleAntecedent *iflowPH2 = new FuzzyRuleAntecedent(); iflowPH2->joinSingle(lowPH2);
FuzzyRuleAntecedent *ifmediumPH2 = new FuzzyRuleAntecedent(); ifmediumPH2->joinSingle(mediumPH2);
FuzzyRuleAntecedent *ifhighPH2 = new FuzzyRuleAntecedent(); ifhighPH2->joinSingle(highPH2);
FuzzyRuleConsequent *thenPump1High = new FuzzyRuleConsequent(); thenPump1High->addOutput(highPWM1);
FuzzyRuleConsequent *thenPump1Medium = new FuzzyRuleConsequent(); thenPump1Medium->addOutput(mediumPWM1);
FuzzyRuleConsequent *thenPump1Low = new FuzzyRuleConsequent(); thenPump1Low->addOutput(lowPWM1);
FuzzyRuleConsequent *thenPump2High = new FuzzyRuleConsequent(); thenPump2High->addOutput(highPWM2);
FuzzyRuleConsequent *thenPump2Medium = new FuzzyRuleConsequent(); thenPump2Medium->addOutput(mediumPWM2);
FuzzyRuleConsequent *thenPump2Low = new FuzzyRuleConsequent(); thenPump2Low->addOutput(lowPWM2);
FuzzyRuleConsequent *thenPump2Off = new FuzzyRuleConsequent(); thenPump2Off->addOutput(offPWM2);
FuzzyRuleConsequent *thenPump1Off = new FuzzyRuleConsequent(); thenPump1Off->addOutput(offPWM1);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, iflowPH1, thenPump1High);
fuzzy->addFuzzyRule(fuzzyRule1);
FuzzyRule *fuzzyRule2 = new FuzzyRule(2, iflowPH1, thenPump2Off);
fuzzy->addFuzzyRule(fuzzyRule2);
FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifmediumPH1, thenPump1Medium);
fuzzy->addFuzzyRule(fuzzyRule3);
FuzzyRule *fuzzyRule4 = new FuzzyRule(4, ifmediumPH1, thenPump2Off);
fuzzy->addFuzzyRule(fuzzyRule4);
FuzzyRule *fuzzyRule5 = new FuzzyRule(5, ifhighPH1, thenPump1Low);
fuzzy->addFuzzyRule(fuzzyRule5);
FuzzyRule *fuzzyRule6 = new FuzzyRule(6, ifhighPH1, thenPump2Off);
fuzzy->addFuzzyRule(fuzzyRule6);
FuzzyRule *fuzzyRule7 = new FuzzyRule(7, ifPHsafe, thenPump1Off);
fuzzy->addFuzzyRule(fuzzyRule7);
FuzzyRule *fuzzyRule8 = new FuzzyRule(8, ifPHsafe, thenPump2Off);
fuzzy->addFuzzyRule(fuzzyRule8);
FuzzyRule *fuzzyRule9 = new FuzzyRule(9, iflowPH2, thenPump2Low);
fuzzy->addFuzzyRule(fuzzyRule9);
FuzzyRule *fuzzyRule10 = new FuzzyRule(10, iflowPH2, thenPump1Off);
fuzzy->addFuzzyRule(fuzzyRule10);
FuzzyRule *fuzzyRule11 = new FuzzyRule(11, ifmediumPH2, thenPump2Medium);
fuzzy->addFuzzyRule(fuzzyRule11);
FuzzyRule *fuzzyRule12 = new FuzzyRule(12, ifmediumPH2, thenPump1Off);
fuzzy->addFuzzyRule(fuzzyRule12);
FuzzyRule *fuzzyRule13 = new FuzzyRule(13, ifhighPH2, thenPump2High);
fuzzy->addFuzzyRule(fuzzyRule13);
FuzzyRule *fuzzyRule14 = new FuzzyRule(14, ifhighPH2, thenPump1Off);
fuzzy->addFuzzyRule(fuzzyRule14);
}
void loop() {
int adc = analogRead(POT_PIN);
phValue = mapFloat(adc, 0.0, 4095.0, 3.0, 10.0);
Serial.println(phValue);
fuzzy->setInput(1, phValue);
fuzzy->fuzzify();
float pump1PWM = fuzzy->defuzzify(1);
float pump2PWM = fuzzy->defuzzify(2);
ledcWrite(PUMP_PIN1, pump1PWM);
ledcWrite(PUMP_PIN2, pump2PWM);
delay(100);
}
float mapFloat(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}