Advertisement
Guest User

RTC LDR

a guest
Nov 14th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include "RTClib.h"
  3.  
  4. RTC_DS1307 rtc;
  5. int ldr_pin = 7;
  6. int int1 = 9;
  7. int int2 = 10;
  8.  
  9. void setup()
  10. {
  11.   Serial.begin(9600);
  12.   Wire.begin();
  13.   rtc.begin();
  14.  
  15.   if (! rtc.isrunning())
  16.   {
  17.     Serial.println("RTC is NOT running!");
  18.   }
  19.   //Any pin. I have used Pin 4
  20.   pinMode ( 9, OUTPUT); // Light 1
  21.   pinMode (10, OUTPUT);  // Light 2
  22.   pinMode ( ldr_pin, INPUT);
  23.   Serial.begin (9600);
  24.  
  25.  
  26. }
  27.  
  28. void loop()
  29. {
  30.   DateTime now = rtc.now();
  31.   Serial.print(now.year(), DEC);
  32.   Serial.print("/");
  33.   Serial.print(now.month(), DEC);
  34.   Serial.print("/");
  35.   Serial.print(now.day(), DEC);
  36.   Serial.print(" (");
  37.   Serial.print(now.hour(), DEC);
  38.   Serial.print(":");
  39.   Serial.print(now.minute(), DEC);
  40.   Serial.print(":");
  41.   Serial.print(now.second(), DEC);
  42.   Serial.print(")");
  43.   Serial.println();
  44.  
  45.   Serial.println();
  46.   delay(1000);
  47.  
  48.   //The time is set as 24 hours
  49.   //Pin 4 get high at 11pm and low at 6am
  50.  
  51.    if (now.hour() == 22 && now.minute() == 4 && now.second() == 0  || digitalRead( ldr_pin ) == 1) // if detect time set OR input in light sensor, relay will turn on light
  52.   {
  53.     digitalWrite(9, LOW); // relay turn on Light 1
  54.     digitalWrite (10, LOW); // relay turn on Light 2
  55.   }
  56.   else if(now.hour() == 22 && now.minute() == 5 && now.second() == 0 || digitalRead( ldr_pin ) == 0) // if detect time set OR input in light sensor, relay will turn off light
  57. {
  58.     digitalWrite(9, HIGH); // relay turn off Light 1
  59.     digitalWrite (10, HIGH); // relay turn off Light 2
  60.   }
  61.  
  62.   Serial.println( digitalRead( ldr_pin ));
  63.   delay(100);
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement