Kali ini, di kesempatan yang berbahagia ini saya akan menjelaskan mengenai bagaimana cara mengendalikan putaran motor dc menggunakan kontrol logika fuzzy, sebenarnya kontroller ini jika harus dibuat di mikrokontroller akan memakan memori yang besar, apalagi jika rule base nya banyak, lebih baik menggunakan mikrokontroller bermemori besar jika ingin menggunakan logika fuzzy ini seperti ATMega2560 atau ATMega128. Prinsip kerjanya sistem ini sangat sederhana sekali yaitu ada sebuah motor dc yang akan berputar dengan kecepatan tergantung oleh suhu dan kelembapan, misal jika suhu panas dan kelembapan juga tinggi maka motor akan berputar kencang, sebaliknya jika dingin dan kering maka motor akan berputar lambat, sensor yang dipakai yaitu sensor suhu dan kelembapan DHT11. untuk lebih jelasnya berikut penjelasan skema dan programnya.
a. Minimum System
b. Sensor DHT11
Hubungkan pin OUT ke PORTB.2 MCU
Hubungkan pin OUT ke PORTB.2 MCU
c. Motor DC 1000 Rpm + Driver L293
Menggunakan PWM1a (PortB.5) dan arah putaran (PortB.0 dan PortB.1)
Menggunakan PWM1a (PortB.5) dan arah putaran (PortB.0 dan PortB.1)
d. Fuzzy Logic Controller
Rumus yang dipakai yaitu
Rumus COG =
e. Program Bascom AVR
'====================================================
'Programmer: Yanuar Mukhammad
'E-mail : Yanuarm@hotmail.com
'Title : Fuzzy Logic Controller
'=====================================================
$regfile = "m16def.dat"
$crystal = 12000000
'--------------------------
Config Lcdpin = Pin , Rs = Portc.0 , E = Portc.1 , Db4 = Portc.2
Config Lcdpin = Pin , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5
Config Lcd = 16 * 2
Config Timer1 = Pwm , Pwm = 8 , Prescale = 64 , Compare A Pwm = Clear Down
Cursor Off
Cls
Declare Sub Get_th(t As Byte , H As Byte)
Config Serialin = Buffered , Size = 128
Config Serialout = Buffered , Size = 128
Dht_put Alias Portb.2
Dht_get Alias Pinb.2
Dht_io_set Alias Ddrb.2
Dim T As Byte
Dim H As Byte
Dim Crc As Byte
Dim Mybyte As Byte
Dim Sensor_data As String * 40
Dim Tmp_str8 As String * 8
Dim Count As Byte
Dim Y As Single
Dim Dingin As Single , Hangat As Single , Panas As Single
Dim Kering As Single , Sedang As Single , Lembab As Single
Dim Lb As Single , Sd As Single , Ke As Single
Dim Hasillb As Single , Hasilsd As Single , Hasilke As Single
Dim Coglb As Single , Cogsd As Single , Cogke As Single
Dim Cogxlb As Single , Cogxsd As Single , Cogxke As Single
Dim Komlb As Single , Komsd As Single , Komke As Single
Dim Penlb As Single , Pensd As Single , Penke As Single
Dim Totalkom As Single , Totalpen As Single
Dim Cog As Single
Dim Strcog As String * 5
Dim Lb1 As Single , Lb2 As Single , Lb3 As Single
Dim Sd1 As Single , Sd2 As Single , Sd3 As Single
Dim Ke1 As Single , Ke2 As Single , Ke3 As Single
Dim Strhasillb As String * 5 , Strhasilsd As String * 5 , Strhasilke As String * 5
Ddrb.0 = 1
Ddrb.1 = 1
Enable Interrupts
Set Dht_io_set
Set Dht_put
Portb.0 = 0
Portb.1 = 1
Cog = 0
Do
'Fuzzifikasi
If T <= 25 Then
Dingin = 1
Elseif T >= 35 Then
Panas = 1
Elseif T = 30 Then
Hangat = 1
Elseif T > 25 And T < 30 Then
Hangat = T - 25
Hangat = Hangat / 5
Dingin = T - 25
Dingin = Dingin / 5
Dingin = 1 - Dingin
Elseif T > 30 And T < 35 Then
Panas = T - 30
Panas = Panas / 5
Hangat = T - 30
Hangat = Hangat / 5
Hangat = 1 - Hangat
End If
If H <= 40 Then
Kering = 1
Elseif H >= 80 Then
Lembab = 1
Elseif H = 60 Then
Sedang = 1
Elseif H > 40 And H < 60 Then
Sedang = H - 40
Sedang = Sedang / 20
Kering = H - 40
Kering = Kering / 20
Kering = 1 - Kering
Elseif T > 60 And T < 80 Then
Lembab = H - 60
Lembab = Lembab / 20
Sedang = H - 60
Sedang = Sedang / 20
Sedang = 1 - Sedang
End If
'Fuzzy Rule
If T <= 25 And H <= 40 Then
If Dingin < Kering Then
Lb = Dingin
Elseif Dingin > Kering Then
Lb = Kering
Elseif Dingin = Kering Then
Lb = Kering
End If
Hasillb = Lb
Hasilsd = 0
Hasilke = 0
Elseif T <= 25 And H > 40 And H < 60 Then
If Dingin < Kering Then
Lb1 = Dingin
Elseif Dingin > Kering Then
Lb1 = Kering
Elseif Dingin = Kering Then
Lb1 = Kering
End If
If Dingin < Sedang Then
Lb2 = Dingin
Elseif Dingin > Sedang Then
Lb2 = Sedang
Elseif Dingin = Sedang Then
Lb2 = Sedang
End If
If Lb1 < Lb2 Then
Hasillb = Lb2
Elseif Lb1 > Lb2 Then
Hasillb = Lb1
Elseif Lb1 = Lb2 Then
Hasillb = Lb1
End If
Hasilsd = 0
Hasilke = 0
Elseif T <= 25 And H > 60 And H < 80 Then
If Dingin < Sedang Then
Lb = Dingin
Elseif Dingin > Sedang Then
Lb = Sedang
Elseif Dingin = Sedang Then
Lb = Sedang
End If
If Dingin < Lembab Then
Sd = Dingin
Elseif Dingin > Lembab Then
Sd = Lembab
Elseif Dingin = Lembab Then
Sd = Lembab
End If
Hasillb = Lb
Hasilsd = Sd
Hasilke = 0
Elseif T <= 25 And H > 80 Then
If Dingin < Lembab Then
Sd = Dingin
Elseif Dingin > Lembab Then
Sd = Lembab
Elseif Dingin = Lembab Then
Sd = Lembab
End If
Hasilsd = Sd
Hasillb = 0
Hasilke = 0
Elseif T > 25 And T < 30 And H <= 40 Then
If Dingin < Kering Then
Lb1 = Dingin
Elseif Dingin > Kering Then
Lb1 = Kering
Elseif Dingin = Kering Then
Lb1 = Kering
End If
If Hangat > Kering Then
Lb2 = Kering
Elseif Hangat < Kering Then
Lb2 = Hangat
Elseif Hangat = Kering Then
Lb2 = Hangat
End If
If Lb1 < Lb2 Then
Hasillb = Lb2
Elseif Lb1 > Lb2 Then
Hasillb = Lb1
Elseif Lb1 = Lb2 Then
Hasillb = Lb1
End If
Hasilsd = 0
Hasilke = 0
Elseif T > 25 And T < 30 And H > 40 And H < 60 Then
If Dingin < Kering Then
Lb1 = Dingin
Elseif Dingin > Kering Then
Lb1 = Kering
Elseif Dingin = Kering Then
Lb1 = Kering
End If
If Dingin < Sedang Then
Lb2 = Dingin
Elseif Dingin > Sedang Then
Lb2 = Sedang
Elseif Dingin = Sedang Then
Lb2 = Sedang
End If
If Hangat < Kering Then
Lb3 = Dingin
Elseif Hangat > Kering Then
Lb3 = Kering
Elseif Hangat = Kering Then
Lb3 = Kering
End If
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
If Lb1 > Lb2 And Lb1 > Lb3 Then 'pedoman
Hasillb = Lb1
Elseif Lb1 > Lb2 And Lb1 = Lb3 Then
Hasillb = Lb1
Elseif Lb1 = Lb2 And Lb1 > Lb3 Then
Hasillb = Lb1
Elseif Lb1 = Lb2 And Lb1 = Lb3 Then
Hasillb = Lb1
Elseif Lb2 > Lb1 And Lb2 > Lb3 Then
Hasillb = Lb2
Elseif Lb2 > Lb1 And Lb2 = Lb3 Then
Hasillb = Lb2
Elseif Lb2 = Lb1 And Lb2 > Lb3 Then
Hasillb = Lb2
Elseif Lb2 = Lb1 And Lb2 = Lb3 Then
Hasillb = Lb2
Elseif Lb3 > Lb1 And Lb3 > Lb2 Then
Hasillb = Lb3
Elseif Lb3 > Lb1 And Lb3 = Lb2 Then
Hasillb = Lb3
Elseif Lb3 = Lb1 And Lb3 > Lb2 Then
Hasillb = Lb3
Elseif Lb3 = Lb1 And Lb3 = Lb2 Then
Hasillb = Lb3
End If
Hasilsd = Sd
Hasilke = 0
Elseif T > 25 And T < 30 And H > 60 And H < 80 Then
If Dingin < Sedang Then
Lb = Dingin
Elseif Dingin > Sedang Then
Lb = Sedang
Elseif Dingin = Sedang Then
Lb = Sedang
End If
If Dingin < Lembab Then
Sd1 = Dingin
Elseif Dingin > Lembab Then
Sd1 = Lembab
Elseif Dingin = Lembab Then
Sd1 = Lembab
End If
If Hangat < Sedang Then
Sd2 = Hangat
Elseif Hangat > Sedang Then
Sd2 = Sedang
Elseif Hangat = Sedang Then
Sd2 = Sedang
End If
If Hangat < Lembab Then
Ke = Hangat
Elseif Hangat > Lembab Then
Ke = Lembab
Elseif Hangat = Lembab Then
Ke = Lembab
End If
If Sd1 > Sd2 Then
Hasilsd = Sd1
Elseif Sd2 > Sd1 Then
Hasilsd = Sd2
Elseif Sd2 = Sd1 Then
Hasilsd = Sd2
End If
Hasillb = Lb
Hasilke = Ke
Elseif T > 25 And T < 30 And H > 80 Then
If Dingin < Lembab Then
Sd = Dingin
Elseif Dingin > Lembab Then
Sd = Lembab
Elseif Dingin = Lembab Then
Sd = Lembab
End If
If Hangat > Lembab Then
Ke = Lembab
Elseif Hangat < Lembab Then
Ke = Hangat
Elseif Hangat = Lembab Then
Ke = Hangat
End If
Hasilsd = Sd
Hasilke = Ke
Hasillb = 0
Elseif T = 30 And H <= 40 Then
If Hangat > Kering Then
Lb = Kering
Elseif Hangat < Kering Then
Lb = Hangat
Elseif Hangat = Kering Then
Lb = Hangat
End If
Hasillb = Lb
Hasilsd = 0
Hasilke = 0
Elseif T = 30 And H > 40 And H < 60 Then
If Hangat < Kering Then
Lb = Hangat
Elseif Hangat > Kering Then
Lb = Kering
Elseif Hangat = Kering Then
Lb = Kering
End If
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Kering Then
Sd = Kering
Elseif Hangat = Kering Then
Sd = Kering
End If
Hasillb = Lb
Hasilsd = Sd
Hasilke = 0
Elseif T = 30 And H > 60 And H < 80 Then
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
If Hangat < Lembab Then
Ke = Hangat
Elseif Hangat > Lembab Then
Ke = Lembab
Elseif Hangat = Lembab Then
Ke = Lembab
End If
Hasilsd = Sd
Hasilke = Ke
Hasillb = 0
Elseif T = 30 And H > 80 Then
If Hangat < Lembab Then
Ke = Hangat
Elseif Hangat > Lembab Then
Ke = Lembab
Elseif Hangat = Lembab Then
Ke = Lembab
End If
Hasilke = Ke
Hasilsd = 0
Hasillb = 0
Elseif T > 30 And T < 35 And H <= 40 Then
If Hangat < Kering Then
Lb = Hangat
Elseif Hangat > Kering Then
Lb = Kering
Elseif Hangat = Kering Then
Lb = Kering
End If
If Panas < Kering Then
Sd = Panas
Elseif Panas > Kering Then
Sd = Kering
Elseif Panas = Kering Then
Sd = Kering
End If
Hasillb = Lb
Hasilsd = Sd
Hasilke = 0
Elseif T > 30 And T < 35 And H > 40 And H < 60 Then
If Hangat < Kering Then
Lb = Hangat
Elseif Hangat > Kering Then
Lb = Kering
Elseif Hangat = Kering Then
Lb = Kering
End If
If Hangat < Sedang Then
Sd1 = Hangat
Elseif Hangat > Sedang Then
Sd1 = Sedang
Elseif Hangat = Sedang Then
Sd1 = Sedang
End If
If Panas < Kering Then
Sd2 = Panas
Elseif Panas > Kering Then
Sd2 = Kering
Elseif Panas = Kering Then
Sd2 = Kering
End If
If Panas < Sedang Then
Ke = Panas
Elseif Panas > Sedang Then
Ke = Sedang
Elseif Panas = Sedang Then
Ke = Sedang
End If
If Sd1 < Sd2 Then
Hasilsd = Sd2
Elseif Sd1 > Sd2 Then
Hasilsd = Sd1
Elseif Sd1 = Sd2 Then
Hasilsd = Sd1
End If
Hasilke = Ke
Hasillb = Lb
Elseif T > 30 And T < 35 And H > 60 And H < 80 Then
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
If Hangat < Lembab Then
Ke1 = Hangat
Elseif Hangat > Lembab Then
Ke1 = Lembab
Elseif Hangat = Lembab Then
Ke1 = Lembab
End If
If Panas < Sedang Then
Ke2 = Panas
Elseif Panas > Sedang Then
Ke2 = Sedang
Elseif Panas = Sedang Then
Ke2 = Sedang
End If
If Panas < Lembab Then
Ke3 = Panas
Elseif Panas > Lembab Then
Ke3 = Lembab
Elseif Panas = Lembab Then
Ke3 = Lembab
End If
If Ke1 > Ke2 And Ke1 > Ke3 Then
Hasilke = Ke1
Elseif Ke1 > Ke2 And Ke1 = Ke3 Then
Hasilke = Ke1
Elseif Ke1 = Ke2 And Ke1 > Ke3 Then
Hasilke = Ke1
Elseif Ke1 = Ke2 And Ke1 = Ke3 Then
Hasilke = Ke1
Elseif Ke2 > Ke1 And Ke2 > Ke3 Then
Hasilke = Ke2
Elseif Ke2 > Ke1 And Ke2 = Ke3 Then
Hasilke = Ke2
Elseif Ke2 = Ke1 And Ke2 > Ke3 Then
Hasilke = Ke2
Elseif Ke2 = Ke1 And Ke2 = Ke3 Then
Hasilke = Ke2
Elseif Ke3 > Ke1 And Ke3 > Ke2 Then
Hasilke = Ke3
Elseif Ke3 > Ke1 And Ke3 = Ke2 Then
Hasilke = Ke3
Elseif Ke3 = Ke1 And Ke3 > Ke2 Then
Hasilke = Ke3
Elseif Ke3 = Ke1 And Ke3 = Ke2 Then
Hasilke = Ke3
End If
Hasilsd = Sd
Hasillb = 0
Elseif T > 30 And T < 35 And H > 80 Then
If Hangat < Lembab Then
Ke1 = Hangat
Elseif Hangat > Lembab Then
Ke1 = Lembab
Elseif Hangat = Lembab Then
Ke1 = Lembab
End If
If Panas < Lembab Then
Ke2 = Panas
Elseif Panas > Lembab Then
Ke2 = Lembab
Elseif Panas = Lembab Then
Ke2 = Lembab
End If
If Ke1 < Ke2 Then
Hasilke = Ke2
Elseif Ke1 > Ke2 Then
Hasilke = Ke1
Elseif Ke1 = Ke2 Then
Hasilke = Ke1
End If
Hasilsd = 0
Hasillb = 0
Elseif T >= 35 And H <= 40 Then
If Panas < Kering Then
Sd = Panas
Elseif Panas > Kering Then
Sd = Kering
Elseif Panas = Kering Then
Sd = Kering
End If
Hasilsd = Sd
Hasillb = 0
Hasilke = 0
Elseif T >= 35 And H > 40 And H < 60 Then
If Panas < Kering Then
Sd = Panas
Elseif Panas > Kering Then
Sd = Kering
Elseif Panas = Kering Then
Sd = Kering
End If
If Panas < Sedang Then
Ke = Panas
Elseif Panas > Sedang Then
Ke = Sedang
Elseif Panas = Sedang Then
Ke = Sedang
End If
Hasilsd = Sd
Hasilke = Ke
Hasillb = 0
Elseif T >= 35 And H > 60 And H < 80 Then
If Panas < Sedang Then
Ke1 = Panas
Elseif Panas > Sedang Then
Ke1 = Sedang
Elseif Panas = Sedang Then
Ke1 = Sedang
End If
If Panas < Lembab Then
Ke2 = Panas
Elseif Panas > Lembab Then
Ke2 = Lembab
Elseif Panas = Lembab Then
Ke2 = Lembab
End If
If Ke1 < Ke2 Then
Hasilke = Ke2
Elseif Ke1 > Ke2 Then
Hasilke = Ke1
Elseif Ke1 = Ke2 Then
Hasilke = Ke1
End If
Hasillb = 0
Hasilsd = 0
Elseif T >= 35 And H > 80 Then
If Panas < Lembab Then
Ke = Panas
Elseif Panas > Lembab Then
Ke = Lembab
Elseif Panas = Lembab Then
Ke = Lembab
End If
Hasilke = Ke
Hasilsd = 0
Hasillb = 0
Elseif T <= 25 And H = 60 Then
If Dingin < Sedang Then
Lb = Dingin
Elseif Dingin > Sedang Then
Lb = Sedang
Elseif Dingin = Sedang Then
Lb = Sedang
End If
Hasillb = Lb
Hasilsd = 0
Hasilke = 0
Elseif T > 25 And T < 30 And H = 60 Then
If Dingin < Sedang Then
Lb = Dingin
Elseif Dingin > Sedang Then
Lb = Sedang
Elseif Dingin = Sedang Then
Lb = Sedang
End If
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
Hasillb = Lb
Hasilsd = Sd
Hasilke = 0
Elseif T = 30 And H = 60 Then
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
Hasilsd = Sd
Hasillb = 0
Hasilke = 0
Elseif T > 30 And T < 35 And H = 60 Then
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
If Panas < Sedang Then
Ke = Panas
Elseif Panas > Sedang Then
Ke = Sedang
Elseif Panas = Sedang Then
Ke = Sedang
End If
Hasilsd = Sd
Hasilke = Ke
Hasillb = 0
Elseif T >= 35 And H = 60 Then
If Panas < Sedang Then
Ke = Panas
Elseif Panas > Sedang Then
Ke = Sedang
Elseif Panas = Sedang Then
Ke = Sedang
End If
Hasilke = Ke
Hasilsd = 0
Hasillb = 0
End If
'Defuzzifikasi
'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
Cog = Totalkom / Totalpen
Call Get_th(t , H)
Cls
Strcog = Fusing(cog , "#.##")
Upperline
Lcd "TMP: " ; T ; "C " ; H ; "%"
Lowerline
Lcd "Fuzzy = " ; Strcog
Pwm1a = Cog
Waitms 1500
Cls
Strhasillb = Fusing(hasillb , "#.##")
Strhasilsd = Fusing(hasilsd , "#.##")
Strhasilke = Fusing(hasilke , "#.##")
Upperline
Lcd "TMP: " ; T ; "C " ; H ; "%"
Lowerline
Lcd Strhasillb ; " " ; Strhasilsd ; " " ; Strhasilke
Wait 3
Loop
'mengambil data DHT11
Sub Get_th(t As Byte , H As Byte)
Count = 0
Sensor_data = ""
Set Dht_io_set
Reset Dht_put
Waitms 25
Set Dht_put
Waitus 40
Reset Dht_io_set
Waitus 40
If Dht_get = 1 Then
H = 1
Exit Sub
End If
Waitus 80
If Dht_get = 0 Then
H = 2
Exit Sub
End If
While Dht_get = 1 : Wend
Do
While Dht_get = 0 : Wend
Waitus 30
If Dht_get = 1 Then
Sensor_data = Sensor_data + "1"
While Dht_get = 1 : Wend
Else
Sensor_data = Sensor_data + "0"
End If
Incr Count
Loop Until Count = 40
Set Dht_io_set
Set Dht_put
Tmp_str8 = Left(sensor_data , 8)
H = Binval(tmp_str8)
Tmp_str8 = Mid(sensor_data , 17 , 8)
T = Binval(tmp_str8)
Tmp_str8 = Right(sensor_data , 8)
Crc = Binval(tmp_str8)
Mybyte = T + H
If Mybyte <> Crc Then
H = 3
End If
End Sub
'Programmer: Yanuar Mukhammad
'E-mail : Yanuarm@hotmail.com
'Title : Fuzzy Logic Controller
'=====================================================
$regfile = "m16def.dat"
$crystal = 12000000
'--------------------------
Config Lcdpin = Pin , Rs = Portc.0 , E = Portc.1 , Db4 = Portc.2
Config Lcdpin = Pin , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5
Config Lcd = 16 * 2
Config Timer1 = Pwm , Pwm = 8 , Prescale = 64 , Compare A Pwm = Clear Down
Cursor Off
Cls
Declare Sub Get_th(t As Byte , H As Byte)
Config Serialin = Buffered , Size = 128
Config Serialout = Buffered , Size = 128
Dht_put Alias Portb.2
Dht_get Alias Pinb.2
Dht_io_set Alias Ddrb.2
Dim T As Byte
Dim H As Byte
Dim Crc As Byte
Dim Mybyte As Byte
Dim Sensor_data As String * 40
Dim Tmp_str8 As String * 8
Dim Count As Byte
Dim Y As Single
Dim Dingin As Single , Hangat As Single , Panas As Single
Dim Kering As Single , Sedang As Single , Lembab As Single
Dim Lb As Single , Sd As Single , Ke As Single
Dim Hasillb As Single , Hasilsd As Single , Hasilke As Single
Dim Coglb As Single , Cogsd As Single , Cogke As Single
Dim Cogxlb As Single , Cogxsd As Single , Cogxke As Single
Dim Komlb As Single , Komsd As Single , Komke As Single
Dim Penlb As Single , Pensd As Single , Penke As Single
Dim Totalkom As Single , Totalpen As Single
Dim Cog As Single
Dim Strcog As String * 5
Dim Lb1 As Single , Lb2 As Single , Lb3 As Single
Dim Sd1 As Single , Sd2 As Single , Sd3 As Single
Dim Ke1 As Single , Ke2 As Single , Ke3 As Single
Dim Strhasillb As String * 5 , Strhasilsd As String * 5 , Strhasilke As String * 5
Ddrb.0 = 1
Ddrb.1 = 1
Enable Interrupts
Set Dht_io_set
Set Dht_put
Portb.0 = 0
Portb.1 = 1
Cog = 0
Do
'Fuzzifikasi
If T <= 25 Then
Dingin = 1
Elseif T >= 35 Then
Panas = 1
Elseif T = 30 Then
Hangat = 1
Elseif T > 25 And T < 30 Then
Hangat = T - 25
Hangat = Hangat / 5
Dingin = T - 25
Dingin = Dingin / 5
Dingin = 1 - Dingin
Elseif T > 30 And T < 35 Then
Panas = T - 30
Panas = Panas / 5
Hangat = T - 30
Hangat = Hangat / 5
Hangat = 1 - Hangat
End If
If H <= 40 Then
Kering = 1
Elseif H >= 80 Then
Lembab = 1
Elseif H = 60 Then
Sedang = 1
Elseif H > 40 And H < 60 Then
Sedang = H - 40
Sedang = Sedang / 20
Kering = H - 40
Kering = Kering / 20
Kering = 1 - Kering
Elseif T > 60 And T < 80 Then
Lembab = H - 60
Lembab = Lembab / 20
Sedang = H - 60
Sedang = Sedang / 20
Sedang = 1 - Sedang
End If
'Fuzzy Rule
If T <= 25 And H <= 40 Then
If Dingin < Kering Then
Lb = Dingin
Elseif Dingin > Kering Then
Lb = Kering
Elseif Dingin = Kering Then
Lb = Kering
End If
Hasillb = Lb
Hasilsd = 0
Hasilke = 0
Elseif T <= 25 And H > 40 And H < 60 Then
If Dingin < Kering Then
Lb1 = Dingin
Elseif Dingin > Kering Then
Lb1 = Kering
Elseif Dingin = Kering Then
Lb1 = Kering
End If
If Dingin < Sedang Then
Lb2 = Dingin
Elseif Dingin > Sedang Then
Lb2 = Sedang
Elseif Dingin = Sedang Then
Lb2 = Sedang
End If
If Lb1 < Lb2 Then
Hasillb = Lb2
Elseif Lb1 > Lb2 Then
Hasillb = Lb1
Elseif Lb1 = Lb2 Then
Hasillb = Lb1
End If
Hasilsd = 0
Hasilke = 0
Elseif T <= 25 And H > 60 And H < 80 Then
If Dingin < Sedang Then
Lb = Dingin
Elseif Dingin > Sedang Then
Lb = Sedang
Elseif Dingin = Sedang Then
Lb = Sedang
End If
If Dingin < Lembab Then
Sd = Dingin
Elseif Dingin > Lembab Then
Sd = Lembab
Elseif Dingin = Lembab Then
Sd = Lembab
End If
Hasillb = Lb
Hasilsd = Sd
Hasilke = 0
Elseif T <= 25 And H > 80 Then
If Dingin < Lembab Then
Sd = Dingin
Elseif Dingin > Lembab Then
Sd = Lembab
Elseif Dingin = Lembab Then
Sd = Lembab
End If
Hasilsd = Sd
Hasillb = 0
Hasilke = 0
Elseif T > 25 And T < 30 And H <= 40 Then
If Dingin < Kering Then
Lb1 = Dingin
Elseif Dingin > Kering Then
Lb1 = Kering
Elseif Dingin = Kering Then
Lb1 = Kering
End If
If Hangat > Kering Then
Lb2 = Kering
Elseif Hangat < Kering Then
Lb2 = Hangat
Elseif Hangat = Kering Then
Lb2 = Hangat
End If
If Lb1 < Lb2 Then
Hasillb = Lb2
Elseif Lb1 > Lb2 Then
Hasillb = Lb1
Elseif Lb1 = Lb2 Then
Hasillb = Lb1
End If
Hasilsd = 0
Hasilke = 0
Elseif T > 25 And T < 30 And H > 40 And H < 60 Then
If Dingin < Kering Then
Lb1 = Dingin
Elseif Dingin > Kering Then
Lb1 = Kering
Elseif Dingin = Kering Then
Lb1 = Kering
End If
If Dingin < Sedang Then
Lb2 = Dingin
Elseif Dingin > Sedang Then
Lb2 = Sedang
Elseif Dingin = Sedang Then
Lb2 = Sedang
End If
If Hangat < Kering Then
Lb3 = Dingin
Elseif Hangat > Kering Then
Lb3 = Kering
Elseif Hangat = Kering Then
Lb3 = Kering
End If
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
If Lb1 > Lb2 And Lb1 > Lb3 Then 'pedoman
Hasillb = Lb1
Elseif Lb1 > Lb2 And Lb1 = Lb3 Then
Hasillb = Lb1
Elseif Lb1 = Lb2 And Lb1 > Lb3 Then
Hasillb = Lb1
Elseif Lb1 = Lb2 And Lb1 = Lb3 Then
Hasillb = Lb1
Elseif Lb2 > Lb1 And Lb2 > Lb3 Then
Hasillb = Lb2
Elseif Lb2 > Lb1 And Lb2 = Lb3 Then
Hasillb = Lb2
Elseif Lb2 = Lb1 And Lb2 > Lb3 Then
Hasillb = Lb2
Elseif Lb2 = Lb1 And Lb2 = Lb3 Then
Hasillb = Lb2
Elseif Lb3 > Lb1 And Lb3 > Lb2 Then
Hasillb = Lb3
Elseif Lb3 > Lb1 And Lb3 = Lb2 Then
Hasillb = Lb3
Elseif Lb3 = Lb1 And Lb3 > Lb2 Then
Hasillb = Lb3
Elseif Lb3 = Lb1 And Lb3 = Lb2 Then
Hasillb = Lb3
End If
Hasilsd = Sd
Hasilke = 0
Elseif T > 25 And T < 30 And H > 60 And H < 80 Then
If Dingin < Sedang Then
Lb = Dingin
Elseif Dingin > Sedang Then
Lb = Sedang
Elseif Dingin = Sedang Then
Lb = Sedang
End If
If Dingin < Lembab Then
Sd1 = Dingin
Elseif Dingin > Lembab Then
Sd1 = Lembab
Elseif Dingin = Lembab Then
Sd1 = Lembab
End If
If Hangat < Sedang Then
Sd2 = Hangat
Elseif Hangat > Sedang Then
Sd2 = Sedang
Elseif Hangat = Sedang Then
Sd2 = Sedang
End If
If Hangat < Lembab Then
Ke = Hangat
Elseif Hangat > Lembab Then
Ke = Lembab
Elseif Hangat = Lembab Then
Ke = Lembab
End If
If Sd1 > Sd2 Then
Hasilsd = Sd1
Elseif Sd2 > Sd1 Then
Hasilsd = Sd2
Elseif Sd2 = Sd1 Then
Hasilsd = Sd2
End If
Hasillb = Lb
Hasilke = Ke
Elseif T > 25 And T < 30 And H > 80 Then
If Dingin < Lembab Then
Sd = Dingin
Elseif Dingin > Lembab Then
Sd = Lembab
Elseif Dingin = Lembab Then
Sd = Lembab
End If
If Hangat > Lembab Then
Ke = Lembab
Elseif Hangat < Lembab Then
Ke = Hangat
Elseif Hangat = Lembab Then
Ke = Hangat
End If
Hasilsd = Sd
Hasilke = Ke
Hasillb = 0
Elseif T = 30 And H <= 40 Then
If Hangat > Kering Then
Lb = Kering
Elseif Hangat < Kering Then
Lb = Hangat
Elseif Hangat = Kering Then
Lb = Hangat
End If
Hasillb = Lb
Hasilsd = 0
Hasilke = 0
Elseif T = 30 And H > 40 And H < 60 Then
If Hangat < Kering Then
Lb = Hangat
Elseif Hangat > Kering Then
Lb = Kering
Elseif Hangat = Kering Then
Lb = Kering
End If
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Kering Then
Sd = Kering
Elseif Hangat = Kering Then
Sd = Kering
End If
Hasillb = Lb
Hasilsd = Sd
Hasilke = 0
Elseif T = 30 And H > 60 And H < 80 Then
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
If Hangat < Lembab Then
Ke = Hangat
Elseif Hangat > Lembab Then
Ke = Lembab
Elseif Hangat = Lembab Then
Ke = Lembab
End If
Hasilsd = Sd
Hasilke = Ke
Hasillb = 0
Elseif T = 30 And H > 80 Then
If Hangat < Lembab Then
Ke = Hangat
Elseif Hangat > Lembab Then
Ke = Lembab
Elseif Hangat = Lembab Then
Ke = Lembab
End If
Hasilke = Ke
Hasilsd = 0
Hasillb = 0
Elseif T > 30 And T < 35 And H <= 40 Then
If Hangat < Kering Then
Lb = Hangat
Elseif Hangat > Kering Then
Lb = Kering
Elseif Hangat = Kering Then
Lb = Kering
End If
If Panas < Kering Then
Sd = Panas
Elseif Panas > Kering Then
Sd = Kering
Elseif Panas = Kering Then
Sd = Kering
End If
Hasillb = Lb
Hasilsd = Sd
Hasilke = 0
Elseif T > 30 And T < 35 And H > 40 And H < 60 Then
If Hangat < Kering Then
Lb = Hangat
Elseif Hangat > Kering Then
Lb = Kering
Elseif Hangat = Kering Then
Lb = Kering
End If
If Hangat < Sedang Then
Sd1 = Hangat
Elseif Hangat > Sedang Then
Sd1 = Sedang
Elseif Hangat = Sedang Then
Sd1 = Sedang
End If
If Panas < Kering Then
Sd2 = Panas
Elseif Panas > Kering Then
Sd2 = Kering
Elseif Panas = Kering Then
Sd2 = Kering
End If
If Panas < Sedang Then
Ke = Panas
Elseif Panas > Sedang Then
Ke = Sedang
Elseif Panas = Sedang Then
Ke = Sedang
End If
If Sd1 < Sd2 Then
Hasilsd = Sd2
Elseif Sd1 > Sd2 Then
Hasilsd = Sd1
Elseif Sd1 = Sd2 Then
Hasilsd = Sd1
End If
Hasilke = Ke
Hasillb = Lb
Elseif T > 30 And T < 35 And H > 60 And H < 80 Then
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
If Hangat < Lembab Then
Ke1 = Hangat
Elseif Hangat > Lembab Then
Ke1 = Lembab
Elseif Hangat = Lembab Then
Ke1 = Lembab
End If
If Panas < Sedang Then
Ke2 = Panas
Elseif Panas > Sedang Then
Ke2 = Sedang
Elseif Panas = Sedang Then
Ke2 = Sedang
End If
If Panas < Lembab Then
Ke3 = Panas
Elseif Panas > Lembab Then
Ke3 = Lembab
Elseif Panas = Lembab Then
Ke3 = Lembab
End If
If Ke1 > Ke2 And Ke1 > Ke3 Then
Hasilke = Ke1
Elseif Ke1 > Ke2 And Ke1 = Ke3 Then
Hasilke = Ke1
Elseif Ke1 = Ke2 And Ke1 > Ke3 Then
Hasilke = Ke1
Elseif Ke1 = Ke2 And Ke1 = Ke3 Then
Hasilke = Ke1
Elseif Ke2 > Ke1 And Ke2 > Ke3 Then
Hasilke = Ke2
Elseif Ke2 > Ke1 And Ke2 = Ke3 Then
Hasilke = Ke2
Elseif Ke2 = Ke1 And Ke2 > Ke3 Then
Hasilke = Ke2
Elseif Ke2 = Ke1 And Ke2 = Ke3 Then
Hasilke = Ke2
Elseif Ke3 > Ke1 And Ke3 > Ke2 Then
Hasilke = Ke3
Elseif Ke3 > Ke1 And Ke3 = Ke2 Then
Hasilke = Ke3
Elseif Ke3 = Ke1 And Ke3 > Ke2 Then
Hasilke = Ke3
Elseif Ke3 = Ke1 And Ke3 = Ke2 Then
Hasilke = Ke3
End If
Hasilsd = Sd
Hasillb = 0
Elseif T > 30 And T < 35 And H > 80 Then
If Hangat < Lembab Then
Ke1 = Hangat
Elseif Hangat > Lembab Then
Ke1 = Lembab
Elseif Hangat = Lembab Then
Ke1 = Lembab
End If
If Panas < Lembab Then
Ke2 = Panas
Elseif Panas > Lembab Then
Ke2 = Lembab
Elseif Panas = Lembab Then
Ke2 = Lembab
End If
If Ke1 < Ke2 Then
Hasilke = Ke2
Elseif Ke1 > Ke2 Then
Hasilke = Ke1
Elseif Ke1 = Ke2 Then
Hasilke = Ke1
End If
Hasilsd = 0
Hasillb = 0
Elseif T >= 35 And H <= 40 Then
If Panas < Kering Then
Sd = Panas
Elseif Panas > Kering Then
Sd = Kering
Elseif Panas = Kering Then
Sd = Kering
End If
Hasilsd = Sd
Hasillb = 0
Hasilke = 0
Elseif T >= 35 And H > 40 And H < 60 Then
If Panas < Kering Then
Sd = Panas
Elseif Panas > Kering Then
Sd = Kering
Elseif Panas = Kering Then
Sd = Kering
End If
If Panas < Sedang Then
Ke = Panas
Elseif Panas > Sedang Then
Ke = Sedang
Elseif Panas = Sedang Then
Ke = Sedang
End If
Hasilsd = Sd
Hasilke = Ke
Hasillb = 0
Elseif T >= 35 And H > 60 And H < 80 Then
If Panas < Sedang Then
Ke1 = Panas
Elseif Panas > Sedang Then
Ke1 = Sedang
Elseif Panas = Sedang Then
Ke1 = Sedang
End If
If Panas < Lembab Then
Ke2 = Panas
Elseif Panas > Lembab Then
Ke2 = Lembab
Elseif Panas = Lembab Then
Ke2 = Lembab
End If
If Ke1 < Ke2 Then
Hasilke = Ke2
Elseif Ke1 > Ke2 Then
Hasilke = Ke1
Elseif Ke1 = Ke2 Then
Hasilke = Ke1
End If
Hasillb = 0
Hasilsd = 0
Elseif T >= 35 And H > 80 Then
If Panas < Lembab Then
Ke = Panas
Elseif Panas > Lembab Then
Ke = Lembab
Elseif Panas = Lembab Then
Ke = Lembab
End If
Hasilke = Ke
Hasilsd = 0
Hasillb = 0
Elseif T <= 25 And H = 60 Then
If Dingin < Sedang Then
Lb = Dingin
Elseif Dingin > Sedang Then
Lb = Sedang
Elseif Dingin = Sedang Then
Lb = Sedang
End If
Hasillb = Lb
Hasilsd = 0
Hasilke = 0
Elseif T > 25 And T < 30 And H = 60 Then
If Dingin < Sedang Then
Lb = Dingin
Elseif Dingin > Sedang Then
Lb = Sedang
Elseif Dingin = Sedang Then
Lb = Sedang
End If
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
Hasillb = Lb
Hasilsd = Sd
Hasilke = 0
Elseif T = 30 And H = 60 Then
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
Hasilsd = Sd
Hasillb = 0
Hasilke = 0
Elseif T > 30 And T < 35 And H = 60 Then
If Hangat < Sedang Then
Sd = Hangat
Elseif Hangat > Sedang Then
Sd = Sedang
Elseif Hangat = Sedang Then
Sd = Sedang
End If
If Panas < Sedang Then
Ke = Panas
Elseif Panas > Sedang Then
Ke = Sedang
Elseif Panas = Sedang Then
Ke = Sedang
End If
Hasilsd = Sd
Hasilke = Ke
Hasillb = 0
Elseif T >= 35 And H = 60 Then
If Panas < Sedang Then
Ke = Panas
Elseif Panas > Sedang Then
Ke = Sedang
Elseif Panas = Sedang Then
Ke = Sedang
End If
Hasilke = Ke
Hasilsd = 0
Hasillb = 0
End If
'Defuzzifikasi
'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
Cog = Totalkom / Totalpen
Call Get_th(t , H)
Cls
Strcog = Fusing(cog , "#.##")
Upperline
Lcd "TMP: " ; T ; "C " ; H ; "%"
Lowerline
Lcd "Fuzzy = " ; Strcog
Pwm1a = Cog
Waitms 1500
Cls
Strhasillb = Fusing(hasillb , "#.##")
Strhasilsd = Fusing(hasilsd , "#.##")
Strhasilke = Fusing(hasilke , "#.##")
Upperline
Lcd "TMP: " ; T ; "C " ; H ; "%"
Lowerline
Lcd Strhasillb ; " " ; Strhasilsd ; " " ; Strhasilke
Wait 3
Loop
'mengambil data DHT11
Sub Get_th(t As Byte , H As Byte)
Count = 0
Sensor_data = ""
Set Dht_io_set
Reset Dht_put
Waitms 25
Set Dht_put
Waitus 40
Reset Dht_io_set
Waitus 40
If Dht_get = 1 Then
H = 1
Exit Sub
End If
Waitus 80
If Dht_get = 0 Then
H = 2
Exit Sub
End If
While Dht_get = 1 : Wend
Do
While Dht_get = 0 : Wend
Waitus 30
If Dht_get = 1 Then
Sensor_data = Sensor_data + "1"
While Dht_get = 1 : Wend
Else
Sensor_data = Sensor_data + "0"
End If
Incr Count
Loop Until Count = 40
Set Dht_io_set
Set Dht_put
Tmp_str8 = Left(sensor_data , 8)
H = Binval(tmp_str8)
Tmp_str8 = Mid(sensor_data , 17 , 8)
T = Binval(tmp_str8)
Tmp_str8 = Right(sensor_data , 8)
Crc = Binval(tmp_str8)
Mybyte = T + H
If Mybyte <> Crc Then
H = 3
End If
End Sub
f. VIDEO HASILNYA
Link referensi - visit here :
http://petro.tanrei.ca/fuzzylogic/fuzzy_negnevistky.html
No comments:
Post a Comment