Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "RTClib.h"
- #include <LiquidCrystal_I2C.h>
- RTC_DS3231 rtc;
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- int led = 7;
- int ldr = A0;
- int ldr_status = 0;
- void setup() {
- rtc.begin();
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0, 0);
- pinMode(led, OUTPUT);
- digitalWrite(led, LOW);
- pinMode(ldr, INPUT);
- if (rtc.lostPower()) {
- Serial.println("RTC lost power, let's set the time!");
- // When time needs to be set on a new device, or after a power loss, the
- // following line sets the RTC to the date & time this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- }
- // When time needs to be re-set on a previously configured device, the
- // following line sets the RTC to the date & time this sketch was compiled
- // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- }
- void loop() {
- ldr_status = analogRead(A0);
- DateTime now = rtc.now();
- lcd.setCursor(0, 0);
- lcd.print(now.year());
- lcd.print("/");
- lcd.print(now.month());
- lcd.print("/");
- lcd.print(now.day());
- lcd.print(" ");
- lcd.print(now.hour());
- lcd.print(":");
- lcd.print(now.minute());
- lcd.print(":");
- lcd.print(now.second());
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print(ldr_status);
- lcd.print(" ");
- lcd.setCursor(4, 1);
- lcd.print(" LED:");
- if (ldr_status < 700) {
- lcd.setCursor(9, 1);
- lcd.print("ON ");
- digitalWrite(led, HIGH);
- }
- else {
- // 18 - 19 - 20 - 21 - 22 - 23 - 0 - 1 - 2 - 3 - 4 - 5 - 6
- if (now.hour() == 18 || now.hour() == 19 || now.hour() == 20 || now.hour() == 21 || now.hour() == 22 || now.hour() == 23 || now.hour() == 0 || now.hour() == 1 || now.hour() == 2 || now.hour() == 3 || now.hour() == 4 || now.hour() == 5 ) {
- lcd.setCursor(9, 1);
- lcd.print("ON ");
- digitalWrite(led, HIGH);
- }
- else {
- lcd.setCursor(9, 1);
- lcd.print("OFF");
- digitalWrite(led, LOW);
- }
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement