Advertisement
safwan092

Untitled

Dec 2nd, 2023
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. Project: Use DS3231 RTC to print Time, Date.. Use Thermistor for temp. Ability to set Alarm.
  2. */
  3.  
  4. //-----------------------------------------------------------------------------
  5.  
  6. // Define variables.
  7.  
  8. #include <DS3231.h> //Library for RTC
  9.  
  10. #include <Wire.h> //Allows comunication with TWI (Two Wire Interface)
  11.  
  12. #include <LiquidCrystal.h> //Used for DS3231 and thermister
  13.  
  14. LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Pins used by the LCD
  15.  
  16. DS3231 rtc(SDA, SCL);
  17.  
  18. Time t;
  19.  
  20. #define buz 11
  21.  
  22. int tempPin = 0;
  23.  
  24. int Hor;
  25.  
  26. int Min;
  27.  
  28. int Sec;
  29.  
  30. int tempC;
  31. int tempF;
  32.  
  33. int tempCDS3231;
  34. int tempFDS3231;
  35.  
  36.  
  37. //-------------------------------------------------------------------------
  38. // Used to fix Date, Time, Day of Week
  39.  
  40. // Uncomment to adjust. Be sure to // back once fixed.
  41.  
  42. //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY
  43.  
  44. //rtc.setTime(01, 33, 00); // Set the time to 12:00:00 (24hr format)
  45.  
  46. //rtc.setDate(23, 8, 2018); // Set the date to August 8th, 2018
  47.  
  48. //--------------------------------------------------------------------------
  49.  
  50.  
  51. void setup()
  52.  
  53. {
  54.  
  55. Wire.begin();
  56.  
  57. rtc.begin();
  58.  
  59. Serial.begin(9600);
  60.  
  61. pinMode(buz, OUTPUT);
  62.  
  63. lcd.begin(16,2);
  64.  
  65. lcd.setCursor(0,0);
  66.  
  67. t = rtc.getTime();
  68.  
  69. Hor = t.hour;
  70.  
  71. Min = t.min;
  72.  
  73. Sec = t.sec;
  74.  
  75.  
  76.  
  77. }
  78.  
  79.  
  80. //---------------------------------------------------------------------
  81.  
  82.  
  83. void loop()
  84.  
  85. {
  86. int tempReading = analogRead(tempPin);
  87. // This is OK
  88. double tempK = log(10000.0 * ((1024.0 / tempReading - 1)));
  89. tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK ); // Temp Kelvin
  90. int tempC = tempK - 273.15; // Convert Kelvin to Celcius
  91. int tempF = (tempC * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  92.  
  93. //-------------------------------------------------------------------------------------------
  94. //Can change the ABOVE tempC and tempF "int" to "float" this can give you temperatures with two decimal points.
  95. //Can also use "lcd.print(tempF, 1);" BELOW to print with one decimal place or "lcd.print(tempF, 0);" // print without decimal place
  96. //-------------------------------------------------------------------------------------------
  97.  
  98. lcd.setCursor(0,0); //Top line of the LCD
  99. lcd.print(" ");
  100. lcd.print(rtc.getTimeStr());
  101. lcd.print(" ");
  102. lcd.setCursor(0,1);
  103. lcd.print(" ");
  104. lcd.print(rtc.getDateStr());
  105.  
  106.  
  107. //----------------------------------------------------------
  108.  
  109. lcd.setCursor(0,1); //Second line of the LCD
  110.  
  111. tempFDS3231 = (tempCDS3231 * 1.8) + 32.0; // Convert C to F
  112. tempCDS3231 = (rtc.getTemp());
  113.  
  114. lcd.setCursor(0,1);
  115. lcd.print(tempF);
  116. lcd.print((char)223); //This creates a Degree symbol
  117. lcd.print("F ");
  118. lcd.print(rtc.getDateStr());
  119.  
  120.  
  121. //--------------------------------------------------------------------------
  122.  
  123. t = rtc.getTime();
  124.  
  125. Hor = t.hour;
  126.  
  127. Min = t.min;
  128.  
  129. Sec = t.sec;
  130.  
  131.  
  132. //-------------------------------------------------------------------------------------------
  133. //Use the BELOW "if" statement to set your desired alarm time
  134. if( Hor == 10 && (Min == 00 || Min == 00)) //Comparing the current time with the Alarm time
  135. //-------------------------------------------------------------------------------------------
  136.  
  137. {
  138. Buzzer();
  139.  
  140. Buzzer();
  141.  
  142. lcd.clear();
  143.  
  144. lcd.print("Alarm ON");
  145.  
  146. lcd.setCursor(0,1);
  147.  
  148. lcd.print("Wake Up!!");
  149.  
  150. Buzzer();
  151.  
  152. Buzzer();
  153.  
  154.  
  155. }
  156. delay(1000);
  157.  
  158. }
  159.  
  160.  
  161.  
  162.  
  163. void Buzzer()
  164.  
  165. {
  166.  
  167. digitalWrite(buz,HIGH);
  168.  
  169. delay(500);
  170.  
  171. digitalWrite(buz, LOW);
  172.  
  173. delay(500);
  174.  
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement