// Fuzzy Logic for FuzzyMed Project
// Inputs: Heart Rate (HR), Body Temperature, SpO2
// Output: Health Risk Score (0-10)
#include <Fuzzy.h>
Fuzzy *fuzzy = new Fuzzy();
// Declare fuzzy set pointers
FuzzySet *hrTooLow, *hrLow, *hrNormal, *hrHigh;
FuzzySet *tempLow, *tempNormal, *tempHigh;
FuzzySet *spo2TooLow, *spo2Low, *spo2Normal;
FuzzySet *riskNormal, *riskLow, *riskWarning, *riskHigh, *riskCritical;
void setup() {
Serial.begin(115200);
// ----------- HEART RATE -----------
FuzzyInput *heartRate = new FuzzyInput(1);
hrTooLow = new FuzzySet(0, 0, 25, 45); // Trapezoidal
hrLow = new FuzzySet(40, 55, 55, 70); // Triangular simulated
hrNormal = new FuzzySet(60, 100, 100, 159); // Triangular simulated
hrHigh = new FuzzySet(160, 170, 200, 200); // Trapezoidal
heartRate->addFuzzySet(hrTooLow);
heartRate->addFuzzySet(hrLow);
heartRate->addFuzzySet(hrNormal);
heartRate->addFuzzySet(hrHigh);
fuzzy->addFuzzyInput(heartRate);
// ----------- TEMPERATURE -----------
FuzzyInput *temperature = new FuzzyInput(2);
tempLow = new FuzzySet(30, 33, 35.5, 36.5); // Trapezoidal
tempNormal = new FuzzySet(36.0, 36.8, 36.8, 37.5); // Triangular simulated
tempHigh = new FuzzySet(37.0, 38.5, 41, 41); // Trapezoidal
temperature->addFuzzySet(tempLow);
temperature->addFuzzySet(tempNormal);
temperature->addFuzzySet(tempHigh);
fuzzy->addFuzzyInput(temperature);
// ----------- SpO2 -----------
FuzzyInput *spo2 = new FuzzyInput(3);
spo2TooLow = new FuzzySet(0, 0, 60, 70); // Trapezoidal
spo2Low = new FuzzySet(65, 85, 85, 94); // Triangular simulated
spo2Normal = new FuzzySet(90, 98, 100, 100); // Trapezoidal
spo2->addFuzzySet(spo2TooLow);
spo2->addFuzzySet(spo2Low);
spo2->addFuzzySet(spo2Normal);
fuzzy->addFuzzyInput(spo2);
// ----------- HEALTH RISK SCORE OUTPUT -----------
FuzzyOutput *healthRisk = new FuzzyOutput(1);
riskNormal = new FuzzySet(0, 0, 1, 2.5); // Trapezoidal
riskLow = new FuzzySet(1.5, 2.5, 2.5, 4.0); // Triangular simulated
riskWarning = new FuzzySet(3.0, 4.5, 4.5, 6.8); // Triangular simulated
riskHigh = new FuzzySet(6.2, 7.5, 7.5, 8.8); // Triangular simulated
riskCritical = new FuzzySet(8.2, 9, 10, 10); // Trapezoidal
healthRisk->addFuzzySet(riskNormal);
healthRisk->addFuzzySet(riskLow);
healthRisk->addFuzzySet(riskWarning);
healthRisk->addFuzzySet(riskHigh);
healthRisk->addFuzzySet(riskCritical);
fuzzy->addFuzzyOutput(healthRisk);
// ----------- RULES -----------
// Rule 1: Normal HR + Normal Temp + Normal SpO2 → Normal
FuzzyRuleAntecedent* a1 = new FuzzyRuleAntecedent();
a1->joinWithAND(hrNormal, tempNormal);
FuzzyRuleAntecedent* r1 = new FuzzyRuleAntecedent();
r1->joinWithAND(a1, spo2Normal);
FuzzyRuleConsequent* r1c = new FuzzyRuleConsequent();
r1c->addOutput(riskNormal);
fuzzy->addFuzzyRule(new FuzzyRule(1, r1, r1c));
// Rule 2: High HR + Normal Temp + Normal SpO2 → Low Risk
FuzzyRuleAntecedent* a2 = new FuzzyRuleAntecedent();
a2->joinWithAND(hrHigh, tempNormal);
FuzzyRuleAntecedent* r2 = new FuzzyRuleAntecedent();
r2->joinWithAND(a2, spo2Normal);
FuzzyRuleConsequent* r2c = new FuzzyRuleConsequent();
r2c->addOutput(riskLow);
fuzzy->addFuzzyRule(new FuzzyRule(2, r2, r2c));
// Rule 3: Normal HR + High Temp + Normal SpO2 → Warning
FuzzyRuleAntecedent* a3 = new FuzzyRuleAntecedent();
a3->joinWithAND(hrNormal, tempHigh);
FuzzyRuleAntecedent* r3 = new FuzzyRuleAntecedent();
r3->joinWithAND(a3, spo2Normal);
FuzzyRuleConsequent* r3c = new FuzzyRuleConsequent();
r3c->addOutput(riskWarning);
fuzzy->addFuzzyRule(new FuzzyRule(3, r3, r3c));
// Rule 4: Low HR + Low Temp + Normal SpO2 → Warning
FuzzyRuleAntecedent* a4 = new FuzzyRuleAntecedent();
a4->joinWithAND(hrLow, tempLow);
FuzzyRuleAntecedent* r4 = new FuzzyRuleAntecedent();
r4->joinWithAND(a4, spo2Normal);
FuzzyRuleConsequent* r4c = new FuzzyRuleConsequent();
r4c->addOutput(riskWarning);
fuzzy->addFuzzyRule(new FuzzyRule(4, r4, r4c));
// Rule 5: High HR + Normal Temp + Low SpO2 → Warning
FuzzyRuleAntecedent* a5 = new FuzzyRuleAntecedent();
a5->joinWithAND(hrHigh, tempNormal);
FuzzyRuleAntecedent* r5 = new FuzzyRuleAntecedent();
r5->joinWithAND(a5, spo2Low);
FuzzyRuleConsequent* r5c = new FuzzyRuleConsequent();
r5c->addOutput(riskWarning);
fuzzy->addFuzzyRule(new FuzzyRule(5, r5, r5c));
// Rule 6: High HR + High Temp + Low SpO2 → High Risk
FuzzyRuleAntecedent* a6 = new FuzzyRuleAntecedent();
a6->joinWithAND(hrHigh, tempHigh);
FuzzyRuleAntecedent* r6 = new FuzzyRuleAntecedent();
r6->joinWithAND(a6, spo2Low);
FuzzyRuleConsequent* r6c = new FuzzyRuleConsequent();
r6c->addOutput(riskHigh);
fuzzy->addFuzzyRule(new FuzzyRule(6, r6, r6c));
// Rule 7: Low HR + Normal Temp + Low SpO2 → High Risk
FuzzyRuleAntecedent* a7 = new FuzzyRuleAntecedent();
a7->joinWithAND(hrLow, tempNormal);
FuzzyRuleAntecedent* r7 = new FuzzyRuleAntecedent();
r7->joinWithAND(a7, spo2Low);
FuzzyRuleConsequent* r7c = new FuzzyRuleConsequent();
r7c->addOutput(riskHigh);
fuzzy->addFuzzyRule(new FuzzyRule(7, r7, r7c));
// Rule 8: Too Low HR + Low Temp + Low SpO2 → Critical
FuzzyRuleAntecedent* a8 = new FuzzyRuleAntecedent();
a8->joinWithAND(hrTooLow, tempLow);
FuzzyRuleAntecedent* r8 = new FuzzyRuleAntecedent();
r8->joinWithAND(a8, spo2Low);
FuzzyRuleConsequent* r8c = new FuzzyRuleConsequent();
r8c->addOutput(riskCritical);
fuzzy->addFuzzyRule(new FuzzyRule(8, r8, r8c));
// Rule 9: Too Low HR + Normal Temp + Too Low SpO2 → Critical
FuzzyRuleAntecedent* a9 = new FuzzyRuleAntecedent();
a9->joinWithAND(hrTooLow, tempNormal);
FuzzyRuleAntecedent* r9 = new FuzzyRuleAntecedent();
r9->joinWithAND(a9, spo2TooLow);
FuzzyRuleConsequent* r9c = new FuzzyRuleConsequent();
r9c->addOutput(riskCritical);
fuzzy->addFuzzyRule(new FuzzyRule(9, r9, r9c));
// Rule 10: Normal HR + Normal Temp + Low SpO2 → Warning
FuzzyRuleAntecedent* a10 = new FuzzyRuleAntecedent();
a10->joinWithAND(hrNormal, tempNormal);
FuzzyRuleAntecedent* r10 = new FuzzyRuleAntecedent();
r10->joinWithAND(a10, spo2Low);
FuzzyRuleConsequent* r10c = new FuzzyRuleConsequent();
r10c->addOutput(riskWarning);
fuzzy->addFuzzyRule(new FuzzyRule(10, r10, r10c));
// Rule 11: High HR + Low Temp + Normal SpO2 → Warning
FuzzyRuleAntecedent* a11 = new FuzzyRuleAntecedent();
a11->joinWithAND(hrHigh, tempLow);
FuzzyRuleAntecedent* r11 = new FuzzyRuleAntecedent();
r11->joinWithAND(a11, spo2Normal);
FuzzyRuleConsequent* r11c = new FuzzyRuleConsequent();
r11c->addOutput(riskWarning);
fuzzy->addFuzzyRule(new FuzzyRule(11, r11, r11c));
// Rule 12: Low HR + High Temp + Normal SpO2 → Warning
FuzzyRuleAntecedent* a12 = new FuzzyRuleAntecedent();
a12->joinWithAND(hrLow, tempHigh);
FuzzyRuleAntecedent* r12 = new FuzzyRuleAntecedent();
r12->joinWithAND(a12, spo2Normal);
FuzzyRuleConsequent* r12c = new FuzzyRuleConsequent();
r12c->addOutput(riskWarning);
fuzzy->addFuzzyRule(new FuzzyRule(12, r12, r12c));
// Rule 13: Normal HR + Low Temp + Too Low SpO2 → Critical
FuzzyRuleAntecedent* a13 = new FuzzyRuleAntecedent();
a13->joinWithAND(hrNormal, tempLow);
FuzzyRuleAntecedent* r13 = new FuzzyRuleAntecedent();
r13->joinWithAND(a13, spo2TooLow);
FuzzyRuleConsequent* r13c = new FuzzyRuleConsequent();
r13c->addOutput(riskCritical);
fuzzy->addFuzzyRule(new FuzzyRule(13, r13, r13c));
// Rule 14: Low HR + Low Temp + Too Low SpO2 → Critical
FuzzyRuleAntecedent* a14 = new FuzzyRuleAntecedent();
a14->joinWithAND(hrLow, tempLow);
FuzzyRuleAntecedent* r14 = new FuzzyRuleAntecedent();
r14->joinWithAND(a14, spo2TooLow);
FuzzyRuleConsequent* r14c = new FuzzyRuleConsequent();
r14c->addOutput(riskCritical);
fuzzy->addFuzzyRule(new FuzzyRule(14, r14, r14c));
}
void loop() {
int rawSpO2 = analogRead(32); // Simulated SpO2 input
int rawHR = analogRead(34); // Simulated heart rate input
int rawTemp = analogRead(35); // Simulated temperature input
float hr = (rawHR / 4095.0) * 200.0; // Scale 0–4095 to 0–200 bpm
float temp = (rawTemp / 4095.0) * 11.0 + 30; // Scale 0–4095 to 30–41°C
float oxygen = (rawSpO2 / 4095.0) * 100.0; // Scale 0–4095 to 0–100%
fuzzy->setInput(1, hr);
fuzzy->setInput(2, temp);
fuzzy->setInput(3, oxygen);
fuzzy->fuzzify();
float result = fuzzy->defuzzify(1);
Serial.print("HR: "); Serial.print(hr);
Serial.print(" Temp: "); Serial.print(temp);
Serial.print(" SpO2: "); Serial.print(oxygen);
Serial.print(" --> Health Risk Score: "); Serial.println(result);
delay(3000);
}