Guest User

Untitled

a guest
Jan 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1. #include <DS3232RTC.h> // https://github.com/JChristensen/DS3232RTC
  2. #include <Timezone.h> // https://github.com/JChristensen/Timezone
  3. #include <TimeLib.h> // https://github.com/PaulStoffregen/Time
  4. #include <Streaming.h> // http://arduiniana.org/libraries/streaming/
  5. #include <Wire.h> // https://www.arduino.cc/en/Reference/Wire
  6. #include <LiquidCrystal.h> // https://www.arduino.cc/en/Reference/LiquidCrystal
  7. #include "Date.h"
  8.  
  9. // initialize the library by associating any needed LCD interface pin
  10. // with the arduino pin number it is connected to
  11. LiquidCrystal lcd(11, 12, 2, 3, 4, 5);
  12.  
  13. // constants won't change:
  14. const String days[2][7] = {{"Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat"},{"zo","ma", "di", "wo", "do", "vr", "za"}};
  15. const String months[2][12] = {{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"},{"jan","feb","mrt","mei","jun","jul","aug","sep","okt","nov","dec"}};
  16. const String translations[2][3] = {{"Temp","D","Wk"},{"Temp","D","Wk"}};
  17.  
  18. /* EXPLANATION DIFFERENT FUNCTIONS FOR CLOCK (WILL ONLY BE USED ON THE HOMEPAGE)
  19. * TIME
  20. 'h' = hours
  21. 'm' = minutes
  22. 's' = seconds
  23. 'a' = alarm 1 (not atm)
  24. 'A' = alarm 2 (not atm)
  25.  
  26. * WEATHER
  27. 'T' = temperature
  28. 'H' = humidity (no sensor atm)
  29. 'P' = dew point (no sensor atm)
  30.  
  31. * DATE
  32. 'd' = day of week
  33. 'D' = day
  34. 'M' = month
  35. 'S' = month (short string)
  36. 'Y' = year
  37. 'w' = weeknumber
  38. 'n' = daynumber
  39.  
  40. * MISCELLANEOUS
  41. 'l' = current location (need to create a array of enums/strings which can be used in Settings)
  42.  
  43. * DELIMTERS
  44. ':' = delimiter
  45. '-' = delimiter
  46. '/' = delimiter
  47. '.' = delimiter
  48. '|' = delimiter
  49. ' ' = delimiter
  50. */
  51. char homepage[4][16] = {{"h:m:s"},{"d D-M-Y"},{"T"},{"w n"}};
  52.  
  53. enum languages_t {EN, NL};
  54.  
  55. typedef struct {
  56. int hourFormat; // 12 or 24 hour format (AM/PM is not displayed)
  57. uint8_t language; // The language for several labels
  58. char degreesFormat; // Celcius or Fahrenheit
  59. boolean labels; // Display temperature, weeknumber and daynumber with label
  60. long intervalPage1; // interval at which to refresh lcd (milliseconds)
  61. long switchPages; // interval at which to switchPage 1 to 2 (milliseconds)
  62. long intervalPage2; // interval at which to switchPage 2 to 1 (milliseconds)
  63. } Settings;
  64.  
  65. Settings default_settings = {24,NL,'c',true,1000,30000,5000};
  66. Settings settings = {24,NL,'c',true,1000,30000,5000};
  67.  
  68. //typedef struct {
  69. // char abbrev[6]; // five chars max
  70. // uint8_t wk; // First, Second, Third, Fourth, or Last week of the month
  71. // uint8_t dow; // day of week, 1=Sun, 2=Mon, ... 7=Sat
  72. // uint8_t mon; // 1=Jan, 2=Feb, ... 12=Dec
  73. // uint8_t hour; // 0-23
  74. // int offset; // offset from UTC in hours
  75. //} TimeSettings;
  76. //
  77. //TimeSettings settings_DST = {"MDT", Last, Sun, Mar, 2, 2};
  78. //TimeSettings settings_STD = {"MST", Last, Sun, Oct, 2, 1};
  79.  
  80. // need to use the settings from TimeSettings and make it changeable (in function)
  81. //TimeChangeRule myDST = {settings_DST.abbrev, settings_DST.wk, settings_DST.dow, settings_DST.mon, settings_DST.hour, settings_DST.offset*60}; //Daylight time/Summertime = UTC + 2 hours
  82. //TimeChangeRule mySTD = {settings_STD.abbrev, settings_STD.wk, settings_STD.dow, settings_STD.mon, settings_STD.hour, settings_STD.offset*60}; //Standard time/Wintertime = UTC + 1 hours
  83.  
  84. TimeChangeRule myDST = {"MDT", Last, Sun, Mar, 2, 2*60}; //Daylight time/Summertime = UTC + 2 hours
  85. TimeChangeRule mySTD = {"MST", Last, Sun, Oct, 2, 1*60}; //Standard time/Wintertime = UTC + 1 hours
  86. Timezone myTZ(myDST, mySTD);
  87. TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
  88.  
  89. unsigned long previousMillis1 = 0; // will store last time lcd was updated (page 1)
  90. unsigned long oldMillis = 0; // will store last time lcd switched pages
  91. int language_id;
  92.  
  93. void setup() {
  94. Serial.begin(9600);
  95.  
  96. // set up the LCD's number of columns and rows:
  97. lcd.begin(16, 2);
  98.  
  99. // setSyncProvider() causes the Time library to synchronize with the
  100. // external RTC by calling RTC.get() every five minutes by default.
  101. setSyncProvider(RTC.get);
  102. if (timeStatus() != timeSet) lcd << ("RTC SYNC FAILED");
  103.  
  104. pinMode(13,OUTPUT);
  105. digitalWrite(13,LOW);
  106.  
  107. }
  108.  
  109. void loop() {
  110. // check to see if it's time to refresh the lcd; that is, if the difference
  111. // between the current time and last time you refreshed the lcd is bigger than
  112. // the interval at which you want to refresh the lcd.
  113. unsigned long currentMillis = millis();
  114. defineLanguageId();
  115. if (currentMillis - previousMillis1 >= settings.intervalPage1) {
  116. // save the last time you refreshed the lcd
  117. previousMillis1 = currentMillis;
  118.  
  119. // display the date and time according to the specificied order with the specified settings
  120. displayPage(0, 2);
  121. }
  122. if (currentMillis - oldMillis >= settings.switchPages) {
  123. oldMillis = currentMillis;
  124.  
  125. displayPage(2, 4);
  126.  
  127. delay(settings.intervalPage2);
  128.  
  129. }
  130. }
  131.  
  132. void displayPage(int rowStart, int rowEnd) {
  133. time_t utc, local;
  134. utc = now();
  135.  
  136. local = myTZ.toLocal(utc, &tcr);
  137.  
  138. // calculate which day and week of the year it is, according to the current local time
  139. DayWeekNumber(year(local),month(local),day(local),weekday(local));
  140.  
  141. lcd.clear();
  142. lcd.setCursor(0,0);
  143. // for-loop which loops through each row
  144. for(int row=rowStart;row<rowEnd;row++) {
  145. // if row == 1, then we are on the second line, so move the cursor of the lcd
  146. if(not(row%2) == 0) lcd.setCursor(0,1);
  147. // for-loop which loops through each char in row
  148. for(int pos=0;pos<15;pos++) {
  149. displayDesiredFunction(row, pos, local);
  150. }
  151. }
  152. return;
  153. }
  154.  
  155. void displayDesiredFunction(int row, int pos, time_t l) {
  156. switch(homepage[row][pos]) {
  157. case 'h':
  158. displayHours(l); // display hours (use settings.hourFormat)
  159. break;
  160. case 'm':
  161. printI00(minute(l)); // display minutes
  162. break;
  163. case 's':
  164. printI00(second(l)); // display seconds
  165. break;
  166. case 'T':
  167. displayTemperature(); // display temperature (use settings.temperatureFormat)
  168. break;
  169. case 'd':
  170. displayWeekday(weekday(l)); // display day of week (use settings.mondayFirstDay and lanuague)
  171. break;
  172. case 'D':
  173. printI00(day(l)); // display day
  174. break;
  175. case 'M':
  176. printI00(month(l)); // display month
  177. break;
  178. case 'S':
  179. displayMonthShortStr(month(l)); // display month as string
  180. break;
  181. case 'Y':
  182. printI00(year(l)); // display year
  183. break;
  184. case 'n':
  185. displayNumber('d'); // display daynumber
  186. break;
  187. case 'w':
  188. displayNumber('w'); // display weeknumber
  189. break;
  190. case 'l':
  191. displayLocation(); // display current location
  192. case ':':
  193. displayDelimiter(':'); // display ':'
  194. break;
  195. case '-':
  196. displayDelimiter('-'); // display '-'
  197. break;
  198. case '/':
  199. displayDelimiter('/'); // display '/'
  200. break;
  201. case '.':
  202. displayDelimiter('.'); // display '.'
  203. break;
  204. case '|':
  205. displayDelimiter('|'); // display '|'
  206. break;
  207. case ' ':
  208. displayDelimiter(' '); // display ' '
  209. break;
  210. }
  211. }
  212.  
  213. void displayHours(time_t l) {
  214. (settings.hourFormat==24) ? printI00(hour(l)) : printI00(hourFormat12(l));
  215. return;
  216. }
  217.  
  218. void displayTemperature() {
  219. int tem = RTC.temperature();
  220. if(settings.labels) {
  221. lcd << translations[language_id][0] << ": ";
  222. }
  223. (settings.degreesFormat == 'c') ? lcd <<int(tem / 4.0) << (char)223 << "C " : lcd << int(tem / 4.0 * 9.0 / 5.0 + 32.0) << (char)223 << "F ";
  224. return;
  225. }
  226.  
  227. void displayWeekday(int val)
  228. {
  229. lcd << days[language_id][val-1];
  230. return;
  231. }
  232.  
  233. void displayNumber(char val) {
  234. if(val == 'd') {
  235. if(settings.labels == true) {
  236. lcd << translations[language_id][1] << ": ";
  237. }
  238. printI00(DW[0]);
  239. }
  240. else {
  241. if(settings.labels == true) {
  242. lcd << translations[language_id][2] << ": ";
  243. }
  244. printI00(DW[1]);
  245. }
  246. return;
  247. }
  248.  
  249. void displayMonthShortStr(int val) {
  250. lcd << months[language_id][val];
  251. return;
  252. }
  253.  
  254. void displayLocation() {
  255. //lcd << settings.location;
  256. }
  257.  
  258. void defineLanguageId() {
  259. switch(settings.language) {
  260. case EN:
  261. language_id = 0;
  262. break;
  263. case NL:
  264. language_id = 1;
  265. }
  266. }
  267.  
  268. void printI00(int val)
  269. {
  270. if (val < 10) lcd << '0';
  271. lcd << _DEC(val);
  272. return;
  273. }
  274.  
  275. void displayDelimiter(char delim) {
  276. lcd << delim;
  277. return;
  278. }
  279.  
  280. void setTimeRTC(unsigned int hours, unsigned int minutes, unsigned int seconds, unsigned int d, unsigned int m, unsigned int y) {
  281. setTime(hours, minutes, seconds, d, m, y);
  282. RTC.set(now());
  283. return;
  284. }
  285.  
  286. short DW[2];
  287.  
  288. void DayWeekNumber(unsigned int y, unsigned int m, unsigned int d, unsigned int w){
  289. int ndays[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; // Number of days at the beginning of the month in a not leap year.
  290. //Start to calculate the number of day
  291. if (m == 1 || m == 2){
  292. DW[0] = ndays[(m-1)]+d; //for any type of year, it calculate the number of days for January or february
  293. } // Now, try to calculate for the other months
  294. else if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0){ //those are the conditions to have a leap year
  295. DW[0] = ndays[(m-1)]+d+1; // if leap year, calculate in the same way but increasing one day
  296. }
  297. else { //if not a leap year, calculate in the normal way, such as January or February
  298. DW[0] = ndays[(m-1)]+d;
  299. }
  300. // Now start to calculate Week number
  301. (w==0) ? DW[1] = (DW[0]-7+10)/7 : DW[1] = (DW[0]-w+10)/7;
  302. return;
  303. }
Add Comment
Please, Sign In to add comment