first commit

This commit is contained in:
Putri_Marwatus 2024-07-24 11:58:52 +07:00
commit 8d4449021c
2 changed files with 154 additions and 0 deletions

149
MainCode.ino Normal file
View File

@ -0,0 +1,149 @@
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
// Set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// Set LCD address, number of columns and rows
// If you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
// Define the DHT sensor pin
const int dhtPin = 2;
// Initialize DHT sensor
DHT dht(dhtPin, DHT21);
// Mendefinisikan pin sensor cahaya
const int lightSensorPin = 3;
// Mendefinisikan pin sensor hujan
const int rainSensorPin = 4;
// Mendefinisikan pin relay untuk lampu
const int lampRelayPin = 5;
// Mendefinisikan pin untuk mengontrol motor (IN1, IN2)
const int motorIn1Pin = 6;
const int motorIn2Pin = 7;
// Variables to track the last state
bool lastRainSensorValue = HIGH;
bool lastLightSensorValue = LOW;
bool motorRunning = false;
void setup() {
// Initialize LCD
Serial.begin(9600);
lcd.init();
// Turn on LCD backlight
lcd.backlight();
dht.begin();
// Mengatur pin sensor hujan sebagai input
pinMode(rainSensorPin, INPUT);
// Mengatur pin relay untuk lampu sebagai output
pinMode(lampRelayPin, OUTPUT);
// Matikan lampu secara default
digitalWrite(lampRelayPin, LOW);
// Mengatur pin untuk mengontrol motor sebagai output
pinMode(motorIn1Pin, OUTPUT);
pinMode(motorIn2Pin, OUTPUT);
stopMotor();
}
void stopMotor() {
digitalWrite(motorIn1Pin, LOW);
digitalWrite(motorIn2Pin, LOW);
}
void loop() {
// Read temperature and humidity from DHT sensor
float temperatureC = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if DHT sensor reading is successful
if (isnan(temperatureC) || isnan(humidity)) {
// Print error message if reading failed
Serial.println(F("Gagal Baca Data DHT sensor!"));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Failed to read DHT");
lcd.setCursor(0, 1);
lcd.print("sensor data!");
delay(2000);
return;
}
Serial.print("Kelembaban: ");
Serial.println(humidity);
Serial.print("Suhu: ");
Serial.println(temperatureC);
// Membaca nilai dari sensor hujan
int rainSensorValue = digitalRead(rainSensorPin);
// Membaca nilai dari sensor cahaya
int lightSensorValue = digitalRead(lightSensorPin);
// Clear LCD sebelum menampilkan data baru
lcd.clear();
// Check if the motor needs to run based on the sensor values and only run it once
if (!motorRunning && (rainSensorValue == LOW || lightSensorValue == HIGH)) {
// Putar motor searah jarum jam (clockwise)
digitalWrite(motorIn1Pin, HIGH);
digitalWrite(motorIn2Pin, LOW);
delay(2000); // Gerakkan motor selama 2 detik
stopMotor();
motorRunning = true; // Set motor running flag
lcd.setCursor(9, 0);
lcd.print("M: CW "); // Print status motor
} else if (!motorRunning && (rainSensorValue != LOW && lightSensorValue != HIGH)) {
// Putar motor berlawanan arah jarum jam (counter-clockwise)
digitalWrite(motorIn1Pin, LOW);
digitalWrite(motorIn2Pin, HIGH);
delay(2000); // Gerakkan motor selama 2 detik
stopMotor();
motorRunning = true; // Set motor running flag
lcd.setCursor(9, 0);
lcd.print("M: CCW "); // Print status motor
}
// Kontrol lampu berdasarkan kondisi suhu, hujan, dan cahaya
if (temperatureC < 25 || rainSensorValue == LOW || lightSensorValue == HIGH) {
// Hidupkan lampu jika suhu < 45, atau hujan terdeteksi, atau cuaca gelap
digitalWrite(lampRelayPin, LOW);
lcd.setCursor(8, 1);
lcd.print("L: ON"); // Print status lampu
} else {
// Matikan lampu jika kondisi lain
digitalWrite(lampRelayPin, HIGH);
lcd.setCursor(8, 1);
lcd.print("L: OFF"); // Print status lampu
}
// Tampilkan status suhu, hujan, lampu, dan motor di LCD
lcd.setCursor(0, 0);
lcd.print("Suhu:");
lcd.print(temperatureC);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Rain:");
if (rainSensorValue == LOW) {
lcd.print("Yes");
} else {
lcd.print("No");
}
// Reset the motorRunning flag if conditions have changed
if ((lastRainSensorValue != rainSensorValue) || (lastLightSensorValue != lightSensorValue)) {
motorRunning = false;
}
// Update the last sensor values
lastRainSensorValue = rainSensorValue;
lastLightSensorValue = lightSensorValue;
delay(2000);
}

5
Penyesuian.ino Normal file
View File

@ -0,0 +1,5 @@
/***
* TA Putri
* Sesuaikan parameter suhu untuk otomasi lampu, Line 113
* delay perputaran motor menyesuaikan dengan panjang tali yang ditarik/diulur. Line 96 dan 105
*/