/*
SUN DETACTOR
*/
#include <LiquidCrystal_I2C.h>//lcd
#include <Servo.h>//servo which is suis
#include "DHT.h"//temperature and humidity
#define DHTPIN 13 // DHT PIN 13
#define DHTTYPE DHT22 // DHT22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
#define LDR_PIN 2 //photoresistor
#define C 2100
#define D 1870
#define E 1670
#define f 1580 // Does not seem to like capital F
#define G 1400
// Define a special note, 'R', to represent a rest
#define R 0
Servo myservo;
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd(0x27, 20, 4);
int melody[] = {E, E, E, R,
E, G, C, D, E, R,
f, f, f,f, f, E, E,E, E, D ,D,E, D, R, G ,R };
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.
// Set overall tempo
long tempo = 500;
// Set length of pause between notes
int pause = 10;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES
// Initialize core variables
int tone_ = 0;
int beat = 0;
long duration = 0;
int speakerOut = 9;
// Do we want debugging on serial out? 1 for yes, 0 for no
int DEBUG = 1;
void setup() {
Serial.begin(9600);
pinMode(LDR_PIN, INPUT);
lcd.begin(20, 4);
Serial.println("System Monitoring Weather for Laundry");
dht.begin();
myservo.attach(3);
pinMode(speakerOut, OUTPUT);
if (DEBUG)
{
Serial.begin(9600); // Set serial out if we want debugging
}
lcd.init();
lcd.backlight();
}
void playTone() {
long elapsed_time = 0;
if (tone_ > 0) { // if this isn't a Rest beat, while the tone has
digitalWrite(LDR_PIN, LOW);// played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone_ / 2);
// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone_ / 2);
// Keep track of how long we pulsed
elapsed_time += (tone_);
}
digitalWrite(LDR_PIN, HIGH);
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}
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));
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
delay(1000);
lcd.setCursor(0, 0);
lcd.print("Cuaca: ");
if (lux >= 50 && lux <= 400)
{
Serial.println("Status: Pick me up! It is about to rain");
lcd.print("Mendung ");
}
else if (lux > 400 )
{
Serial.println("Status: Dry your clothes ");
lcd.print("Panas terik ");
}
else if (lux < 50)
{
Serial.println("Status: Remove your clothes ");
lcd.print("Malam ");
}
lcd.setCursor(0, 1);
lcd.print("Lux: ");
lcd.print(lux);
lcd.print(" ");
lcd.print("Suhu:");
lcd.print(t);
lcd.println(" C");
lcd.scrollDisplayLeft();
delay(50);
/*Serial.print("Suhu: ");
Serial.print(t);
Serial.print(" *C\t");
Serial.print("Kelembapan: ");
Serial.print(h);
Serial.println(" %");*/
if (lux >= 50 && lux <= 400)
{
for (int i=0; i<MAX_COUNT; i++) {
tone_ = melody[i];
beat = 50;
duration = beat * tempo; // Set up timing
playTone();
// A pause between notes...
delayMicroseconds(pause);
Serial.println("ROOF: ON");
//myservo.write(135); // Turn Servo Ke kanan 135 degrees
//delay(1000);
myservo.write(180); // Turn Servo ke kanan 180 degrees
delay(250);
myservo.write(90); // Turn Servo ke posisi center position (90 degrees)
delay(250);
}
}
else if (t < 25)
{
for (int i=0; i<MAX_COUNT; i++) {
tone_ = melody[i];
beat = 50;
duration = beat * tempo; // Set up timing
playTone();
// A pause between notes...
delayMicroseconds(pause);
Serial.println("ROOF: ON");
//myservo.write(135); // Turn Servo Ke kanan 135 degrees
//delay(1000);
myservo.write(180); // Turn Servo ke kanan 180 degrees
delay(250);
myservo.write(90); // Turn Servo ke posisi center position (90 degrees)
delay(250);
}
}
else if (lux < 400 && t > 30 )
{
Serial.println("ROOF: ON");
//myservo.write(135); // Turn Servo Ke kanan 135 degrees
//delay(1000);
myservo.write(180); // Turn Servo ke kanan 180 degrees
delay(250);
myservo.write(90); // Turn Servo ke posisi center position (90 degrees)
delay(250);
}
else if (lux > 400 || t > 30 )
{
Serial.println("ROOF: OFF");
//myservo.write(45); // Turn Servo ke kiri 45 degrees
//delay(1000);
myservo.write(0); // Turn Servo ke kiri to 0 degrees
delay(250);
myservo.write(90); // Turn Servo ke posisi center position (90 degrees)
delay(250);
}
else if (lux < 50)
{
Serial.println("ROOF: ON");
//myservo.write(135); // Turn Servo Ke kanan 135 degrees
//delay(500);
myservo.write(180); // Turn Servo ke kanan 180 degrees
delay(250);
myservo.write(90); // Turn Servo ke posisi center position (90 degrees)
delay(200);
}
}