Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang bisa digunakan untuk monitor gas dan asap dengan menggunakan metode fuzzy logic mamdani, alat ini menggunakan 4 buah sensor sebagi inputnya dan outputnya menggunakan relay. untuk lebih jelasnya berikut adalah komponen dan kodingnya.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
float dinginx, hangatx, panasx;
float sedikitx, sedangx, banyakx;
float lbx, sdx, kex;
float hasillbx, hasilsdx, hasilkex;
float Coglbx, Cogsdx, Cogkex;
float Cogxlbx, Cogxsdx, Cogxkex;
float Komlbx, Komsdx, Komkex;
float Penlbx, Pensdx, Penkex;
float lb1x, lb2x, lb3x;
float sd1x, sd2x, sd3x;
float ke1x, ke2x, ke3x;
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 Totalkomx, Totalpenx;
float Cog1,Cog2,Cogt;
float lb1, lb2, lb3;
float sd1, sd2, sd3;
float ke1, ke2, ke3;
float adc1mq135;
float adc2mq2;
float adc3mq135;
float adc4mq2;
int statusarea = 0;
int pompa = 0;
int valve = 0;
int doorlock = 0;
//pompa
int relay1 = 22;
//doorlock
int relay2 = 24;
//solenoid1
int relay3 = 26;
//solenoid2
int relay4 = 28;
int led = 30;
int buzzer = 49;
void setup()
{
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
pinMode(buzzer,OUTPUT);
pinMode(led,OUTPUT);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(relay4,OUTPUT);
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(led,LOW);
digitalWrite(buzzer,LOW);
}
void loop()
{
adc1mq135 = analogRead(A0);
adc2mq2 = analogRead(A1);
delay(200);
//===============================
// fuzzyfikasi gas 1 MQ135
//===============================
if(adc1mq135 <= 150){
sedikit = 1;
sedang = 0;
banyak = 0;
}
else if(adc1mq135 >= 300){
banyak = 1;
sedang = 0;
sedikit = 0;
}
else if(adc1mq135 == 200){
banyak = 0;
sedang = 1;
sedikit = 0;
}
else if((adc1mq135 > 150)&&(adc1mq135 < 200)){
sedang = (adc1mq135 - 150)/50;
sedikit = 1-(adc1mq135 - 150)/50;
banyak = 0;
}
else if((adc1mq135 > 200)&&(adc1mq135 < 300)){
sedang = (adc1mq135 - 200)/100;
banyak = 1-(adc1mq135 - 200)/100;
sedikit = 0;
}
/*
Serial.print("Sedikit= ");
Serial.println(sedikit);
Serial.print("sedang= ");
Serial.println(sedang);
Serial.print("banyak= ");
Serial.println(banyak);
*/
//===============================
// fuzzyfikasi gas 2 MQ2
//===============================
if(adc2mq2 <= 150){
dingin = 1;
hangat = 0;
panas = 0;
}
else if(adc2mq2 >= 300){
dingin = 0;
hangat = 0;
panas = 1;
}
else if(adc2mq2 == 200){
dingin = 0;
hangat = 1;
panas = 0;
}
else if((adc2mq2 > 150)&&(adc2mq2 < 200)){
hangat = (adc2mq2 - 150)/50;
dingin = 1-(adc2mq2 - 150)/50;
panas = 0;
}
else if((adc2mq2 > 200)&&(adc2mq2 < 300)){
hangat = (adc2mq2 - 200)/100;
panas = 1-(adc2mq2 - 200)/100;
dingin = 0;
}
/*
Serial.print("Sedikit= ");
Serial.println(sedikit);
Serial.print("sedang= ");
Serial.println(sedang);
Serial.print("banyak= ");
Serial.println(banyak);
*/
//==================================
// fuzzy rule 1
//===================================
//============================================================================
if((adc1mq135 <= 150 )&&(adc2mq2 <= 150)){ //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((adc1mq135 <= 150 )&&(adc2mq2 > 150)&&(adc2mq2 < 200)){ //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((adc1mq135 <= 150 )&&(adc2mq2 > 200)&&(adc2mq2 < 300)){ //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((adc1mq135 <= 150 )&&(adc2mq2 >= 300)){ //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((adc1mq135 > 150 )&&(adc1mq135 < 200 )&&(adc2mq2 <= 150)){ //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((adc1mq135 > 150 )&&(adc1mq135 < 200 )&&(adc2mq2 > 150)&&(adc2mq2 < 200)){ //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((adc1mq135 > 150)&&(adc1mq135 < 200)&&(adc2mq2 > 200)&&(adc2mq2 < 300)){ //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((adc1mq135 > 150)&&(adc1mq135 < 200)&&(adc2mq2 >= 300)){ //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 ((adc1mq135 == 200) && (adc2mq2 <= 150)){ //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 ((adc1mq135 == 200) && (adc2mq2 > 150) && (adc2mq2 < 200)){ //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 ((adc1mq135 == 200) && (adc2mq2 > 200) && (adc2mq2 < 300)){ //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 ((adc1mq135 == 200) && (adc2mq2 >= 300)){ //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 ((adc1mq135 > 200) && (adc1mq135 < 300) && (adc2mq2 <= 150)){ //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 ((adc1mq135 > 200) && (adc1mq135 < 300) && (adc2mq2 > 150) && (adc2mq2 < 200)){ //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 ((adc1mq135 > 200) && (adc1mq135 < 300) && (adc2mq2 > 200) && (adc2mq2 < 300)){ //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 ((adc1mq135 > 200) && (adc1mq135 < 300) && (adc2mq2 >= 300)){ //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 ((adc1mq135 >= 300) && (adc2mq2 <= 150)){ //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 ((adc1mq135 >= 300) && (adc2mq2 > 150) && (adc2mq2 < 200)){ //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 ((adc1mq135 >= 300) && (adc2mq2 > 200) && (adc2mq2 < 300)){ //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 ((adc1mq135 >= 300) && (adc2mq2 >= 300)){ //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 ((adc1mq135 <= 150) && (adc2mq2 == 200)){ //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 ((adc1mq135 > 150) && (adc1mq135 < 200) && (adc2mq2 == 200)){ //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 ((adc1mq135 == 200) && (adc2mq2 == 200)){ //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 ((adc1mq135 > 200) && (adc1mq135 < 300) && (adc2mq2 == 200)){ //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 ((adc1mq135 >= 300) && (adc2mq2 == 200)){ //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 1
//==================================================
//0-10-20-200
Coglb = 60;
Cogxlb = 4;
//40-50-60-70
Cogsd = 220;
Cogxsd = 4;
//80-90-100-110-120
Cogke = 500;
Cogxke = 5;
Totalkom = (Coglb * hasillb) + (Cogsd * hasilsd) + (Cogke * hasilke);
Totalpen = (hasillb * Cogxlb) + (hasilsd * Cogxsd) + (hasilke * Cogxke);
Cog1 = Totalkom / Totalpen;
/*
Serial.print("hasillb= ");
Serial.println(hasillb);
Serial.print("hasilsd= ");
Serial.println(hasilsd);
Serial.print("hasilke= ");
Serial.println(hasilke);
Serial.print("Totalkom= ");
Serial.println(Totalkom);
Serial.print("Totalpen= ");
Serial.println(Totalpen);
Serial.print("COG= ");
Serial.println(Cog1);
*/
lcd.setCursor(0,0);
lcd.print("M135=");
lcd.print(adc1mq135,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("MQ02=");
lcd.print(adc2mq2,0);
lcd.print(" ");
//lcd.setCursor (10,0);
//lcd.print ("COG1=");
//lcd.print (Cog1,1);
//lcd.print (" ");
delay(100);
//=======================================================================================================================================
adc3mq135 = analogRead(A2);
adc4mq2 = analogRead(A3);
//===============================
// fuzzyfikasi gas 3 MQ135
//===============================
if(adc3mq135 <= 150){
sedikitx = 1;
sedangx = 0;
banyakx = 0;
}
else if(adc3mq135 >= 300){
banyakx = 1;
sedangx = 0;
sedikitx = 0;
}
else if(adc3mq135 == 200){
banyakx = 0;
sedangx = 1;
sedikitx = 0;
}
else if((adc3mq135 > 150)&&(adc3mq135 < 200)){
sedangx = (adc3mq135 - 150)/50;
sedikitx = 1-(adc3mq135 - 150)/50;
banyakx = 0;
}
else if((adc3mq135 > 200)&&(adc3mq135 < 300)){
sedangx = (adc3mq135 - 200)/100;
banyakx = 1-(adc3mq135 - 200)/100;
sedikitx = 0;
}
// Serial.print("Sedikit= ");
// Serial.println(sedikit);
// Serial.print("sedang= ");
// Serial.println(sedang);
// Serial.print("banyak= ");
// Serial.println(banyak);
//===============================
// fuzzyfikasi gas 4 MQ2
//===============================
if(adc4mq2 <= 150){
dinginx = 1;
hangatx = 0;
panasx = 0;
}
else if(adc4mq2 >= 300){
panasx = 1;
hangatx = 0;
dinginx = 0;
}
else if(adc4mq2 == 200){
panasx = 0;
hangatx = 1;
dinginx = 0;
}
else if((adc4mq2 > 150)&&(adc4mq2 < 200)){
hangatx = (adc4mq2 - 150)/50;
dinginx = 1-(adc4mq2 - 150)/50;
panasx = 0;
}
else if((adc4mq2 > 200)&&(adc4mq2 < 300)){
hangatx = (adc4mq2 - 200)/100;
panasx = 1-(adc4mq2 - 200)/100;
dinginx = 0;
}
// Serial.print("Sedikit= ");
// Serial.println(sedikit);
// Serial.print("sedang= ");
// Serial.println(sedang);
// Serial.print("banyak= ");
// Serial.println(banyak);
//==================================
// fuzzy rule 2
//===================================
//============================================================================
if((adc3mq135 <= 150 )&&(adc4mq2 <= 150)){ //1A
if(dinginx < sedikitx){
lbx = dinginx;
}
else if(dinginx > sedikitx){
lbx = sedikitx;
}
else if(dinginx == sedikitx){
lbx = sedikitx;
}
hasillbx = lbx;
hasilsdx = 0;
hasilkex = 0;
}
//=========================================================================
else if((adc3mq135 <= 150 )&&(adc4mq2 > 150)&&(adc4mq2 < 200)){ //1AB
if(dinginx < sedikitx){
lb1x = dinginx;
}
else if(dinginx > sedikitx){
lb1x = sedikitx;
}
else if(dinginx == sedikitx){
lb1x = sedikitx;
}
if(dinginx < sedangx){
lb2x = dinginx;
}
else if(dinginx > sedangx){
lb2x = sedangx;
}
else if(dinginx == sedangx){
lb2x = sedangx ;
}
if(lb1x < lb2x){
hasillbx = lb2x;
}
else if(lb1x > lb2x){
hasillbx = lb1x;
}
else if(lb1x == lb2x){
hasillbx = lb1x;
}
hasilsdx = 0;
hasilkex = 0;
}
//========================================================================
else if((adc3mq135 <= 150 )&&(adc4mq2 > 200)&&(adc4mq2 < 300)){ //1BC
if(dinginx < sedangx){
lbx = dinginx;
}
else if(dinginx > sedangx){
lbx = sedangx;
}
else if(dinginx == sedangx){
lbx = sedangx;
}
if(dinginx < banyak){
sdx = dinginx;
}
else if(dinginx > banyak){
sdx = banyak;
}
else if(dinginx == banyak){
sdx = banyak;
}
hasillbx = lbx;
hasilsdx = sdx;
hasilkex = 0;
}
//=======================================================================
else if((adc3mq135 <= 150 )&&(adc4mq2 >= 300)){ //1C
if(dinginx < banyak){
sdx = dinginx;
}
else if(dinginx > banyak){
sdx = banyakx;
}
else if(dinginx == banyakx){
sdx = banyakx;
}
hasillbx = 0;
hasilsdx = sdx;
hasilkex = 0;
}
//==========================================================================
else if((adc3mq135 > 150 )&&(adc3mq135 < 200 )&&(adc4mq2 <= 150)){ //12A
if(dinginx < sedikitx){
lb1x = dinginx;
}
else if(dinginx > sedikitx){
lb1x = sedikitx;
}
else if(dinginx == sedikitx){
lb1x = sedikitx;
}
if(hangatx > sedikitx){
lb2x = sedikitx;
}
else if(hangatx < sedikitx){
lb2x = hangatx;
}
else if(hangatx == sedikitx){
lb2x = hangatx;
}
if(lb1x < lb2x){
hasillbx = lb2x;
}
else if(lb1x > lb2x){
hasillbx = lb1x;
}
else if(lb1x == lb2x){
hasillbx = lb1x;
}
hasilsdx = 0;
hasilkex = 0;
}
//=========================================================================
else if((adc3mq135 > 150 )&&(adc3mq135 < 200 )&&(adc4mq2 > 150)&&(adc4mq2 < 200)){ //12AB
if(dinginx < sedikitx){
lb1x = dinginx;
}
else if(dinginx > sedikitx){
lb1x = sedikitx;
}
else if(dinginx == sedikitx){
lb1x = sedikitx;
}
if(dinginx < sedangx){
lb2x = dinginx;
}
else if(dinginx > sedangx){
lb2x = sedangx;
}
else if(dinginx == sedangx){
lb2x = sedangx;
}
if(hangatx < sedikitx){
lb3x = dinginx;
}
else if(hangatx > sedikitx){
lb3x = sedikitx;
}
else if(hangatx == sedikitx){
lb3x = sedikitx;
}
if(hangatx < sedangx){
sdx = hangatx;
}
else if(hangatx > sedangx){
sdx = sedangx;
}
else if(hangatx == sedangx){
sdx = sedangx;
}
if((lb1x > lb2x)&&(lb1x > lb3x)){
hasillbx = lb1x;
}
else if((lb1x > lb2x)&&(lb1x == lb3x)){
hasillbx = lb1x;
}
else if((lb1x == lb2x)&&(lb1x > lb3x)){
hasillbx = lb1x;
}
else if((lb1x == lb2x)&&(lb1x == lb3x)){
hasillbx = lb1x;
}
else if((lb2x > lb1x)&&(lb2x > lb3x)){
hasillbx = lb2x;
}
else if((lb2x > lb1x)&&(lb2x == lb3x)){
hasillbx = lb2x;
}
else if((lb2x == lb1x)&&(lb2x > lb3x)){
hasillbx = lb2x;
}
else if((lb2x == lb1x)&&(lb2x == lb3x)){
hasillbx = lb2x;
}
else if((lb3x > lb1x)&&(lb3x > lb2x)){
hasillbx = lb3x;
}
else if((lb3x > lb1x)&&(lb3x == lb2x)){
hasillbx = lb3x;
}
else if((lb3x == lb1x)&&(lb3x > lb2x)){
hasillbx = lb3x;
}
else if((lb3x == lb1x)&&(lb3x == lb2x)){
hasillbx = lb3x;
}
hasilsdx = sdx;
hasilkex = 0;
}
//===============================================================================
else if((adc3mq135 > 150)&&(adc3mq135 < 200)&&(adc4mq2 > 200)&&(adc4mq2 < 300)){ //12BC
if (dinginx < sedangx){
lbx = dinginx;
}
else if(dinginx > sedangx){
lbx = sedangx;
}
else if(dinginx == sedangx){
lbx = sedangx;
}
if(dinginx < banyakx){
sd1x = dinginx;
}
else if(dinginx > banyakx){
sd1x = banyakx;
}
else if(dinginx == banyakx){
sd1x = banyakx;
}
if(hangatx < sedangx){
sd2x = hangatx;
}
else if(hangatx > sedangx){
sd2x = sedangx;
}
else if(hangatx == sedangx){
sd2x = sedangx;
}
if(hangatx < banyakx){
kex = hangatx;
}
else if(hangatx > banyakx){
kex = banyakx;
}
else if(hangatx == banyakx){
kex = banyakx;
}
if(sd1x > sd2x){
hasilsdx = sd1x;
}
else if(sd2x > sd1x){
hasilsdx = sd2x;
}
else if(sd2x == sd1x){
hasilsdx = sd2x;
}
hasillbx = lbx;
hasilkex = kex;
}
//=======================================================================
else if((adc3mq135 > 150)&&(adc3mq135 < 200)&&(adc4mq2 >= 300)){ //12C
if(dinginx < banyakx){
sdx = dinginx;
}
else if(dinginx > banyakx){
sdx = banyakx;
}
else if(dinginx == banyakx){
sdx = banyakx;
}
if(hangatx > banyakx){
kex = banyakx;
}
else if(hangatx < banyakx){
kex = hangatx;
}
else if(hangatx == banyakx){
kex = hangatx;
}
hasilsdx = sdx;
hasilkex = kex;
hasillbx = 0;
}
//===============================================================
else if ((adc3mq135 == 200) && (adc4mq2 <= 150)){ //2A
if (hangatx > sedikitx){
lbx = sedikitx;
}
else if (hangatx < sedikitx){
lbx = hangatx;
}
else if (hangatx == sedikitx){
lbx = hangatx;
}
hasillbx = lbx;
hasilsdx = 0;
hasilkex = 0;
}
//============================================================
else if ((adc3mq135 == 200) && (adc4mq2 > 150) && (adc4mq2 < 200)){ //2AB
if (hangatx < sedikitx){
lbx = hangatx;
}
else if (hangatx > sedikitx){
lbx = sedikitx;
}
else if (hangatx == sedikitx){
lbx = sedikitx;
}
if (hangatx < sedangx){
sdx = hangatx;
}
else if (hangatx > sedikitx){
sdx = sedikitx;
}
else if (hangatx == sedikitx){
sdx = sedikitx;
}
hasillbx = lbx;
hasilsdx = sdx;
hasilkex = 0;
}
//===================================================================
else if ((adc3mq135 == 200) && (adc4mq2 > 200) && (adc4mq2 < 300)){ //2BC
if (hangatx < sedangx){
sdx = hangatx;
}
else if (hangatx > sedangx){
sdx = sedangx;
}
else if (hangatx == sedangx){
sdx = sedangx;
}
if (hangatx < banyakx){
kex = hangatx;
}
else if (hangatx > banyakx){
kex = banyakx;
}
else if (hangatx == banyakx){
kex = banyakx;
}
hasilsdx = sdx;
hasilkex = kex;
hasillbx = 0;
}
//=============================================================
else if ((adc3mq135 == 200) && (adc4mq2 >= 300)){ //2C
if (hangatx < banyakx){
kex = hangatx;
}
else if (hangatx > banyakx){
kex = banyakx;
}
else if (hangatx == banyakx){
kex = banyakx;
}
hasilkex = kex;
hasilsdx = 0;
hasillbx = 0;
}
//====================================================================
else if ((adc3mq135 > 200) && (adc3mq135 < 300) && (adc4mq2 <= 150)){ //23A
if (hangatx < sedikitx){
lbx = hangatx;
}
else if (hangatx > sedikitx){
lbx = sedikitx;
}
else if (hangatx == sedikitx){
lbx = sedikitx;
}
if (panasx < sedikitx){
sdx = panasx;
}
else if (panasx > sedikitx){
sdx = sedikitx;
}
else if (panasx == sedikitx){
sdx = sedikitx;
}
hasillbx = lbx;
hasilsdx = sdx;
hasilkex = 0;
}
//=======================================================================
else if ((adc3mq135 > 200) && (adc3mq135 < 300) && (adc4mq2 > 150) && (adc4mq2 < 200)){ //23AB
if (hangatx < sedikitx){
lbx = hangatx;
}
else if (hangatx > sedikitx){
lbx = sedikitx;
}
else if (hangatx == sedikitx){
lbx = sedikitx;
}
if (hangatx < sedangx){
sd1x = hangatx;
}
else if (hangatx > sedangx){
sd1x = sedangx;
}
else if (hangatx == sedangx){
sd1x = sedangx;
}
if (panasx < sedikitx){
sd2x = panasx;
}
else if (panasx > sedikitx){
sd2x = sedikitx;
}
else if (panasx == sedikitx){
sd2x = sedikitx;
}
if (panasx < sedangx){
kex = panasx;
}
else if (panasx > sedangx){
kex = sedangx;
}
else if (panasx == sedangx){
kex = sedangx;
}
if (sd1x < sd2x){
hasilsdx = sd2x;
}
else if (sd1x > sd2x){
hasilsdx = sd1x;
}
else if (sd1x == sd2x){
hasilsdx = sd1x;
}
hasilkex = kex;
hasillbx = lbx;
}
//=========================================================================
else if ((adc3mq135 > 200) && (adc3mq135 < 300) && (adc4mq2 > 200) && (adc4mq2 < 300)){ //23BC
if (hangatx < sedangx){
sdx = hangatx;
}
else if (hangatx > sedangx){
sdx = sedangx;
}
else if (hangatx == sedangx){
sdx = sedangx;
}
if (hangatx < banyakx){
ke1x = hangatx;
}
else if (hangatx > banyakx){
ke1x = banyakx;
}
else if (hangatx == banyakx){
ke1x = banyakx;
}
if (panasx < sedangx){
ke2x = panasx;
}
else if (panasx > sedangx){
ke2x = sedangx;
}
else if (panasx == sedangx){
ke2x = sedangx;
}
if (panasx < banyakx){
ke3x = panasx;
}
else if (panasx > banyakx){
ke3x = banyakx;
}
else if (panasx == banyakx){
ke3x = banyakx;
}
if ((ke1x > ke2x) && (ke1x > ke3x)){
hasilkex = ke1x;
}
else if ((ke1x > ke2x) && (ke1x == ke3x)){
hasilkex = ke1x;
}
else if ((ke1x == ke2x) && (ke1x > ke3x)){
hasilkex = ke1x;
}
else if ((ke1x == ke2x) && (ke1x == ke3x)){
hasilkex = ke1x;
}
else if ((ke2x > ke1x) && (ke2x > ke3x)){
hasilkex = ke2x;
}
else if ((ke2x > ke1x) && (ke2x == ke3x)){
hasilkex = ke2x;
}
else if ((ke2x == ke1x) && (ke2x > ke3x)){
hasilkex = ke2x;
}
else if ((ke2x == ke1x) && (ke2x == ke3x)){
hasilkex = ke2x;
}
else if ((ke3x > ke1x) && (ke3x > ke2x)){
hasilkex = ke3x;
}
else if ((ke3x > ke1x) && (ke3x == ke2x)){
hasilkex = ke3x;
}
else if ((ke3x == ke1x) && (ke3x > ke2x)){
hasilkex = ke3x;
}
else if ((ke3x == ke1x) && (ke3x == ke2x)){
hasilkex = ke3x;
}
hasilsdx = sdx;
hasillbx = 0;
}
//===========================================================================
else if ((adc3mq135 > 200) && (adc3mq135 < 300) && (adc4mq2 >= 300)){ //23C
if (hangatx < banyakx){
ke1x = hangatx;
}
else if (hangatx > banyakx){
ke1x = banyakx;
}
else if (hangatx == banyakx){
ke1x = banyakx;
}
if (panasx < banyakx){
ke2x = panasx;
}
else if (panasx > banyakx){
ke2x = banyakx;
}
else if (panasx == banyakx){
ke2x = banyakx;
}
if (ke1x < ke2x){
hasilkex = ke2x;
}
else if (ke1x > ke2x){
hasilkex = ke1x;
}
else if (ke1x == ke2x){
hasilkex = ke1x;
}
hasilsdx = 0;
hasillbx = 0;
}
//===========================================================================
else if ((adc3mq135 >= 300) && (adc4mq2 <= 150)){ //3A
if (panasx < sedikitx){
sdx = panasx;
}
else if (panasx > sedikitx){
sdx = sedikitx;
}
else if (panasx == sedikitx){
sdx = sedikitx;
}
hasilsdx = sdx;
hasillbx = 0;
hasilkex = 0;
}
//============================================================================
else if ((adc3mq135 >= 300) && (adc4mq2 > 150) && (adc4mq2 < 200)){ //3AB
if (panasx < sedikitx){
sdx = panasx;
}
else if (panasx > sedikitx){
sdx = sedikitx;
}
else if (panasx == sedikitx){
sdx = sedikitx;
}
if (panasx < sedangx){
kex = panasx;
}
else if (panasx > sedangx){
kex = sedangx;
}
else if (panasx == sedangx){
kex = sedangx;
}
hasilsdx = sdx;
hasilkex = kex;
hasillbx = 0;
}
//======================================================================
else if ((adc3mq135 >= 300) && (adc4mq2 > 200) && (adc4mq2 < 300)){ //3BC
if (panasx < sedangx){
ke1x = panasx;
}
else if (panasx > sedangx){
ke1x = sedangx;
}
else if (panasx == sedangx){
ke1x = sedangx;
}
if (panasx < banyakx){
ke2x = panasx;
}
else if (panasx > banyakx){
ke2x = banyakx;
}
else if (panasx == banyakx){
ke2x = banyakx;
}
if (ke1x < ke2x){
hasilkex = ke2x;
}
else if (ke1x > ke2x){
hasilkex = ke1x;
}
else if (ke1x == ke2x){
hasilkex = ke1x;
}
hasillbx = 0;
hasilsdx = 0;
}
//=========================================================================
else if ((adc3mq135 >= 300) && (adc4mq2 >= 300)){ //3C
if (panasx < banyakx){
kex = panasx;
}
else if (panasx > banyakx){
kex = banyakx;
}
else if (panasx == banyakx){
kex = banyakx;
}
hasilkex = kex;
hasilsdx = 0;
hasillbx = 0;
}
//======================================================================
else if ((adc3mq135 <= 150) && (adc4mq2 == 200)){ //1B
if (dinginx < sedangx){
lbx = dinginx;
}
else if (dinginx > sedangx){
lbx = sedangx;
}
else if (dinginx == sedangx){
lbx = sedangx;
}
hasillbx = lbx;
hasilsdx = 0;
hasilkex = 0;
}
//===================================================================
else if ((adc3mq135 > 150) && (adc3mq135 < 200) && (adc4mq2 == 200)){ //12B
if (dinginx < sedangx){
lbx = dinginx;
}
else if (dinginx > sedangx){
lbx = sedangx;
}
else if (dinginx == sedangx){
lbx = sedangx;
}
if (hangatx < sedangx){
sdx = hangatx;
}
else if (hangatx > sedangx){
sdx = sedangx;
}
else if (hangatx == sedangx){
sdx = sedangx;
}
hasillbx = lbx;
hasilsdx = sdx;
hasilkex = 0;
}
//==========================================================
else if ((adc3mq135 == 200) && (adc4mq2 == 200)){ //2B
if (hangatx < sedangx){
sdx = hangatx;
}
else if (hangatx > sedangx){
sdx = sedangx;
}
else if (hangatx == sedangx){
sdx = sedangx;
}
hasilsdx = sdx;
hasillbx = 0;
hasilkex = 0;
}
//================================================================
else if ((adc3mq135 > 200) && (adc3mq135 < 300) && (adc4mq2 == 200)){ //23B
if (hangatx < sedangx){
sdx = hangatx;
}
else if (hangatx > sedangx){
sdx = sedangx;
}
else if (hangatx == sedangx){
sdx = sedangx;
}
if (panasx < sedangx){
kex = panasx;
}
else if (panasx > sedangx){
kex = sedangx;
}
else if (panasx == sedangx){
kex = sedangx;
}
hasilsdx = sdx;
hasilkex = kex;
hasillbx = 0;
}
//================================================================
else if ((adc3mq135 >= 300) && (adc4mq2 == 200)){ //3B
if (panasx < sedangx){
kex = panasx;
}
else if (panasx > sedangx){
kex = sedangx;
}
else if (panasx == sedangx){
kex = sedangx;
}
hasilkex = kex;
hasilsdx = 0;
hasillbx = 0;
}
//==================================================
// Defuzzifikasi 2
//==================================================
//0-10-20-200
Coglbx = 60;
Cogxlbx = 4;
//40-50-60-70
Cogsdx = 220;
Cogxsdx = 4;
//80-90-100-110-120
Cogkex = 500;
Cogxkex = 5;
Totalkomx = (Coglbx * hasillbx) + (Cogsdx * hasilsdx) + (Cogkex * hasilkex);
Totalpenx = (hasillbx * Cogxlbx) + (hasilsdx * Cogxsdx) + (hasilkex * Cogxkex);
Cog2 = Totalkomx / Totalpenx;
Cogt = (Cog1 + Cog2)/2;
//Serial.print("Totalkom= ");
//Serial.println(Totalkom);
//Serial.print("Totalpen= ");
//Serial.println(Totalpen);
//Serial.print("COG= ");
//Serial.println(Cog);
lcd.setCursor(0,2);
lcd.print("M135=");
lcd.print(adc3mq135,0);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print("MQ02=");
lcd.print(adc4mq2,0);
lcd.print(" ");
//lcd.setCursor (10,1);
//lcd.print("COG2=");
//lcd.print(Cog2,1);
//lcd.print(" ");
lcd.setCursor (10,0);
lcd.print("COG=");
lcd.print(Cogt,1);
lcd.print(" ");
//jika terdeteksi gas
if(Cogt > 99){
digitalWrite(relay1,LOW); //pompa aktif
digitalWrite(relay2,LOW); //doorlock
digitalWrite(relay3,LOW); //solenoid
digitalWrite(buzzer,HIGH);
digitalWrite(led,HIGH);
lcd.setCursor (10,1);
lcd.print("WASPADAGAS");
statusarea = 1;
pompa = 1;
valve = 1;
doorlock = 1;
}
//jika tidak terdeteksi gas
if(Cogt <= 99){
digitalWrite(relay1,HIGH); //pompa aktif
digitalWrite(relay2,HIGH); //doorlock
digitalWrite(relay3,HIGH); //solenoid
digitalWrite(buzzer,LOW);
digitalWrite(led,LOW);
lcd.setCursor (10,1);
lcd.print("AREA AMAN ");
statusarea = 0;
pompa = 0;
valve = 0;
doorlock = 0;
}
Serial.print("*");
Serial.print(adc1mq135*1);
Serial.print(",");
Serial.print(adc2mq2*1);
Serial.print(",");
Serial.print(adc3mq135*1);
Serial.print(",");
Serial.print(adc4mq2*1);
Serial.print(",");
Serial.print(Cogt*100.0);
Serial.print(",");
Serial.print(statusarea*1);
Serial.print(",");
Serial.print(pompa*1);
Serial.print(",");
Serial.print(valve*1);
Serial.print(",");
Serial.print(doorlock*1);
Serial.println("#");
delay(100);
}