Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat untuk monitoring suhu dan asap / gas dengan menggunakan metode fuzzy logic control mamdani untuk bisa menghasilkan putaran pada Fan AC yang optimal, alat ini menggunakan dua buah sensor yaitu sensor suhu ds18b20 dan sensor gas / asap MQ-2 dengan outputnya untuk kendali kipas AC. jadi saat kondisi panas dan ada asap kipas akan berputar cepat, jika kondisi dingin dan tidak ada asap maka kipas akan berputar lambat atau OFF, begitu seterusnya tergantung pada Fuzzy Rule yang dibuat, untuk lebih jelasnya berikut adalah skema dan programnya.
Link Referensi:
a. Arduino Uno
b. Sensor Suhu DS18b20
c. Sensor Asap / Gas MQ-2
d. Kipas Motor AC
e. Lcd 16x2
f. Blok Diagram Fuzzy
g. Fuzzy Logic Control
- Rumus yang dipakai yaitu:
- Rumus COG :
h. Program Arduino IDE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define AC_LOAD 6 // connect pin 6 arduino to input terminal of driver motor
#include <OneWire.h>
#define on 1
#define off 0
LiquidCrystal_I2C lcd(0x27, 16, 2);
OneWire ds(7);
const int AOUTpin1=0;
int adc0;
int suhu;
float h;
float T;
int output;
//int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF
float dimming;
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
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();
pinMode(AC_LOAD, OUTPUT); // Set AC Load pin as output (pin 6)
attachInterrupt(0, zero_cross_int, FALLING); // connect pin 2 arduino to output terminal of driver motor
}
void zero_cross_int() //function to be fired at the zero crossing
{
// Firing angle calculation : 1 full 50hz wave =1/50=20ms
// Every zerocrossing thus: (50hz)-> 10ms (1/2 Cycle)
// 10ms=10000us
// (10000us - 10us) / 128 = 70 (Approx)
if(output==on)
{
digitalWrite(AC_LOAD, LOW);
int dimtime = (70*dimming);
delayMicroseconds(dimtime); // Wait till firing the driver motor
digitalWrite(AC_LOAD, HIGH); // Fire the driver motor
}
else{digitalWrite(AC_LOAD, LOW);
} // Stop the driver motor
}
void loop()
{
output=on;
if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
T = celsius;
lcd.setCursor(0,0);
lcd.print("T=");
lcd.print(celsius,1);
h = 1.953125 *(analogRead(AOUTpin1));
lcd.setCursor (0,1);
lcd.print ("Gas=");
lcd.print (h,1);
lcd.print (" ");
//======================
// fuzzyfikasi suhu
//======================
if(T <= 25){
dingin = 1;
hangat = 0;
panas = 0;
}
else if(T >= 35){
panas = 1;
hangat = 0;
dingin = 0;
}
else if(T == 30){
hangat = 1;
dingin = 0;
panas = 0;
}
else if((T > 25)&&(T < 30)){
hangat = (T - 25)/5;
dingin = 1-(T - 25)/5;
panas = 0;
}
else if((T > 30)&&(T < 35)){
panas = (T - 30)/5;
hangat = 1-(T - 30)/5;
dingin = 0;
}
// Serial.print("panas= ");
// Serial.println(panas);
// Serial.print("hangat= ");
// Serial.println(hangat);
// Serial.print("dingin= ");
// Serial.println(dingin);
//===============================
// fuzzyfikasi gas
//===============================
if(h <= 150){
sedikit = 1;
sedang = 0;
banyak = 0;
}
else if(h >= 300){
banyak = 1;
sedang = 0;
sedikit = 0;
}
else if(h == 200){
banyak = 0;
sedang = 1;
sedikit = 0;
}
else if((h > 150)&&(h < 200)){
sedang = (h - 150)/50;
sedikit = 1-(h - 150)/50;
banyak = 0;
}
else if((h > 200)&&(h < 300)){
sedang = (h - 200)/100;
banyak = 1-(h - 200)/100;
sedikit = 0;
}
// Serial.print("Sedikit= ");
// Serial.println(sedikit);
// Serial.print("sedang= ");
// Serial.println(sedang);
// Serial.print("banyak= ");
// Serial.println(banyak);
//==================================
// fuzzy rule
//===================================
//============================================================================
if((T <= 25 )&&(h <= 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((T <= 25 )&&(h > 150)&&(h < 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((T <= 25 )&&(h > 200)&&(h < 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((T <= 25 )&&(h >= 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((T > 25 )&&(T < 30 )&&(h <= 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((T > 25 )&&(T < 30 )&&(h > 150)&&(h < 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((T > 25)&&(T < 30)&&(h > 200)&&(h < 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((T > 25)&&(T < 30)&&(h >= 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 ((T == 30) && (h <= 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 ((T == 30) && (h > 150) && (h < 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 ((T == 30) && (h > 200) && (h < 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 ((T == 30) && (h >= 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 ((T > 30) && (T < 35) && (h <= 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 ((T > 30) && (T < 35) && (h > 150) && (h < 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 ((T > 30) && (T < 35) && (h > 200) && (h < 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 ((T > 30) && (T < 35) && (h >= 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 ((T >= 35) && (h <= 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 ((T >= 35) && (h > 150) && (h < 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 ((T >= 35) && (h > 200) && (h < 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 ((T >= 35) && (h >= 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 ((T <= 25) && (h == 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 ((T > 25) && (T < 30) && (h == 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 ((T == 30) && (h == 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 ((T > 30) && (T < 35) && (h == 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 ((T >= 35) && (h == 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
//==================================================
//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;
dimming = 128 - Cog;
//Serial.print("Totalkom= ");
//Serial.println(Totalkom);
//Serial.print("Totalpen= ");
//Serial.println(Totalpen);
//Serial.print("COG= ");
//Serial.println(Cog);
lcd.setCursor (9,0);
lcd.print ("D=");
lcd.print (dimming,1);
lcd.print (" ");
lcd.setCursor (10,1);
lcd.print ("C=");
lcd.print (Cog,1);
lcd.print (" ");
delay(100);
}
i. Video Hasilnya
No comments:
Post a Comment