Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang menggunakan metode fuzzy logic mamdani untuk input sensot PH dan turbidity. alat ini menggunakan lcd sebagai interface dan juga bisa dimonitor melalui aplikasi blynk. untuk lebih jelasnya berikut adalah koding dan komponennya.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
float pHValue;
float ntu;
int ntunew;
int adcntu;
int adcPH;
float T;
int h;
int golongan;
int dryValue = 957;
int wetValue = 56;
int friendlyDryValue = 0;
int friendlyWetValue = 100;
int rawValue;
float dingin, hangat, panas;
float sedikit, sedang, banyak;
float lb, sd, ke;
float hasillb, hasilsd, hasilke;
float Coglb, Cogsd, Cogke;
float Cogxlb, Cogxsd, Cogxke;
float Komlb, Komsd, Komke;
float Penlb, Pensd, Penke;
float Totalkom, Totalpen;
float Cog;
float lb1, lb2, lb3;
float sd1, sd2, sd3;
float ke1, ke2, ke3;
void setup()
{
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
}
void loop()
{
adcPH = analogRead(A0); //menggunakan pin A0 untuk membaca output sensor pH
pHValue = (adcPH - 1105.6)/ -34.4;
T = pHValue;
adcntu = analogRead(A1);
ntunew = map(adcntu, dryValue, wetValue, friendlyDryValue, friendlyWetValue);
//ntu = (adcntu - 912.5)/-0.279;
h = ntunew;
lcd.setCursor (0,0);
lcd.print ("N=");
lcd.print (h);
lcd.print (" ");
lcd.setCursor (0,1);
lcd.print ("P=");
lcd.print (T,1);
lcd.print (" ");
//======================
// fuzzyfikasi PH
//======================
//dingin = asam , hangat = netral , panas = basa
if(T <= 6){
dingin = 1;
hangat = 0;
panas = 0;
}
else if(T >= 8){
panas = 1;
hangat = 0;
dingin = 0;
}
else if(T == 7){
hangat = 1;
dingin = 0;
panas = 0;
}
else if((T > 6)&&(T < 7)){
hangat = (T - 6)/1;
dingin = 1-(T - 6)/1;
panas = 0;
}
else if((T > 7)&&(T < 8)){
panas = (T - 7)/1;
hangat = 1-(T - 7)/1;
dingin = 0;
}
// Serial.print("panas= ");
// Serial.println(panas);
// Serial.print("hangat= ");
// Serial.println(hangat);
// Serial.print("dingin= ");
// Serial.println(dingin);
//===============================
// fuzzyfikasi TDS/NTU
//===============================
//sedikit = rendah , sedang = sedang , banyak = tinggi
if(h <= 25){
sedikit = 1;
sedang = 0;
banyak = 0;
}
else if(h >= 50){
banyak = 1;
sedang = 0;
sedikit = 0;
}
else if(h == 37){
banyak = 0;
sedang = 1;
sedikit = 0;
}
else if((h > 25)&&(h < 37)){
sedang = (h - 25)/12;
sedikit = 1-(h - 25)/12;
banyak = 0;
}
else if((h > 37)&&(h < 50)){
sedang = (h - 37)/13;
banyak = 1-(h - 37)/13;
sedikit = 0;
}
// Serial.print("Sedikit= ");
// Serial.println(sedikit);
// Serial.print("sedang= ");
// Serial.println(sedang);
// Serial.print("banyak= ");
// Serial.println(banyak);
//==================================
// fuzzy rule
//===================================
// lb = GOL A , sd = GOL B , ke = GOL C
//===============================================================================
if((T <= 6 )&&(h <= 25)){ //1A
if(dingin < sedikit){
lb = dingin;
}
else if(dingin > sedikit){
lb = sedikit;
}
else if(dingin == sedikit){
lb = sedikit;
}
hasillb = lb;
hasilsd = 0;
hasilke = 0;
}
//=========================================================================
else if((T <= 6 )&&(h > 25)&&(h < 37)){ //1AB
if(dingin < sedikit){
lb1 = dingin;
}
else if(dingin > sedikit){
lb1 = sedikit;
}
else if(dingin == sedikit){
lb1 = sedikit;
}
if(dingin < sedang){
lb2 = dingin;
}
else if(dingin > sedang){
lb2 = sedang;
}
else if(dingin == sedang){
lb2 = sedang ;
}
if(lb1 < lb2){
hasillb = lb2;
}
else if(lb1 > lb2){
hasillb = lb1;
}
else if(lb1 == lb2){
hasillb = lb1;
}
hasilsd = 0;
hasilke = 0;
}
//========================================================================
else if((T <= 6 )&&(h > 37)&&(h < 50)){ //1BC
if(dingin < sedang){
lb = dingin;
}
else if(dingin > sedang){
lb = sedang;
}
else if(dingin == sedang){
lb = sedang;
}
if(dingin < banyak){
sd = dingin;
}
else if(dingin > banyak){
sd = banyak;
}
else if(dingin == banyak){
sd = banyak;
}
hasillb = lb;
hasilsd = sd;
hasilke = 0;
}
//=======================================================================
else if((T <= 6 )&&(h >= 50)){ //1C
if(dingin < banyak){
sd = dingin;
}
else if(dingin > banyak){
sd = banyak;
}
else if(dingin == banyak){
sd = banyak;
}
hasillb = 0;
hasilsd = sd;
hasilke = 0;
}
//==========================================================================
else if((T > 6 )&&(T < 7 )&&(h <= 25)){ //12A
if(dingin < sedikit){
lb1 = dingin;
}
else if(dingin > sedikit){
lb1 = sedikit;
}
else if(dingin == sedikit){
lb1 = sedikit;
}
if(hangat > sedikit){
lb2 = sedikit;
}
else if(hangat < sedikit){
lb2 = hangat;
}
else if(hangat == sedikit){
lb2 = hangat;
}
if(lb1 < lb2){
hasillb = lb2;
}
else if(lb1 > lb2){
hasillb = lb1;
}
else if(lb1 == lb2){
hasillb = lb1;
}
hasilsd = 0;
hasilke = 0;
}
//=========================================================================
else if((T > 6 )&&(T < 7 )&&(h > 25)&&(h < 37)){ //12AB
if(dingin < sedikit){
lb1 = dingin;
}
else if(dingin > sedikit){
lb1 = sedikit;
}
else if(dingin == sedikit){
lb1 = sedikit;
}
if(dingin < sedang){
lb2 = dingin;
}
else if(dingin > sedang){
lb2 = sedang;
}
else if(dingin == sedang){
lb2 = sedang;
}
if(hangat < sedikit){
lb3 = dingin;
}
else if(hangat > sedikit){
lb3 = sedikit;
}
else if(hangat == sedikit){
lb3 = sedikit;
}
if(hangat < sedang){
sd = hangat;
}
else if(hangat > sedang){
sd = sedang;
}
else if(hangat == sedang){
sd = sedang;
}
if((lb1 > lb2)&&(lb1 > lb3)){
hasillb = lb1;
}
else if((lb1 > lb2)&&(lb1 == lb3)){
hasillb = lb1;
}
else if((lb1 == lb2)&&(lb1 > lb3)){
hasillb = lb1;
}
else if((lb1 == lb2)&&(lb1 == lb3)){
hasillb = lb1;
}
else if((lb2 > lb1)&&(lb2 > lb3)){
hasillb = lb2;
}
else if((lb2 > lb1)&&(lb2 == lb3)){
hasillb = lb2;
}
else if((lb2 == lb1)&&(lb2 > lb3)){
hasillb = lb2;
}
else if((lb2 == lb1)&&(lb2 == lb3)){
hasillb = lb2;
}
else if((lb3 > lb1)&&(lb3 > lb2)){
hasillb = lb3;
}
else if((lb3 > lb1)&&(lb3 == lb2)){
hasillb = lb3;
}
else if((lb3 == lb1)&&(lb3 > lb2)){
hasillb = lb3;
}
else if((lb3 == lb1)&&(lb3 == lb2)){
hasillb = lb3;
}
hasilsd = sd;
hasilke = 0;
}
//===============================================================================
else if((T > 6)&&(T < 7)&&(h > 37)&&(h < 50)){ //12BC
if (dingin < sedang){
lb = dingin;
}
else if(dingin > sedang){
lb = sedang;
}
else if(dingin == sedang){
lb = sedang;
}
if(dingin < banyak){
sd1 = dingin;
}
else if(dingin > banyak){
sd1 = banyak;
}
else if(dingin == banyak){
sd1 = banyak;
}
if(hangat < sedang){
sd2 = hangat;
}
else if(hangat > sedang){
sd2 = sedang;
}
else if(hangat == sedang){
sd2 = sedang;
}
if(hangat < banyak){
ke = hangat;
}
else if(hangat > banyak){
ke = banyak;
}
else if(hangat == banyak){
ke = banyak;
}
if(sd1 > sd2){
hasilsd = sd1;
}
else if(sd2 > sd1){
hasilsd = sd2;
}
else if(sd2 == sd1){
hasilsd = sd2;
}
hasillb = lb;
hasilke = ke;
}
//=======================================================================
else if((T > 6)&&(T < 7)&&(h >= 50)){ //12C
if(dingin < banyak){
sd = dingin;
}
else if(dingin > banyak){
sd = banyak;
}
else if(dingin == banyak){
sd = banyak;
}
if(hangat > banyak){
ke = banyak;
}
else if(hangat < banyak){
ke = hangat;
}
else if(hangat == banyak){
ke = hangat;
}
hasilsd = sd;
hasilke = ke;
hasillb = 0;
}
//===============================================================
else if ((T == 7) && (h <= 25)){ //2A
if (hangat > sedikit){
lb = sedikit;
}
else if (hangat < sedikit){
lb = hangat;
}
else if (hangat == sedikit){
lb = hangat;
}
hasillb = lb;
hasilsd = 0;
hasilke = 0;
}
//============================================================
else if ((T == 7) && (h > 25) && (h < 37)){ //2AB
if (hangat < sedikit){
lb = hangat;
}
else if (hangat > sedikit){
lb = sedikit;
}
else if (hangat == sedikit){
lb = sedikit;
}
if (hangat < sedang){
sd = hangat;
}
else if (hangat > sedikit){
sd = sedikit;
}
else if (hangat == sedikit){
sd = sedikit;
}
hasillb = lb;
hasilsd = sd;
hasilke = 0;
}
//===================================================================
else if ((T == 7) && (h > 37) && (h < 50)){ //2BC
if (hangat < sedang){
sd = hangat;
}
else if (hangat > sedang){
sd = sedang;
}
else if (hangat == sedang){
sd = sedang;
}
if (hangat < banyak){
ke = hangat;
}
else if (hangat > banyak){
ke = banyak;
}
else if (hangat == banyak){
ke = banyak;
}
hasilsd = sd;
hasilke = ke;
hasillb = 0;
}
//=============================================================
else if ((T == 7) && (h >= 50)){ //2C
if (hangat < banyak){
ke = hangat;
}
else if (hangat > banyak){
ke = banyak;
}
else if (hangat == banyak){
ke = banyak;
}
hasilke = ke;
hasilsd = 0;
hasillb = 0;
}
//====================================================================
else if ((T > 7) && (T < 8) && (h <= 25)){ //23A
if (hangat < sedikit){
lb = hangat;
}
else if (hangat > sedikit){
lb = sedikit;
}
else if (hangat == sedikit){
lb = sedikit;
}
if (panas < sedikit){
sd = panas;
}
else if (panas > sedikit){
sd = sedikit;
}
else if (panas == sedikit){
sd = sedikit;
}
hasillb = lb;
hasilsd = sd;
hasilke = 0;
}
//=======================================================================
else if ((T > 7) && (T < 8) && (h > 25) && (h < 37)){ //23AB
if (hangat < sedikit){
lb = hangat;
}
else if (hangat > sedikit){
lb = sedikit;
}
else if (hangat == sedikit){
lb = sedikit;
}
if (hangat < sedang){
sd1 = hangat;
}
else if (hangat > sedang){
sd1 = sedang;
}
else if (hangat == sedang){
sd1 = sedang;
}
if (panas < sedikit){
sd2 = panas;
}
else if (panas > sedikit){
sd2 = sedikit;
}
else if (panas == sedikit){
sd2 = sedikit;
}
if (panas < sedang){
ke = panas;
}
else if (panas > sedang){
ke = sedang;
}
else if (panas == sedang){
ke = sedang;
}
if (sd1 < sd2){
hasilsd = sd2;
}
else if (sd1 > sd2){
hasilsd = sd1;
}
else if (sd1 == sd2){
hasilsd = sd1;
}
hasilke = ke;
hasillb = lb;
}
//=========================================================================
else if ((T > 7) && (T < 8) && (h > 37) && (h < 50)){ //23BC
if (hangat < sedang){
sd = hangat;
}
else if (hangat > sedang){
sd = sedang;
}
else if (hangat == sedang){
sd = sedang;
}
if (hangat < banyak){
ke1 = hangat;
}
else if (hangat > banyak){
ke1 = banyak;
}
else if (hangat == banyak){
ke1 = banyak;
}
if (panas < sedang){
ke2 = panas;
}
else if (panas > sedang){
ke2 = sedang;
}
else if (panas == sedang){
ke2 = sedang;
}
if (panas < banyak){
ke3 = panas;
}
else if (panas > banyak){
ke3 = banyak;
}
else if (panas == banyak){
ke3 = banyak;
}
if ((ke1 > ke2) && (ke1 > ke3)){
hasilke = ke1;
}
else if ((ke1 > ke2) && (ke1 == ke3)){
hasilke = ke1;
}
else if ((ke1 == ke2) && (ke1 > ke3)){
hasilke = ke1;
}
else if ((ke1 == ke2) && (ke1 == ke3)){
hasilke = ke1;
}
else if ((ke2 > ke1) && (ke2 > ke3)){
hasilke = ke2;
}
else if ((ke2 > ke1) && (ke2 == ke3)){
hasilke = ke2;
}
else if ((ke2 == ke1) && (ke2 > ke3)){
hasilke = ke2;
}
else if ((ke2 == ke1) && (ke2 == ke3)){
hasilke = ke2;
}
else if ((ke3 > ke1) && (ke3 > ke2)){
hasilke = ke3;
}
else if ((ke3 > ke1) && (ke3 == ke2)){
hasilke = ke3;
}
else if ((ke3 == ke1) && (ke3 > ke2)){
hasilke = ke3;
}
else if ((ke3 == ke1) && (ke3 == ke2)){
hasilke = ke3;
}
hasilsd = sd;
hasillb = 0;
}
//===========================================================================
else if ((T > 7) && (T < 8) && (h >= 50)){ //23C
if (hangat < banyak){
ke1 = hangat;
}
else if (hangat > banyak){
ke1 = banyak;
}
else if (hangat == banyak){
ke1 = banyak;
}
if (panas < banyak){
ke2 = panas;
}
else if (panas > banyak){
ke2 = banyak;
}
else if (panas == banyak){
ke2 = banyak;
}
if (ke1 < ke2){
hasilke = ke2;
}
else if (ke1 > ke2){
hasilke = ke1;
}
else if (ke1 == ke2){
hasilke = ke1;
}
hasilsd = 0;
hasillb = 0;
}
//===========================================================================
else if ((T >= 8) && (h <= 25)){ //3A
if (panas < sedikit){
sd = panas;
}
else if (panas > sedikit){
sd = sedikit;
}
else if (panas == sedikit){
sd = sedikit;
}
hasilsd = sd;
hasillb = 0;
hasilke = 0;
}
//============================================================================
else if ((T >= 8) && (h > 25) && (h < 37)){ //3AB
if (panas < sedikit){
sd = panas;
}
else if (panas > sedikit){
sd = sedikit;
}
else if (panas == sedikit){
sd = sedikit;
}
if (panas < sedang){
ke = panas;
}
else if (panas > sedang){
ke = sedang;
}
else if (panas == sedang){
ke = sedang;
}
hasilsd = sd;
hasilke = ke;
hasillb = 0;
}
//======================================================================
else if ((T >= 8) && (h > 37) && (h < 50)){ //3BC
if (panas < sedang){
ke1 = panas;
}
else if (panas > sedang){
ke1 = sedang;
}
else if (panas == sedang){
ke1 = sedang;
}
if (panas < banyak){
ke2 = panas;
}
else if (panas > banyak){
ke2 = banyak;
}
else if (panas == banyak){
ke2 = banyak;
}
if (ke1 < ke2){
hasilke = ke2;
}
else if (ke1 > ke2){
hasilke = ke1;
}
else if (ke1 == ke2){
hasilke = ke1;
}
hasillb = 0;
hasilsd = 0;
}
//=========================================================================
else if ((T >= 8) && (h >= 50)){ //3C
if (panas < banyak){
ke = panas;
}
else if (panas > banyak){
ke = banyak;
}
else if (panas == banyak){
ke = banyak;
}
hasilke = ke;
hasilsd = 0;
hasillb = 0;
}
//======================================================================
else if ((T <= 6) && (h == 37)){ //1B
if (dingin < sedang){
lb = dingin;
}
else if (dingin > sedang){
lb = sedang;
}
else if (dingin == sedang){
lb = sedang;
}
hasillb = lb;
hasilsd = 0;
hasilke = 0;
}
//===================================================================
else if ((T > 6) && (T < 7) && (h == 37)){ //12B
if (dingin < sedang){
lb = dingin;
}
else if (dingin > sedang){
lb = sedang;
}
else if (dingin == sedang){
lb = sedang;
}
if (hangat < sedang){
sd = hangat;
}
else if (hangat > sedang){
sd = sedang;
}
else if (hangat == sedang){
sd = sedang;
}
hasillb = lb;
hasilsd = sd;
hasilke = 0;
}
//==========================================================
else if ((T == 7) && (h == 37)){ //2B
if (hangat < sedang){
sd = hangat;
}
else if (hangat > sedang){
sd = sedang;
}
else if (hangat == sedang){
sd = sedang;
}
hasilsd = sd;
hasillb = 0;
hasilke = 0;
}
//================================================================
else if ((T > 7) && (T < 8) && (h == 37)){ //23B
if (hangat < sedang){
sd = hangat;
}
else if (hangat > sedang){
sd = sedang;
}
else if (hangat == sedang){
sd = sedang;
}
if (panas < sedang){
ke = panas;
}
else if (panas > sedang){
ke = sedang;
}
else if (panas == sedang){
ke = sedang;
}
hasilsd = sd;
hasilke = ke;
hasillb = 0;
}
//================================================================
else if ((T >= 8) && (h == 37)){ //3B
if (panas < sedang){
ke = panas;
}
else if (panas > sedang){
ke = sedang;
}
else if (panas == sedang){
ke = sedang;
}
hasilke = ke;
hasilsd = 0;
hasillb = 0;
}
//==================================================
// Defuzzifikasi
//==================================================
//0-10-20-30
Coglb = 60;
Cogxlb = 4;
//40-50-60-70
Cogsd = 220;
Cogxsd = 4;
//80-90-100-110-120
Cogke = 500;
Cogxke = 5;
/*
//0-10-20-30-40-50
Coglb = 150;
Cogxlb = 6;
//60-70-80-90-100-110-120
Cogsd = 630;
Cogxsd = 7;
//130-140-150-160-170-180-190-200
Cogke = 1320;
Cogxke = 8;
*/
//Komlb = Coglb * hasillb;
//Komsd = Cogsd * hasilsd;
//Komke = Cogke * hasilke;
//Totalkom = Komlb + Komsd;
//Totalkom = Totalkom + Komke;
//Penlb = hasillb * Cogxlb;
//Pensd = hasilsd * Cogxsd;
//Penke = hasilke * Cogxke;
//Totalpen = Penlb + Pensd;
//Totalpen = Totalpen + Penke;
Totalkom = (Coglb * hasillb) + (Cogsd * hasilsd) + (Cogke * hasilke);
Totalpen = (hasillb * Cogxlb) + (hasilsd * Cogxsd) + (hasilke * Cogxke);
Cog = Totalkom / Totalpen;
//Serial.print("Totalkom= ");
//Serial.println(Totalkom);
//Serial.print("Totalpen= ");
//Serial.println(Totalpen);
//Serial.print("COG= ");
//Serial.println(Cog);
lcd.setCursor (9,0);
lcd.print ("C=");
lcd.print (Cog,0);
lcd.print (" ");
if(Cog < 50){
lcd.setCursor (9,1);
lcd.print ("GOL A");
golongan = 1;
}
if((Cog >= 50)&&(Cog < 80)){
lcd.setCursor (9,1);
lcd.print ("GOL B");
golongan = 2;
}
if(Cog >= 80){
lcd.setCursor (9,1);
lcd.print ("GOL C");
golongan = 3;
}
Serial.print("*");
Serial.print(T);
Serial.print(",");
Serial.print(h*100.0);
Serial.print(",");
Serial.print(Cog);
Serial.print(",");
Serial.print(golongan);
Serial.println("#");
delay(200);
}