#define pinServoPagar A0
#define waktuBukaServo 2000//milidetik
#define servoBuka 20//derajat
#define servoTutup 60//derajat
#define waktuPagar1 DateTime(0, 1, 1, 8, 0, 0, 0)//jam 8 pagi
#define waktuPagar2 DateTime(0, 1, 1, 15, 0, 0, 0)//jam 5 sore
#define echoPin 12
#define trigPin 13
int redLed = 11;
int yellowLed = 10;
int greenLed = 9;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "Sodaq_DS3231.h"
#include <Servo.h>
long duration;
int distance;
LiquidCrystal_I2C lcd(0x3F, 16, 2);//coba juga 0x27
Servo servoPagarOtomatis;
byte detikSebelumnya;
char buf[17];
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(greenLed, OUTPUT);
Serial.begin(9600);
Serial.println("Pagar Otomatis");
Serial.println("45 Shafin Adinata");
servoPagarOtomatis.attach(pinServoPagar);
servoPagarOtomatis.write(servoTutup);
Wire.begin();
rtc.begin();
//DateTime dt(2011, 11, 10, 15, 18, 0, 5); // set tanggal dan waktu (format): tahun, bulan tanggal, jam, menit, detik, hari (1=minggu, 7=sabtu)
//rtc.setDateTime(dt);
Wire.beginTransmission(0x3F);
if (Wire.endTransmission())
{
lcd = LiquidCrystal_I2C(0x27, 16, 2);
}
lcd.init();
lcd.backlight();
lcd.print("Pagar Otomatis");
lcd.setCursor(0, 1);
lcd.print("Shafin Adinata");
delay(3000);
lcd.clear();
Serial.println("Sistem mulai");
sprintf(buf, "Set waktu 1 = %02d:%02d (%lu)", waktuPagar1.hour(), waktuPagar1.minute(), waktuPagar1.get());
Serial.println(buf);
sprintf(buf, "Set waktu 2 = %02d:%02d (%lu)", waktuPagar2.hour(), waktuPagar2.minute(), waktuPagar2.get());
Serial.println(buf);
}
void loop() {
DateTime now = rtc.now();
if (detikSebelumnya != now.second())
{
sprintf(buf, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
lcd.setCursor(4, 0);
lcd.print(buf);
Serial.print(buf);
detikSebelumnya = now.second();
uint32_t epoch = now.get() % 86400;//hanya jam menit detik
if ((epoch == waktuPagar1.get()) ||
(epoch == waktuPagar2.get()))
{
char buf[17];
sprintf(buf, "Pagar = %02d:%02d", now.hour(), now.minute());
lcd.setCursor(0, 1);
lcd.print(buf);
Serial.println(buf);
servoPagarOtomatis.write(servoBuka);
delay(waktuBukaServo);
servoPagarOtomatis.write(servoTutup);
}
}
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if(distance>1 && distance <=150){
digitalWrite(redLed, HIGH);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, LOW);
}
if(distance>151 && distance <=200){
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, HIGH);
digitalWrite(greenLed, LOW);
}
if(distance>201 && distance <=300){
digitalWrite(redLed, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(greenLed, HIGH);
}
}