//Nama : M FAJAR RAFLI
//NIM : 191020100016
//Kelas :7A1
// LCD1602 to Arduino Uno connection example
#include <LiquidCrystal.h>
#include "DHT.h"
#include "Servo.h" // Bibliothek für Servos laden
#define DHT_PIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define LDR_PIN 2
#define SPEAKER_PIN 4
DHT dht(DHT_PIN, DHTTYPE);
Servo myservo;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
int val;
void setup() {
pinMode(LDR_PIN, INPUT);
Serial.begin(9600);
lcd.begin(16, 2);
dht.begin();
lcd.setCursor(5,0);lcd.print("Arduino");
lcd.setCursor(3,1);lcd.print("T+H Monitor");
delay(2000);
lcd.clear();
pinMode(SPEAKER_PIN, OUTPUT);
myservo.attach(5); // attaches the servo on pin 9 to the servo object
}
void loop() {
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.setCursor(2, 0);
lcd.print("Lux: ");
lcd.print(lux);
lcd.print(" ");
delay(100);
int ldrStatus = analogRead(LDR_PIN);
if (lux >= 400) {
tone(SPEAKER_PIN, 100);
noTone(SPEAKER_PIN);
delay(100);
Serial.println("----------- ALARM ACTIVATED -----------");
}
else {
noTone(SPEAKER_PIN);
Serial.println("ALARM DEACTIVATED");
}
delay(100);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
int err;
if (isnan(h) || isnan(t) ) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print(t); //Temperature data
Serial.print(",");//Data splitter
Serial.println(h); //Humidity data
lcd.clear();
lcd.setCursor(0,1);lcd.print("T:");lcd.print(t);
lcd.print(" C");//LCD line 1 print temperature
lcd.setCursor(9,1);lcd.print("H:");lcd.print(h);
lcd.print(" %");//LCD line 2 print Humidity
val = dht.readTemperature(); // reads the value of the potentiometer (value between 0 and 1023)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
}