The code below is still wrong when I press one of the buttons so the (out value) is not updated.
Please help me, when button one is pressed then (out1 value) is updated like when it loops, and when button two is pressed then (out value) is like when it loops, and when button three is pressed then (out1 value) and (out value) are updated like when loops.
#include <SoftwareSerial.h>
#include "DHT.h"
#include <Wire.h>
#include <BH1750.h>
#define intervallo 2000
#define DHTPIN1 A3
#define DHTPIN2 A2
const int button1Pin = 4; // Definisikan pin untuk tombol 1
const int button2Pin = 5; // Definisikan pin untuk tombol 2
const int button3Pin = 6; // Definisikan pin untuk tombol 3
const int ledPin = 7; // Definisikan pin untuk LED
const int buzzerPin = 8; // Definisikan pin untuk buzzer
int lastButton1State = 0; // Variabel untuk menyimpan status terakhir tombol 1
int lastButton2State = 0; // Variabel untuk menyimpan status terakhir tombol 2
int lastButton3State = 0; // Variabel untuk menyimpan status terakhir tombol 3
bool ledOn = false; // Bendera untuk status LED
bool buzzerOn = false; // Bendera untuk status buzzer
#define DHTTYPE1 DHT11
#define DHTTYPE2 DHT11
DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);
unsigned long tempo = 0;
BH1750 lightMeter;
SoftwareSerial Bluetooth(2, 3); // RX, TX
float suhuh1;
float suhuh2;
float cahaya1;
float buttonpin1, buttonpin2, buttonpin3;
float jumlah, jumlah2;
float jumlah1, jumlah12;
float cahaya, suhu, out, suhu1, out1;
float dingin, hangat, panas, mendung, berawan, cerah;
float rule1, rule2, rule3, rule4, rule5, rule6, rule7,
rule8, rule9;
float mrule1, mrule2, mrule3, mrule4, mrule5, mrule6,
mrule7, mrule8, mrule9;
float dingin1, hangat1, panas1, mendung1, berawan1, cerah1;
float rule11, rule12, rule13, rule14, rule15, rule16, rule17,
rule18, rule19;
float mrule11, mrule12, mrule13, mrule14, mrule15, mrule16,
mrule17, mrule18, mrule19;
unsigned char suhuDingin(){
if (suhu >= 30){dingin =0;}
else if (suhu >=20 && suhu <=30){dingin=(30-suhu)/10;}
else if (suhu <= 20){dingin =1;}
return dingin;
}
unsigned char suhuHangat(){
if (suhu <= 20 && suhu >=40){hangat = 0;}
else if (suhu >=20 && suhu <=30){hangat =(suhu-20)/10;}
else if (suhu >=30 && suhu <=40){hangat =(40-suhu)/10;}
return hangat;
}
unsigned char suhuPanas(){
if (suhu <= 30){panas = 0;}
else if (suhu >= 30 && suhu <=40){panas = (suhu - 30) / 10;}
else if (suhu >=40){panas = 1;}
return panas;
}
unsigned char suhuDingin1(){
if (suhu1 >= 30){dingin1 =0;}
else if (suhu1 >=20 && suhu1 <=30){dingin1=(30-suhu1)/10;}
else if (suhu1 <= 20){dingin1 =1;}
return dingin1;
}
unsigned char suhuHangat1(){
if (suhu1 <= 20 && suhu1 >=40){hangat1 = 0;}
else if (suhu1 >=20 && suhu1 <=30){hangat1 =(suhu1-20)/10;}
else if (suhu1 >=30 && suhu1 <=40){hangat1 =(40-suhu1)/10;}
return hangat1;
}
unsigned char suhuPanas1(){
if (suhu1 <= 30){panas1 = 0;}
else if (suhu1 >= 30 && suhu1 <=40){panas1 = (suhu1 - 30) / 10;}
else if (suhu1 >=40){panas1 = 1;}
return panas1;
}
unsigned char cahayaMendung(){
if (cahaya >= 6500){mendung =0;}
else if (cahaya >=3500 && cahaya <=6500){mendung = (6500-cahaya)/3000;}
else if (cahaya <= 3500){mendung = 1;}
return mendung;
}
unsigned char cahayaBerawan(){
if (cahaya <= 3500 && cahaya >=12000){berawan =0;}
else if (cahaya >=3500 && cahaya <=6500){berawan=(cahaya-3500)/3000;}
else if (cahaya >=6500 && cahaya <=12000){berawan=(12000-cahaya)/3000;}
return berawan;
}
unsigned char cahayaCerah(){
if (cahaya <=6500 ){cerah =0;}
else if (cahaya >=6500 && cahaya <=12000){cerah=(cahaya-6500)/3000;}
else if (cahaya >= 12000){cerah =1;}
return cerah;
}
//Fuzzifikasi
void fuzzifikasi(){
suhuDingin();
suhuHangat();
suhuPanas();
suhuDingin1();
suhuHangat1();
suhuPanas1();
cahayaMendung();
cahayaBerawan();
cahayaCerah();
}
// Rule
void rule(){
fuzzifikasi();
rule1 = 100;
mrule1 = min(dingin, mendung);
rule2 = 100;
mrule2 = min(dingin, berawan);
rule3 = 255;
mrule3 = min(dingin, cerah);
rule4 = 0;
mrule4 = min(hangat, mendung);
rule5 = 0;
mrule5 = min(hangat, berawan);
rule6 = 0;
mrule6 = min(hangat, cerah);
rule7 = 255;
mrule7 = min(panas, mendung);
rule8 = 255;
mrule8 = min(panas, berawan);
rule9 = 100;
mrule9 = min(panas, cerah);
rule11 = 100;
mrule11 = min(dingin1, mendung);
rule12 = 100;
mrule12 = min(dingin1, berawan);
rule13 = 255;
mrule13 = min(dingin1, cerah);
rule14 = 0;
mrule14 = min(hangat1, mendung);
rule15 = 0;
mrule15 = min(hangat1, berawan);
rule16 = 0;
mrule16 = min(hangat1, cerah);
rule17 = 255;
mrule17 = min(panas1, mendung);
rule18 = 255;
mrule18 = min(panas1, berawan);
rule19 = 100;
mrule19 = min(panas1, cerah);
jumlah = (mrule1 * rule1) + (mrule2 * rule2) + (mrule3 *
rule3) + (mrule4 * rule4) + (mrule5 * rule5)+ (mrule6 *
rule6)+ (mrule7 * rule7)+ (mrule8 * rule8)+ (mrule9 *
rule9);
jumlah2 = (mrule1 + mrule2 + mrule3 + mrule4 + mrule5 +
mrule6 + mrule7 + mrule8 + mrule9);
out = jumlah/jumlah2;
jumlah1 = (mrule11 * rule11) + (mrule12 * rule12) + (mrule13 *
rule13) + (mrule14 * rule14) + (mrule15 * rule15)+ (mrule16 *
rule16)+ (mrule17 * rule17)+ (mrule18 * rule18)+ (mrule19 *
rule19);
jumlah12 = (mrule11 + mrule12 + mrule13 + mrule14 + mrule15 +
mrule16 + mrule17 + mrule18 + mrule19);
out1 = jumlah1/jumlah12;
}
void kodular (){
if ((millis()-tempo)>intervallo)
{
suhuh1 = float(dht1.readTemperature());
Bluetooth.print(suhuh1);
Bluetooth.print("-");
cahaya1 = float(lightMeter.readLightLevel());
Bluetooth.print(cahaya1);
Bluetooth.print("-");
Bluetooth.print(out);
Bluetooth.println("-");
//fuzzzy2
suhuh2 = float(dht2.readTemperature());
Bluetooth.print(suhuh2);
Bluetooth.print("-");
cahaya1 = float(lightMeter.readLightLevel());
Bluetooth.print(cahaya1);
Bluetooth.print("-");
Bluetooth.print(out1);
Bluetooth.println("-");
tempo = millis();
}
}
void setup() {
Serial.begin(9600);
dht1.begin();
dht2.begin();
Wire.begin();
lightMeter.begin();
Bluetooth.begin(9600);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
float suhuh1 = dht1.readTemperature();
float suhuh2 = dht2.readTemperature();
float cahaya1 = lightMeter.readLightLevel();
kodular();
suhu = suhuh1;
suhu1 = suhuh2;
cahaya = cahaya1;
rule();
int button1State = digitalRead(button1Pin);
int button2State = digitalRead(button2Pin);
int button3State = digitalRead(button3Pin);
if (button1State == HIGH && lastButton1State == LOW) {
ledOn = !ledOn; // Nyalakan/matikan buzzer
if (ledOn) {
digitalWrite(ledPin, HIGH); // Nyalakan buzzer
Serial.println("++++Suhu Dingin++++");
Serial.print("suhu :");
Serial.println(suhuh1);
Serial.print("cahaya :");
Serial.println(cahaya1);
Serial.print("dingin : ");
Serial.print(dingin);
Serial.print(" hangat : ");
Serial.print(hangat);
Serial.print(" panas : ");
Serial.println(panas );
Serial.print("mendung : ");
Serial.print(mendung );
Serial.print(" berawan : ");
Serial.print(berawan );
Serial.print(" cerah : ");
Serial.println(cerah );
Serial.print("Hasil DeFuzzy: ");
Serial.println(out);
} else {
digitalWrite(ledPin, LOW); // Matikan buzzer
}
}
if (button2State == HIGH && lastButton2State == LOW) {
buzzerOn = !buzzerOn; // Nyalakan/matikan buzzer
if (buzzerOn) {
digitalWrite(buzzerPin, HIGH); // Nyalakan buzzer
Serial.println("++++Suhu Panas++++");
Serial.print("suhu 1:");
Serial.println(suhuh2);
Serial.print("cahaya :");
Serial.println(cahaya1);
Serial.print("dingin : ");
Serial.print(dingin1);
Serial.print(" hangat : ");
Serial.print(hangat1);
Serial.print(" panas : ");
Serial.println(panas1);
Serial.print("mendung : ");
Serial.print(mendung );
Serial.print(" berawan : ");
Serial.print(berawan );
Serial.print(" cerah : ");
Serial.println(cerah );
Serial.print("Hasil DeFuzzy: ");
Serial.println(out1);
} else {
digitalWrite(buzzerPin, LOW); // Matikan buzzer
}
}
if (button3State == HIGH && lastButton3State == LOW) {
buzzerOn = !buzzerOn; // Nyalakan/matikan buzzer
ledOn = !ledOn; // Nyalakan/matikan buzzer
if (ledOn && buzzerOn) {
digitalWrite(buzzerPin, HIGH);
digitalWrite(ledPin, HIGH);
Serial.println("++++Suhu Dingin++++");
Serial.print("suhu :");
Serial.println(suhuh1);
Serial.print("cahaya :");
Serial.println(cahaya1);
Serial.print("dingin : ");
Serial.print(dingin);
Serial.print(" hangat : ");
Serial.print(hangat);
Serial.print(" panas : ");
Serial.println(panas );
Serial.print("mendung : ");
Serial.print(mendung );
Serial.print(" berawan : ");
Serial.print(berawan );
Serial.print(" cerah : ");
Serial.println(cerah );
Serial.print("Hasil DeFuzzy: ");
Serial.println(out);
Serial.println("++++Suhu Panas++++");
Serial.print("suhu 1:");
Serial.println(suhuh2);
Serial.print("cahaya :");
Serial.println(cahaya1);
Serial.print("dingin : ");
Serial.print(dingin1);
Serial.print(" hangat : ");
Serial.print(hangat1);
Serial.print(" panas : ");
Serial.println(panas1);
Serial.print("mendung : ");
Serial.print(mendung );
Serial.print(" berawan : ");
Serial.print(berawan );
Serial.print(" cerah : ");
Serial.println(cerah );
Serial.print("Hasil DeFuzzy: ");
Serial.println(out1);
} else {
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
}
}
lastButton1State = button1State;
lastButton2State = button2State;
lastButton3State = button3State;
}