Advertisement
safwan092

Untitled

Apr 6th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "RTClib.h"
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. RTC_DS3231 rtc;
  6. LiquidCrystal_I2C lcd(0x27, 16, 2);
  7.  
  8. int led = 7;
  9. int ldr = A0;
  10. int ldr_status = 0;
  11.  
  12. void setup() {
  13. rtc.begin();
  14. lcd.init();
  15. lcd.backlight();
  16. lcd.setCursor(0, 0);
  17. pinMode(led, OUTPUT);
  18. digitalWrite(led, LOW);
  19. pinMode(ldr, INPUT);
  20. if (rtc.lostPower()) {
  21. Serial.println("RTC lost power, let's set the time!");
  22. // When time needs to be set on a new device, or after a power loss, the
  23. // following line sets the RTC to the date & time this sketch was compiled
  24. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  25. // This line sets the RTC with an explicit date & time, for example to set
  26. // January 21, 2014 at 3am you would call:
  27. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  28. }
  29.  
  30. // When time needs to be re-set on a previously configured device, the
  31. // following line sets the RTC to the date & time this sketch was compiled
  32. // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  33. // This line sets the RTC with an explicit date & time, for example to set
  34. // January 21, 2014 at 3am you would call:
  35. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  36. }
  37.  
  38. void loop() {
  39. ldr_status = analogRead(A0);
  40. DateTime now = rtc.now();
  41. lcd.setCursor(0, 0);
  42. lcd.print(now.year());
  43. lcd.print("/");
  44. lcd.print(now.month());
  45. lcd.print("/");
  46. lcd.print(now.day());
  47. lcd.print(" ");
  48. lcd.print(now.hour());
  49. lcd.print(":");
  50. lcd.print(now.minute());
  51. lcd.print(":");
  52. lcd.print(now.second());
  53. lcd.print(" ");
  54. lcd.setCursor(0, 1);
  55. lcd.print(ldr_status);
  56. lcd.print(" ");
  57. lcd.setCursor(4, 1);
  58. lcd.print(" LED:");
  59.  
  60. if (ldr_status < 700) {
  61. lcd.setCursor(9, 1);
  62. lcd.print("ON ");
  63. digitalWrite(led, HIGH);
  64. }
  65. else {
  66. // 18 - 19 - 20 - 21 - 22 - 23 - 0 - 1 - 2 - 3 - 4 - 5 - 6
  67. 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 ) {
  68. lcd.setCursor(9, 1);
  69. lcd.print("ON ");
  70. digitalWrite(led, HIGH);
  71.  
  72. }
  73. else {
  74. lcd.setCursor(9, 1);
  75. lcd.print("OFF");
  76. digitalWrite(led, LOW);
  77. }
  78. }
  79. delay(1000);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement