Advertisement
Guest User

DS3231 Alarm

a guest
Aug 12th, 2016
2,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. // DS3231_Serial_Hard
  2. // Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
  3. // web: http://www.RinkyDinkElectronics.com/
  4. //
  5. // A quick demo of how to use my DS3231-library to
  6. // retrieve time- and date-data for you to manipulate.
  7. //
  8. // To use the hardware I2C (TWI) interface of the Arduino you must connect
  9. // the pins as follows:
  10. //
  11. // Arduino Uno/2009:
  12. // ----------------------
  13. // DS3231: SDA pin -> Arduino Analog 4 or the dedicated SDA pin
  14. // SCL pin -> Arduino Analog 5 or the dedicated SCL pin
  15. //
  16. // Arduino Leonardo:
  17. // ----------------------
  18. // DS3231: SDA pin -> Arduino Digital 2 or the dedicated SDA pin
  19. // SCL pin -> Arduino Digital 3 or the dedicated SCL pin
  20. //
  21. // Arduino Mega:
  22. // ----------------------
  23. // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA pin
  24. // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL pin
  25. //
  26. // Arduino Due:
  27. // ----------------------
  28. // DS3231: SDA pin -> Arduino Digital 20 (SDA) or the dedicated SDA1 (Digital 70) pin
  29. // SCL pin -> Arduino Digital 21 (SCL) or the dedicated SCL1 (Digital 71) pin
  30. //
  31. // The internal pull-up resistors will be activated when using the
  32. // hardware I2C interfaces.
  33. //
  34. // You can connect the DS3231 to any available pin but if you use any
  35. // other than what is described above the library will fall back to
  36. // a software-based, TWI-like protocol which will require exclusive access
  37. // to the pins used, and you will also have to use appropriate, external
  38. // pull-up resistors on the data and clock signals.
  39. //
  40.  
  41. #include <DS3231.h>
  42.  
  43. // Init the DS3231 using the hardware interface
  44. DS3231 rtc(SDA, SCL);
  45.  
  46. // Init a Time-data structure
  47. Time t;
  48.  
  49. void setup()
  50. {
  51. // Setup Serial connection
  52. Serial.begin(115200);
  53. // Uncomment the next line if you are using an Arduino Leonardo
  54. //while (!Serial) {}
  55.  
  56. // Initialize the rtc object
  57. rtc.begin();
  58.  
  59. // The following lines can be uncommented to set the date and time
  60. rtc.setDOW(SUNDAY); // Set Day-of-Week to SUNDAY
  61. rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
  62. rtc.setDate(1, 1, 2016); // Set the date to DD/MM/YYYY
  63. }
  64.  
  65. void loop()
  66. {
  67.  
  68. t = rtc.getTime(); // Get data from the DS3231
  69.  
  70. // Send date over serial connection
  71. Serial.print("Date: ");
  72. Serial.print(t.date, DEC);
  73. Serial.print("/");
  74. Serial.print(t.mon, DEC);
  75. Serial.print("/");
  76. Serial.print(t.year, DEC);
  77. Serial.println();
  78.  
  79. // Send Day-of-Week and time
  80. Serial.print("Day of Week: ");
  81. Serial.print(t.dow, DEC);
  82. Serial.println();
  83. Serial.print("Time: ");
  84. Serial.print(t.hour, DEC);
  85. Serial.print(":");
  86. Serial.print(t.min, DEC);
  87. Serial.print(":");
  88. Serial.print(t.sec, DEC);
  89. Serial.println();
  90. Serial.println("--------------------------------");
  91.  
  92. delay(1000); //Delay is for displaying the time in 1 second interval.
  93.  
  94. if (t.hour == 14 && t.min == 32 && t.sec == 53) //Setting alarm/timer at every 2:32:53pm, in other words you can insert t.dow for every Thursday?, t.date for specific date?
  95. { digitalWrite(9,HIGH); //Lets say that your component is wired to pin 9
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement