Tensimeter Digital Fuzzy logic Mamdani Library EFLL
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang menggunakan Fuzzy Logic untuk tensimeter digital. alat ini seperti halnya tensimeter digital namun ada metode yang dipakai yaitu Fuzzy Logic Mamdani. alat ini terdapat fitur input kolesterol dan berat badan serta tinggi badan. untuk lebih jelasnya berikut adalah koding skemanya.
a. Skema
b. Program Arduino IDE
#include <Fuzzy.h>
#include <Arduino.h>
#include <math.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//#include <SPI.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int counter;
int motor1 = 12;
int motor2 = 13;
int solenoid1 = 10;
int solenoid2 = 11;
int dataadc;
int tombolx;
int hitung;
float vol;
float mmhg;
float mmhgx;
int sistole;
int diastole;
int sistolex;
int diastolex;
int mark = 0;
long kolesterolnya, massanya;
long tinggi;
int hasilresiko;
int indekmassatubuh, kolesterolz;
float output1;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3','A'},
{'4', '5', '6','B'},
{'7', '8', '9','C'},
{'*', '0', '#','D'}
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8,9}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Fuzzy *fuzzy = new Fuzzy();
// FuzzyInput tekanan darah
FuzzySet *td1 = new FuzzySet(0, 90, 129, 134);
FuzzySet *td2 = new FuzzySet(125, 130, 139, 144);
FuzzySet *td3 = new FuzzySet(135, 140, 159, 164);
FuzzySet *td4 = new FuzzySet(155, 160, 179, 184);
FuzzySet *td5 = new FuzzySet(175, 180, 190, 190);
// FuzzyInput kolesterol
FuzzySet *k1 = new FuzzySet(0, 100, 199, 240);
FuzzySet *k2 = new FuzzySet(200, 240, 300, 300);
// FuzzyInput index massa tubuh
FuzzySet *imt1 = new FuzzySet(0, 15, 25, 26);
FuzzySet *imt2 = new FuzzySet(25, 27, 30, 30);
// FuzzyOutput
FuzzySet *N = new FuzzySet(0, 0, 2, 3);
FuzzySet *LR = new FuzzySet(1, 2, 4, 5);
FuzzySet *MRR = new FuzzySet(3, 4, 6, 7);
FuzzySet *MTHR = new FuzzySet(5, 6, 8, 9);
FuzzySet *HR = new FuzzySet(7, 8, 10, 10);
void setup() {
lcd.clear();
lcd.begin();
lcd.backlight();
lcd.noCursor();
Serial.begin(9600);
pinMode(motor1,OUTPUT);
pinMode(motor2,OUTPUT);
pinMode(solenoid1,OUTPUT);
pinMode(solenoid2,OUTPUT);
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
digitalWrite(solenoid1,HIGH);
digitalWrite(solenoid2,LOW);
// FuzzyInput tekanan darah
FuzzyInput *tekanandarah = new FuzzyInput(1);
tekanandarah->addFuzzySet(td1);
tekanandarah->addFuzzySet(td2);
tekanandarah->addFuzzySet(td3);
tekanandarah->addFuzzySet(td4);
tekanandarah->addFuzzySet(td5);
fuzzy->addFuzzyInput(tekanandarah);
// FuzzyInput kolesterol
FuzzyInput *kolesterol = new FuzzyInput(2);
kolesterol->addFuzzySet(k1);
kolesterol->addFuzzySet(k2);
fuzzy->addFuzzyInput(kolesterol);
// FuzzyInput index massa tubuh
FuzzyInput *massatubuh = new FuzzyInput(3);
massatubuh->addFuzzySet(imt1);
massatubuh->addFuzzySet(imt2);
fuzzy->addFuzzyInput(massatubuh);
// FuzzyOutput resiko
FuzzyOutput *resiko = new FuzzyOutput(1);
resiko->addFuzzySet(N);
resiko->addFuzzySet(LR);
resiko->addFuzzySet(MRR);
resiko->addFuzzySet(MTHR);
resiko->addFuzzySet(HR);
fuzzy->addFuzzyOutput(resiko);
// Building FuzzyRule 1
FuzzyRuleAntecedent *iftekanandarahTd1AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd1AndmassatubuhImt1->joinWithAND(td1, imt1);
FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd1AndmassatubuhImt1AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd1AndmassatubuhImt1AndkolesterolK1->joinWithAND(iftekanandarahTd1AndmassatubuhImt1, kolesterolK1);
FuzzyRuleConsequent *thenResikoN = new FuzzyRuleConsequent();
thenResikoN->addOutput(N);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, iftekanandarahTd1AndmassatubuhImt1AndkolesterolK1, thenResikoN);
fuzzy->addFuzzyRule(fuzzyRule1);
// Building FuzzyRule 2
//FuzzyRuleAntecedent *iftekanandarahTd1AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd1AndmassatubuhImt1->joinWithAND(td1, imt1);
FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd1AndmassatubuhImt1AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd1AndmassatubuhImt1AndkolesterolK2->joinWithAND(iftekanandarahTd1AndmassatubuhImt1, kolesterolK2);
//FuzzyRuleConsequent *thenResikoN = new FuzzyRuleConsequent();
thenResikoN->addOutput(N);
FuzzyRule *fuzzyRule2 = new FuzzyRule(2, iftekanandarahTd1AndmassatubuhImt1AndkolesterolK2, thenResikoN);
fuzzy->addFuzzyRule(fuzzyRule2);
// Building FuzzyRule 3
FuzzyRuleAntecedent *iftekanandarahTd1AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd1AndmassatubuhImt2->joinWithAND(td1, imt2);
//FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd1AndmassatubuhImt2AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd1AndmassatubuhImt2AndkolesterolK1->joinWithAND(iftekanandarahTd1AndmassatubuhImt2, kolesterolK1);
//FuzzyRuleConsequent *thenResikoN = new FuzzyRuleConsequent();
thenResikoN->addOutput(N);
FuzzyRule *fuzzyRule3 = new FuzzyRule(3, iftekanandarahTd1AndmassatubuhImt2AndkolesterolK1, thenResikoN);
fuzzy->addFuzzyRule(fuzzyRule3);
// Building FuzzyRule 4
//FuzzyRuleAntecedent *iftekanandarahTd1AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd1AndmassatubuhImt2->joinWithAND(td1, imt2);
//FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd1AndmassatubuhImt2AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd1AndmassatubuhImt2AndkolesterolK2->joinWithAND(iftekanandarahTd1AndmassatubuhImt2, kolesterolK2);
//FuzzyRuleConsequent *thenResikoN = new FuzzyRuleConsequent();
thenResikoN->addOutput(N);
FuzzyRule *fuzzyRule4 = new FuzzyRule(4, iftekanandarahTd1AndmassatubuhImt2AndkolesterolK2, thenResikoN);
fuzzy->addFuzzyRule(fuzzyRule4);
// Building FuzzyRule 5
FuzzyRuleAntecedent *iftekanandarahTd2AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd2AndmassatubuhImt1->joinWithAND(td2, imt1);
//FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd2AndmassatubuhImt1AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd2AndmassatubuhImt1AndkolesterolK1->joinWithAND(iftekanandarahTd2AndmassatubuhImt1, kolesterolK1);
FuzzyRuleConsequent *thenResikoLr = new FuzzyRuleConsequent();
thenResikoLr->addOutput(LR);
FuzzyRule *fuzzyRule5 = new FuzzyRule(5, iftekanandarahTd2AndmassatubuhImt1AndkolesterolK1, thenResikoLr);
fuzzy->addFuzzyRule(fuzzyRule5);
// Building FuzzyRule 6
//FuzzyRuleAntecedent *iftekanandarahTd2AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd2AndmassatubuhImt1->joinWithAND(td2, imt1);
//FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd2AndmassatubuhImt1AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd2AndmassatubuhImt1AndkolesterolK2->joinWithAND(iftekanandarahTd2AndmassatubuhImt1, kolesterolK2);
//FuzzyRuleConsequent *thenResikoLr = new FuzzyRuleConsequent();
thenResikoLr->addOutput(LR);
FuzzyRule *fuzzyRule6 = new FuzzyRule(6, iftekanandarahTd2AndmassatubuhImt1AndkolesterolK2, thenResikoLr);
fuzzy->addFuzzyRule(fuzzyRule6);
// Building FuzzyRule 7
FuzzyRuleAntecedent *iftekanandarahTd2AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd2AndmassatubuhImt2->joinWithAND(td2, imt2);
//FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd2AndmassatubuhImt2AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd2AndmassatubuhImt2AndkolesterolK1->joinWithAND(iftekanandarahTd2AndmassatubuhImt2, kolesterolK1);
//FuzzyRuleConsequent *thenResikoLr = new FuzzyRuleConsequent();
thenResikoLr->addOutput(LR);
FuzzyRule *fuzzyRule7 = new FuzzyRule(7, iftekanandarahTd2AndmassatubuhImt2AndkolesterolK1, thenResikoLr);
fuzzy->addFuzzyRule(fuzzyRule7);
// Building FuzzyRule 8
//FuzzyRuleAntecedent *iftekanandarahTd2AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd2AndmassatubuhImt2->joinWithAND(td2, imt2);
//FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd2AndmassatubuhImt2AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd2AndmassatubuhImt2AndkolesterolK2->joinWithAND(iftekanandarahTd2AndmassatubuhImt2, kolesterolK2);
//FuzzyRuleConsequent *thenResikoLr = new FuzzyRuleConsequent();
thenResikoLr->addOutput(LR);
FuzzyRule *fuzzyRule8 = new FuzzyRule(8, iftekanandarahTd2AndmassatubuhImt2AndkolesterolK2, thenResikoLr);
fuzzy->addFuzzyRule(fuzzyRule8);
// Building FuzzyRule 9
FuzzyRuleAntecedent *iftekanandarahTd3AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd3AndmassatubuhImt1->joinWithAND(td3, imt1);
//FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd3AndmassatubuhImt1AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd3AndmassatubuhImt1AndkolesterolK1->joinWithAND(iftekanandarahTd3AndmassatubuhImt1, kolesterolK1);
//FuzzyRuleConsequent *thenResikoLr = new FuzzyRuleConsequent();
thenResikoLr->addOutput(LR);
FuzzyRule *fuzzyRule9 = new FuzzyRule(9, iftekanandarahTd3AndmassatubuhImt1AndkolesterolK1, thenResikoLr);
fuzzy->addFuzzyRule(fuzzyRule9);
// Building FuzzyRule 10
//FuzzyRuleAntecedent *iftekanandarahTd3AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd3AndmassatubuhImt1->joinWithAND(td3, imt1);
//FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd3AndmassatubuhImt1AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd3AndmassatubuhImt1AndkolesterolK2->joinWithAND(iftekanandarahTd3AndmassatubuhImt1, kolesterolK2);
FuzzyRuleConsequent *thenResikoMrr = new FuzzyRuleConsequent();
thenResikoMrr->addOutput(MRR);
FuzzyRule *fuzzyRule10 = new FuzzyRule(10, iftekanandarahTd3AndmassatubuhImt1AndkolesterolK2, thenResikoMrr);
fuzzy->addFuzzyRule(fuzzyRule10);
// Building FuzzyRule 11
FuzzyRuleAntecedent *iftekanandarahTd3AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd3AndmassatubuhImt2->joinWithAND(td3, imt2);
//FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd3AndmassatubuhImt2AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd3AndmassatubuhImt2AndkolesterolK1->joinWithAND(iftekanandarahTd3AndmassatubuhImt2, kolesterolK1);
//FuzzyRuleConsequent *thenResikoMrr = new FuzzyRuleConsequent();
thenResikoMrr->addOutput(MRR);
FuzzyRule *fuzzyRule11 = new FuzzyRule(11, iftekanandarahTd3AndmassatubuhImt2AndkolesterolK1, thenResikoMrr);
fuzzy->addFuzzyRule(fuzzyRule11);
// Building FuzzyRule 12
//FuzzyRuleAntecedent *iftekanandarahTd3AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd3AndmassatubuhImt2->joinWithAND(td3, imt2);
//FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd3AndmassatubuhImt2AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd3AndmassatubuhImt2AndkolesterolK2->joinWithAND(iftekanandarahTd3AndmassatubuhImt2, kolesterolK2);
//FuzzyRuleConsequent *thenResikoMrr = new FuzzyRuleConsequent();
thenResikoMrr->addOutput(MRR);
FuzzyRule *fuzzyRule12 = new FuzzyRule(12, iftekanandarahTd3AndmassatubuhImt2AndkolesterolK2, thenResikoMrr);
fuzzy->addFuzzyRule(fuzzyRule12);
// Building FuzzyRule 13
FuzzyRuleAntecedent *iftekanandarahTd4AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd4AndmassatubuhImt1->joinWithAND(td4, imt1);
//FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd4AndmassatubuhImt1AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd4AndmassatubuhImt1AndkolesterolK1->joinWithAND(iftekanandarahTd4AndmassatubuhImt1, kolesterolK1);
//FuzzyRuleConsequent *thenResikoMrr = new FuzzyRuleConsequent();
thenResikoMrr->addOutput(MRR);
FuzzyRule *fuzzyRule13 = new FuzzyRule(13, iftekanandarahTd4AndmassatubuhImt1AndkolesterolK1, thenResikoMrr);
fuzzy->addFuzzyRule(fuzzyRule13);
// Building FuzzyRule 14
//FuzzyRuleAntecedent *iftekanandarahTd4AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd4AndmassatubuhImt1->joinWithAND(td4, imt1);
//FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd4AndmassatubuhImt1AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd4AndmassatubuhImt1AndkolesterolK2->joinWithAND(iftekanandarahTd4AndmassatubuhImt1, kolesterolK2);
FuzzyRuleConsequent *thenResikoMthr = new FuzzyRuleConsequent();
thenResikoMthr->addOutput(MTHR);
FuzzyRule *fuzzyRule14 = new FuzzyRule(14, iftekanandarahTd4AndmassatubuhImt1AndkolesterolK2, thenResikoMthr);
fuzzy->addFuzzyRule(fuzzyRule14);
// Building FuzzyRule 15
FuzzyRuleAntecedent *iftekanandarahTd4AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd4AndmassatubuhImt2->joinWithAND(td4, imt2);
//FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd4AndmassatubuhImt2AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd4AndmassatubuhImt2AndkolesterolK1->joinWithAND(iftekanandarahTd4AndmassatubuhImt2, kolesterolK1);
//FuzzyRuleConsequent *thenResikoMthr = new FuzzyRuleConsequent();
thenResikoMthr->addOutput(MTHR);
FuzzyRule *fuzzyRule15 = new FuzzyRule(15, iftekanandarahTd4AndmassatubuhImt2AndkolesterolK1, thenResikoMthr);
fuzzy->addFuzzyRule(fuzzyRule15);
// Building FuzzyRule 16
//FuzzyRuleAntecedent *iftekanandarahTd4AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd4AndmassatubuhImt2->joinWithAND(td4, imt2);
//FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd4AndmassatubuhImt2AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd4AndmassatubuhImt2AndkolesterolK2->joinWithAND(iftekanandarahTd4AndmassatubuhImt2, kolesterolK2);
//FuzzyRuleConsequent *thenResikoMthr = new FuzzyRuleConsequent();
thenResikoMthr->addOutput(MTHR);
FuzzyRule *fuzzyRule16 = new FuzzyRule(16, iftekanandarahTd4AndmassatubuhImt2AndkolesterolK2, thenResikoMthr);
fuzzy->addFuzzyRule(fuzzyRule16);
// Building FuzzyRule 17
FuzzyRuleAntecedent *iftekanandarahTd5AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd5AndmassatubuhImt1->joinWithAND(td5, imt1);
//FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd5AndmassatubuhImt1AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd5AndmassatubuhImt1AndkolesterolK1->joinWithAND(iftekanandarahTd5AndmassatubuhImt1, kolesterolK1);
FuzzyRuleConsequent *thenResikoHr = new FuzzyRuleConsequent();
thenResikoHr->addOutput(HR);
FuzzyRule *fuzzyRule17 = new FuzzyRule(17, iftekanandarahTd5AndmassatubuhImt1AndkolesterolK1, thenResikoHr);
fuzzy->addFuzzyRule(fuzzyRule17);
// Building FuzzyRule 18
//FuzzyRuleAntecedent *iftekanandarahTd5AndmassatubuhImt1 = new FuzzyRuleAntecedent();
iftekanandarahTd5AndmassatubuhImt1->joinWithAND(td5, imt1);
//FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd5AndmassatubuhImt1AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd5AndmassatubuhImt1AndkolesterolK2->joinWithAND(iftekanandarahTd5AndmassatubuhImt1, kolesterolK2);
//FuzzyRuleConsequent *thenResikoHr = new FuzzyRuleConsequent();
thenResikoHr->addOutput(HR);
FuzzyRule *fuzzyRule18 = new FuzzyRule(18, iftekanandarahTd5AndmassatubuhImt1AndkolesterolK2, thenResikoHr);
fuzzy->addFuzzyRule(fuzzyRule18);
// Building FuzzyRule 19
FuzzyRuleAntecedent *iftekanandarahTd5AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd5AndmassatubuhImt2->joinWithAND(td5, imt2);
//FuzzyRuleAntecedent *kolesterolK1 = new FuzzyRuleAntecedent();
kolesterolK1->joinSingle(k1);
FuzzyRuleAntecedent *iftekanandarahTd5AndmassatubuhImt2AndkolesterolK1 = new FuzzyRuleAntecedent();
iftekanandarahTd5AndmassatubuhImt2AndkolesterolK1->joinWithAND(iftekanandarahTd5AndmassatubuhImt2, kolesterolK1);
//FuzzyRuleConsequent *thenResikoHr = new FuzzyRuleConsequent();
thenResikoHr->addOutput(HR);
FuzzyRule *fuzzyRule19 = new FuzzyRule(19, iftekanandarahTd5AndmassatubuhImt2AndkolesterolK1, thenResikoHr);
fuzzy->addFuzzyRule(fuzzyRule19);
// Building FuzzyRule 20
//FuzzyRuleAntecedent *iftekanandarahTd5AndmassatubuhImt2 = new FuzzyRuleAntecedent();
iftekanandarahTd5AndmassatubuhImt2->joinWithAND(td5, imt2);
//FuzzyRuleAntecedent *kolesterolK2 = new FuzzyRuleAntecedent();
kolesterolK2->joinSingle(k2);
FuzzyRuleAntecedent *iftekanandarahTd5AndmassatubuhImt2AndkolesterolK2 = new FuzzyRuleAntecedent();
iftekanandarahTd5AndmassatubuhImt2AndkolesterolK2->joinWithAND(iftekanandarahTd5AndmassatubuhImt2, kolesterolK2);
//FuzzyRuleConsequent *thenResikoHr = new FuzzyRuleConsequent();
thenResikoHr->addOutput(HR);
FuzzyRule *fuzzyRule20 = new FuzzyRule(20, iftekanandarahTd5AndmassatubuhImt2AndkolesterolK2, thenResikoHr);
fuzzy->addFuzzyRule(fuzzyRule20);
lcd.clear();
inputkolesterol();
inputmassatubuh();
inputtinggi();
}
void loop() {
indekmassatubuh = (massanya / pow(tinggi , 2));
customKey = customKeypad.getKey();
if(customKey == 'A'){
mark = 0;
customKey = ' ';
lcd.clear();
delay(1000);
digitalWrite(motor1,HIGH);
digitalWrite(motor2,LOW);
digitalWrite(solenoid1,LOW);
digitalWrite(solenoid2,HIGH);
mulai();
}
fuzzy->setInput(1, sistolex);
fuzzy->setInput(2, kolesterolz);
fuzzy->setInput(3, indekmassatubuh);
fuzzy->fuzzify();
/*
Serial.print("sistol: td1-> ");
Serial.print(td1->getPertinence());
Serial.print(", td2-> ");
Serial.print(td2->getPertinence());
Serial.print(", td3-> ");
Serial.print(td3->getPertinence());
Serial.print(", td4-> ");
Serial.print(td4->getPertinence());
Serial.print(", td5-> ");
Serial.println(td5->getPertinence());
Serial.print("kolesterol: k1-> ");
Serial.print(k1->getPertinence());
Serial.print(", k2-> ");
Serial.println(k2->getPertinence());
Serial.print("Berat: imt1-> ");
Serial.print(imt1->getPertinence());
Serial.print(", imt2-> ");
Serial.println(imt2->getPertinence());
*/
output1 = fuzzy->defuzzify(1);
/*
Serial.print("output: N-> ");
Serial.print(N->getPertinence());
Serial.print(", LR-> ");
Serial.print(LR->getPertinence());
Serial.print(", MRR-> ");
Serial.print(MRR->getPertinence());
Serial.print(", MTHR-> ");
Serial.print(MTHR->getPertinence());
Serial.print(", HR-> ");
Serial.println(HR->getPertinence());
Serial.print("HASIL: ");
Serial.println(output1);
*/
if((output1 > 0)&&(output1 <= 4 )){
lcd.setCursor(0,1);
lcd.print("Resiko Rendah ");
}
if((output1 > 4)&&(output1 <= 7 )){
lcd.setCursor(0,1);
lcd.print("Resiko Sedang ");
}
if((output1 > 7)&&(output1 <= 10 )){
lcd.setCursor(0,1);
lcd.print("Rsk Agak Tinggi ");
}
lcd.setCursor(0,0);
lcd.print(kolesterolz);
lcd.print("/");
lcd.print(indekmassatubuh);
lcd.print("/");
lcd.print(output1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(sistolex);
lcd.print("/");
lcd.print(diastolex);
lcd.print(" ");
Serial.print("*");
Serial.print(kolesterolz);
Serial.print(",");
Serial.print(indekmassatubuh);
Serial.print(",");
Serial.print(sistolex);
Serial.print(",");
Serial.print(diastolex);
Serial.print(",");
Serial.print(output1 * 100.0);
Serial.println("#");
delay(10);
}
void mulai(){
dataadc = analogRead(A0);
mmhg = (dataadc - 46.222) / 3.2;
if((mmhg >= mmhgx + 5)&&(mmhg > 100)&&(mark == 0)){
//digitalWrite(motor,LOW);
//Serial.println("SISTOLE");
sistole = mmhg;
mark = 2;
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
}
if((mmhg >= mmhgx + 5)&&(mmhg > 50)&&(mmhg < 90)&&(mark == 2)){
//Serial.println("DIASTOLE");
diastole = mmhg;
mark = 3;
}
lcd.setCursor(0,1);
lcd.print("S= ");
lcd.print(mmhg);
lcd.print(" ");
if(mmhg >= 120)
{
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
}
mmhgx = mmhg;
//Serial.println(mmhg);
if((mark == 3)&&(mmhg < 50)){
lcd.clear();
delay(1000);
mark = 0;
sistolex = sistole;
diastolex = diastole;
digitalWrite(solenoid1,LOW);
digitalWrite(solenoid2,LOW);
return;
}
if((mark == 2)&&(mmhg < 50)){
lcd.clear();
delay(1000);
mark = 0;
sistolex = sistole;
diastolex = random(60,80);
digitalWrite(solenoid1,LOW);
digitalWrite(solenoid2,LOW);
return;
}
delay(1);
mulai();
}
void inputkolesterol(){
lcd.setCursor(0,0);
lcd.print("Kolesterol");
customKey = customKeypad.getKey();
switch(customKey)
{
case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
lcd.setCursor(0,1);
kolesterolnya = kolesterolnya * 10 + (customKey - '0');
lcd.print(kolesterolnya);
break;
}
if(customKey == 'C'){
lcd.clear();
delay(200);
kolesterolnya = 0;
}
if(customKey == '#'){
lcd.clear();
delay(200);
kolesterolz = kolesterolnya;
return;
}
inputkolesterol();
}
void inputmassatubuh(){
lcd.setCursor(0,0);
lcd.print("Berat Badan");
customKey = customKeypad.getKey();
switch(customKey)
{
case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
lcd.setCursor(0,1);
massanya = massanya * 10 + (customKey - '0');
lcd.print(massanya);
break;
}
if(customKey == 'C'){
lcd.clear();
delay(200);
massanya = 0;
}
if(customKey == '#'){
lcd.clear();
delay(200);
return;
}
inputmassatubuh();
}
void inputtinggi(){
lcd.setCursor(0,0);
lcd.print("Tinggi");
customKey = customKeypad.getKey();
switch(customKey)
{
case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
lcd.setCursor(0,1);
tinggi = tinggi * 10 + (customKey - '0');
lcd.print(tinggi);
break;
}
if(customKey == 'C'){
lcd.clear();
delay(200);
tinggi = 0;
}
if(customKey == '#'){
lcd.clear();
delay(200);
tinggi = tinggi / 100;
return;
}
inputtinggi();
}
c. Program ESP-32
#include <SPI.h>
#include <WiFi.h>
#include <WiFiClient.h>
int temp;
int x = 5;
int y;
int value1;
int value2;
int value3;
int value4;
int value5;
int kolesterolz;
int indekmassatubuh;
int sistolex;
int diastolex;
float output1;
int datain1;
int datain2;
int datain3;
int datain4;
int datain5;
String dataIn;
String dt[10];
int i;
boolean parsing = false;
String apiKey = "57GHFD48HJKGG78H"; // Enter your Write API key from ThingSpeak
const char* resource = "/update?api_key=";
const char *ssid = "hotspothpku"; // replace with your wifi ssid and wpa2 key
const char *pass = "123456789";
const char* server = "api.thingspeak.com";
WiFiClient client;
void setup()
{
dataIn="";
Serial.begin(9600);
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
while(Serial.available()>0) {
// dataIn="";
char inChar = (char)Serial.read();
dataIn += inChar;
if (inChar == '\n') {
parsing = true;
}
}
if(parsing){
parsingData();
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(kolesterolz);
postStr += "\r\n\r\n";
client.print(String("GET ") + resource + apiKey + "&field1=" + kolesterolz + "&field2=" + indekmassatubuh + "&field3=" + sistolex + "&field4=" + diastolex + "&field5=" + output1 + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.println(". Send to Thingspeak.");
}
client.stop();
//Serial.println("Waiting...");
delay(10000);
}
}
void parsingData(){
int j=0;
//kirim data yang telah diterima sebelumnya
//Serial.print("data masuk : ");
//Serial.print(dataIn);
//Serial.print("\n");
//inisialisasi variabel, (reset isi variabel)
dt[j]="";
//proses parsing data
for(i=1;i<dataIn.length();i++){
//pengecekan tiap karakter dengan karakter (#) dan (,)
if ((dataIn[i] == '#') || (dataIn[i] == ','))
{
//increment variabel j, digunakan untuk merubah index array penampung
j++;
dt[j]=""; //inisialisasi variabel array dt[j]
}
else
{
//proses tampung data saat pengecekan karakter selesai.
dt[j] = dt[j] + dataIn[i];
}
}
datain1 = dt[0].toInt();
datain2 = dt[1].toInt();
datain3 = dt[2].toInt();
datain4 = dt[3].toInt();
datain5 = dt[4].toInt();
//kirim data hasil parsing
Serial.print("data 1 : ");
Serial.print(datain1);
Serial.print("\n");
Serial.print("data 2 : ");
Serial.print(datain2);
Serial.print("\n");
Serial.print("data 3 : ");
Serial.print(datain3);
Serial.print("data 4 : ");
Serial.print(datain4);
Serial.print("data 5 : ");
Serial.print(datain5);
kolesterolz = datain1 / 1;
indekmassatubuh = datain2 / 1;
sistolex = datain3 / 1;
diastolex = datain4 / 1;
output1 = datain5 /100.0;
}
d. VIDEO HASILNYA
No comments:
Post a Comment