Advertisement
Guest User

Untitled

a guest
Dec 20th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <TimeLib.h>
  2. #include <TimeAlarms.h>
  3. #include <Wire.h>
  4. #include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
  5.  
  6. const int led = 13;
  7.  
  8. void setup() {
  9. // prepare pin as output
  10. pinMode(led, OUTPUT);
  11. digitalWrite(led, LOW);
  12.  
  13. Serial.begin(9600);
  14. // wait for Arduino Serial Monitor
  15. while (!Serial) ;
  16.  
  17. // get and set the time from the RTC
  18. setSyncProvider(RTC.get);
  19. if (timeStatus() != timeSet)
  20. Serial.println("Unable to sync with the RTC");
  21. else
  22. Serial.println("RTC has set the system time");
  23.  
  24. // to test your project, you can set the time manually
  25. //setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
  26.  
  27. // create the alarms, to trigger functions at specific times
  28. Alarm.alarmRepeat(9,0,0,MorningAlarm); // 9:00am every day
  29. Alarm.alarmRepeat(11,26,0,EveningAlarm); // 19:00 -> 7:00pm every day
  30. }
  31.  
  32. void loop() {
  33. digitalClockDisplay();
  34. // wait one second between each clock display in serial monitor
  35. Alarm.delay(1000);
  36. }
  37.  
  38. // functions to be called when an alarm triggers
  39. void MorningAlarm() {
  40. // write here the task to perform every morning
  41. Serial.println("Tturn light off");
  42. digitalWrite(led, LOW);
  43. }
  44. void EveningAlarm() {
  45. // write here the task to perform every evening
  46. Serial.println("Turn light on");
  47. digitalWrite(led, HIGH);
  48. }
  49.  
  50. void digitalClockDisplay() {
  51. // digital clock display of the time
  52. Serial.print(hour());
  53. printDigits(minute());
  54. printDigits(second());
  55. Serial.println();
  56. }
  57. void printDigits(int digits) {
  58. Serial.print(":");
  59. if (digits < 10)
  60. Serial.print('0');
  61. Serial.print(digits);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement