Hi,
I would like to ask why my application is not working as I intended. The buzzer I am using is already on when the alarm button is pressed, but the labels for temperature and humidity in the application do not change according to the temperature read from the sensor. Additionally, my servo does not move when the open and close buttons are pressed. Can someone help me?
Here is the design of the application:
Here are the code blocks:
And this is the program for Arduino that I am using:
#include <DHT.h>
#include <DHT_U.h>
#include <SoftwareSerial.h>
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
SoftwareSerial bt(8, 9); // RX, TX
// Definisi pin
#define BUZZER 3
#define SERVO 2
// Definisi sensor DHT22
#define DHTPIN 12
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Definisi LCD dengan modul I2C
#define I2C_ADDR 0x20 // Alamat LCD sesuai dengan yang Anda sebutkan
#define LCD_COLS 16 // Jumlah kolom LCD
#define LCD_ROWS 2 // Jumlah baris LCD
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);
// Inisiasi variabel
String text;
Servo myservo;
void setup() {
pinMode(BUZZER, OUTPUT);
Serial.begin(9600);
bt.begin(9600);
myservo.attach(SERVO); // Menginisialisasi servo pada pin yang sesuai
dht.begin();
// Inisialisasi LCD
lcd.begin();
}
void loop() {
// Membaca suhu dan kelembapan dari sensor DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Debugging: cek apakah pembacaan sensor berhasil
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
lcd.setCursor(6, 0);
lcd.print("Error");
lcd.setCursor(10, 1);
lcd.print("Error");
} else {
// Menampilkan nilai suhu dan kelembapan pada LCD
lcd.setCursor(0, 0);
lcd.print("Suhu:");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity:");
lcd.print(humidity);
lcd.print("%");
bt.print(temperature); //send distance to MIT App
bt.print(";");
bt.print(humidity); //send distance to MIT App
bt.println(";");
}
// Mengecek input serial Bluetooth
while (Serial.available()) {
delay(10);
char c = Serial.read();
text += c;
}
if (text.length() > 0) {
Serial.println(text);
if (text == "OPEN") {
// Untuk memutar servo 90 derajat ke kanan
myservo.write(90);
}
if (text == "CLOSE") {
// Untuk memutar servo 90 derajat ke kiri
myservo.write(0);
}
if (text == "MALING") {
// Untuk menyalakan buzzer
digitalWrite(BUZZER, HIGH);
} else {
digitalWrite(BUZZER, LOW);
}
text = "";
}
delay(2000); // Delay untuk pengambilan data yang lebih lambat
}