Components and supplies
Arduino Nano 33 IoT
DFRobot Transparent Display
Tools and machines
Drill / Driver, Cordless
Project description
Code
Nice Display Clock
arduino
1// Sketch clock with Arduino and DF Robot Transparent Display 2// Carlo Stramaglia 3// 26 March 2022 4// Youtube channel https://www.youtube.com/c/CarloStramaglia 5 6 7#include <Arduino.h> 8#include <U8g2lib.h> 9#include <WiFiNINA.h> 10#include <UnixTime.h> 11#include "arduino_secrets.h" 12 13#ifdef U8X8_HAVE_HW_SPI 14#include <SPI.h> 15#endif 16#ifdef U8X8_HAVE_HW_I2C 17#include <Wire.h> 18#endif 19 20UnixTime tm (1); 21 22int Year; 23int Month; 24int Day; 25int Hour; 26int Minute; 27int Second; 28int Status=0; 29long SecondsToEnd; 30 31 32U8G2_SSD1309_128X64_NONAME0_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 5, /* dc=*/ 7, /* reset=*/ 6); 33 34///////please enter your sensitive data in the Secret tab/arduino_secrets.h 35char ssid[] = SECRET_SSID; // your network SSID (name) 36char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) 37int keyIndex = 0; // your network key index number (needed only for WEP) 38 39int status = WL_IDLE_STATUS; 40 41void printWifiStatus() 42{ 43 // print the SSID of the network you're attached to: 44 Serial.print("SSID: "); 45 Serial.println(WiFi.SSID()); 46 47 // print your board's IP address: 48 IPAddress ip = WiFi.localIP(); 49 Serial.print("IP Address: "); 50 Serial.println(ip); 51 52 // print the received signal strength: 53 long rssi = WiFi.RSSI(); 54 Serial.print("Signal strength (RSSI):"); 55 Serial.print(rssi); 56 Serial.println(" dBm"); 57} 58 59 60 61void setup() { 62 Serial.begin(115200); 63 u8g2.begin(); 64 WiFi.begin(SECRET_SSID, SECRET_PASS); 65 delay(1000); 66 67 // attempt to connect to Wifi network: 68 while (status != WL_CONNECTED) 69 { 70 // Connect to WPA/WPA2 network. Change this line if using open or WEP network: 71 status = WiFi.status(); 72 delay(1000); 73 } 74 75 printWifiStatus(); 76 77} 78 79void loop() { 80 81 int i; 82 83 for (i=0; i< 20; i++) 84 display(); 85 for (i=0; i< 20; i++) 86 secondsToYear(); 87 for (i=0; i< 20; i++) 88 secondsToMonth(); 89 for (i=0; i< 20; i++) 90 secondsToDay(); 91} 92 93//Display second to end of day 94void secondsToDay() 95{ 96 char buf[40]; 97 tm.getDateTime (WiFi.getTime()); 98 Day = tm.day; 99 Month = tm.month; 100 Year = tm.year; 101 Hour = tm.hour; 102 Minute = tm.minute; 103 Second = tm.second; 104 105 SecondsToEnd = (long)(23-Hour)*60*60; 106 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 107 SecondsToEnd = SecondsToEnd + (long)60-Second; 108 109 110 Serial.print ("Time: "); 111 Serial.print (Hour); 112 Serial.print (Minute); 113 Serial.println (Second); 114 Serial.print ("Seconds to end of day: "); 115 sprintf(buf, "%.8ld", SecondsToEnd); 116 Serial.println (buf); 117 118 u8g2.clearBuffer(); 119 u8g2.setFont(u8g2_font_ncenB18_tr); 120 121 u8g2.drawStr(5,50, buf); 122 sprintf(buf, "Seconds To Day End"); 123 u8g2.setFont(u8g2_font_ncenB08_tr); 124 u8g2.drawStr(1,10, buf); 125 u8g2.sendBuffer(); 126 127 delay(1000); 128} 129 130// Display Seconds to end of the month 131void secondsToMonth() 132{ 133 char buf[40]; 134 tm.getDateTime (WiFi.getTime()); 135 Day = tm.day; 136 Month = tm.month; 137 Year = tm.year; 138 Hour = tm.hour; 139 Minute = tm.minute; 140 Second = tm.second; 141 142 if (Month == 1 or Month == 3 or Month == 5 or Month == 7 or Month == 8 or Month == 10 or Month == 12) 143 { 144 Serial.print ("Days to end of month: "); 145 Serial.println (31-Day); 146 if (Day != 31) 147 { 148 SecondsToEnd = (long)(31-Day)*24*60*60; 149 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 150 } 151 else 152 SecondsToEnd = (long)(23-Hour)*60*60; 153 154 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 155 SecondsToEnd = SecondsToEnd + (long)60-Second; 156 157 } 158 159 else if (Month == 2) 160 { 161 Serial.print ("Days to end of month: "); 162 Serial.println (27-Day); 163 if (Day != 28) 164 { 165 SecondsToEnd = (long)(27-Day)*24*60*60; 166 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 167 } 168 else 169 SecondsToEnd = (long)(23-Hour)*60*60; 170 171 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 172 SecondsToEnd = SecondsToEnd + (long)60-Second; 173 174 } 175 else if (Month == 4 or Month == 6 or Month == 9 or Month == 11) 176 { 177 Serial.print ("Days to end of month: "); 178 Serial.println (29-Day); 179 if (Day !=30) 180 { 181 SecondsToEnd = (long)(29-Day)*24*60*60; 182 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 183 } 184 else 185 SecondsToEnd = (long)(23-Hour)*60*60; 186 187 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 188 SecondsToEnd = SecondsToEnd + (long)60-Second; 189 190 } 191 192 Serial.print ("Time: "); 193 Serial.print (Hour); 194 Serial.print (Minute); 195 Serial.println (Second); 196 Serial.print ("Seconds to end of month: "); 197 sprintf(buf, "%.8ld", SecondsToEnd); 198 Serial.println (buf); 199 200 u8g2.clearBuffer(); 201 u8g2.setFont(u8g2_font_ncenB18_tr); 202 203 u8g2.drawStr(5,50, buf); 204 sprintf(buf, "Seconds To Month End"); 205 u8g2.setFont(u8g2_font_ncenB08_tr); 206 u8g2.drawStr(0,10, buf); 207 u8g2.sendBuffer(); 208 delay(1000); 209} 210 211// Display Seconds to end of the year 212void secondsToYear() 213{ 214 char buf[40]; 215 tm.getDateTime (WiFi.getTime()); 216 Day = tm.day; 217 Month = tm.month; 218 Year = tm.year; 219 Hour = tm.hour; 220 Minute = tm.minute; 221 Second = tm.second; 222 223 if (Month == 1) 224 { 225 Serial.print ("Days to end of month: "); 226 Serial.println (31-Day); 227 if (Day != 31) 228 { 229 SecondsToEnd = (long)(31-Day)*24*60*60; //da aggiungere 230 SecondsToEnd = SecondsToEnd + (long)28*24*60*60; 231 } 232 else 233 SecondsToEnd = (long)28*24*60*60; 234 235 SecondsToEnd = SecondsToEnd + (long)6*31*24*60*60; 236 SecondsToEnd = SecondsToEnd + (long)4*30*24*60*60; 237 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 238 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 239 SecondsToEnd = SecondsToEnd + (long)60-Second; 240 } 241 242 else if (Month == 2) 243 { 244 Serial.print ("Days to end of month: "); 245 Serial.println (27-Day); 246 if (Day != 28) 247 { 248 SecondsToEnd = (long)(27-Day)*24*60*60; 249 SecondsToEnd = SecondsToEnd + (long)6*31*24*60*60; 250 } 251 else 252 SecondsToEnd = (long)6*31*24*60*60; 253 254 SecondsToEnd = SecondsToEnd + (long)4*30*24*60*60; 255 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 256 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 257 SecondsToEnd = SecondsToEnd + (long)60-Second; 258 259 } 260 else if (Month == 3) 261 { 262 Serial.print ("Days to end of month: "); 263 Serial.println (30-Day); 264 if (Day != 31) 265 { 266 SecondsToEnd = (long)(30-Day)*24*60*60; 267 SecondsToEnd = SecondsToEnd + (long)5*31*24*60*60; 268 } 269 else 270 SecondsToEnd = (long)5*31*24*60*60; 271 272 SecondsToEnd = SecondsToEnd + (long)4*30*24*60*60; 273 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 274 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 275 SecondsToEnd = SecondsToEnd + (long)60-Second; 276 277 } 278 else if (Month == 4) 279 { 280 Serial.print ("Days to end of month: "); 281 Serial.println (29-Day); 282 if (Day != 30) 283 { 284 SecondsToEnd = (long)(24-Day)*24*60*60; 285 SecondsToEnd = SecondsToEnd + (long)5*31*24*60*60; 286 } 287 else 288 SecondsToEnd = (long)5*31*24*60*60; 289 290 SecondsToEnd = SecondsToEnd + (long)3*30*24*60*60; 291 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 292 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 293 SecondsToEnd = SecondsToEnd + (long)60-Second; 294 295 } 296 else if (Month == 5) 297 { 298 Serial.print ("Days to end of month: "); 299 Serial.println (30-Day); 300 if (Day != 31) 301 { 302 SecondsToEnd = (long)(30-Day)*24*60*60; 303 SecondsToEnd = SecondsToEnd + (long)4*31*24*60*60; 304 } 305 else 306 SecondsToEnd = (long)4*31*24*60*60; 307 308 SecondsToEnd = SecondsToEnd + (long)3*30*24*60*60; 309 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 310 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 311 SecondsToEnd = SecondsToEnd + (long)60-Second; 312 } 313 else if (Month == 6) 314 { 315 Serial.print ("Days to end of month: "); 316 Serial.println (29-Day); 317 if (Day !=30) 318 { 319 SecondsToEnd = (long)(29-Day)*24*60*60; 320 SecondsToEnd = SecondsToEnd + (long)4*31*24*60*60; 321 } 322 else 323 SecondsToEnd = (long)4*31*24*60*60; 324 325 SecondsToEnd = SecondsToEnd + (long)2*30*24*60*60; 326 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 327 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 328 SecondsToEnd = SecondsToEnd + (long)60-Second; 329 } 330 else if (Month == 7) 331 { 332 Serial.print ("Days to end of month: "); 333 Serial.println (30-Day); 334 if (Day != 31) 335 { 336 SecondsToEnd = (long)(30-Day)*24*60*60; 337 SecondsToEnd = SecondsToEnd + (long)3*31*24*60*60; 338 } 339 else 340 SecondsToEnd = (long)3*31*24*60*60; 341 342 SecondsToEnd = SecondsToEnd + (long)2*30*24*60*60; 343 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 344 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 345 SecondsToEnd = SecondsToEnd + (long)60-Second; 346 } 347 else if (Month == 8) 348 { 349 Serial.print ("Days to end of month: "); 350 Serial.println (30-Day); 351 if (Day != 31) 352 { 353 SecondsToEnd = (long)(30-Day)*24*60*60; 354 SecondsToEnd = SecondsToEnd + (long)2*31*24*60*60; 355 } 356 else 357 SecondsToEnd = (long)2*31*24*60*60; 358 359 SecondsToEnd = SecondsToEnd + (long)2*30*24*60*60; 360 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 361 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 362 SecondsToEnd = SecondsToEnd + (long)60-Second; 363 } 364 else if (Month == 9) 365 { 366 Serial.print ("Days to end of month: "); 367 Serial.println (29-Day); 368 if (Day != 30) 369 { 370 SecondsToEnd = (long)(29-Day)*24*60*60; 371 SecondsToEnd = SecondsToEnd + (long)2*31*24*60*60; 372 } 373 else 374 SecondsToEnd = (long)2*31*24*60*60; 375 376 SecondsToEnd = SecondsToEnd + (long)1*30*24*60*60; 377 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 378 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 379 SecondsToEnd = SecondsToEnd + (long)60-Second; 380 381 } 382 else if (Month == 10) 383 { 384 Serial.print ("Days to end of month: "); 385 Serial.println (30-Day); 386 if (Day != 31) 387 { 388 SecondsToEnd = (long)(30-Day)*24*60*60; 389 SecondsToEnd = SecondsToEnd + (long)1*31*24*60*60; 390 } 391 else 392 SecondsToEnd = (long)1*31*24*60*60; 393 394 SecondsToEnd = SecondsToEnd + (long)1*30*24*60*60; 395 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 396 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 397 SecondsToEnd = SecondsToEnd + (long)60-Second; 398 399 } 400 else if (Month == 11) 401 { 402 Serial.print ("Days to end of month: "); 403 Serial.println (29-Day); 404 if (Day != 30) 405 { 406 SecondsToEnd = (long)(29-Day)*24*60*60; 407 SecondsToEnd = SecondsToEnd + (long)1*31*24*60*60; 408 } 409 else 410 SecondsToEnd = (long)1*31*24*60*60; 411 412 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 413 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 414 SecondsToEnd = SecondsToEnd + (long)60-Second; 415 416 } 417 else if (Month == 12) 418 { 419 Serial.print ("Days to end of month: "); 420 Serial.println (30-Day); 421 if (Day != 31) 422 { 423 SecondsToEnd = (long)(30-Day)*24*60*60; 424 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 425 } 426 else 427 SecondsToEnd = (long)(23-Hour)*60*60; 428 429 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 430 SecondsToEnd = SecondsToEnd + (long)60-Second; 431 432 } 433 434 Serial.print ("Months to end of year: "); 435 Serial.println (12-Month); 436 Serial.print ("Year now: "); 437 Serial.println (tm.year); 438 Serial.print ("Time: "); 439 Serial.print (Hour); 440 Serial.print (Minute); 441 Serial.println (Second); 442 Serial.print ("Seconds to end of year: "); 443 sprintf(buf, "%.8ld", SecondsToEnd); 444 Serial.println (buf); 445 446 u8g2.clearBuffer(); 447 u8g2.setFont(u8g2_font_ncenB18_tr); 448 449 u8g2.drawStr(5,50, buf); 450 sprintf(buf, "Seconds To Year End"); 451 u8g2.setFont(u8g2_font_ncenB08_tr); 452 u8g2.drawStr(0,10, buf); 453 u8g2.sendBuffer(); 454 455 delay(1000); 456} 457 458 459// Display Ora a data 460void display() 461{ 462 char buf[40]; 463 char monthWord[40]; 464 tm.getDateTime (WiFi.getTime()); 465 466 u8g2.clearBuffer(); 467 u8g2.setFont(u8g2_font_ncenB08_tr); 468 469 switch (tm.month) 470 { 471 case 1: 472 sprintf(monthWord,"January"); 473 break; 474 case 2: 475 sprintf(monthWord,"February"); 476 break; 477 case 3: 478 sprintf(monthWord,"March"); 479 break; 480 case 4: 481 sprintf(monthWord,"April"); 482 break; 483 case 5: 484 sprintf(monthWord,"May"); 485 break; 486 case 6: 487 sprintf(monthWord,"June"); 488 break; 489 case 7: 490 sprintf(monthWord,"July"); 491 break; 492 case 8: 493 sprintf(monthWord,"August"); 494 break; 495 case 9: 496 sprintf(monthWord,"September"); 497 break; 498 case 10: 499 sprintf(monthWord,"October"); 500 break; 501 case 11: 502 sprintf(monthWord,"November"); 503 break; 504 case 12: 505 sprintf(monthWord,"December"); 506 break; 507 default: 508 sprintf(monthWord,"Unknown"); 509 break; 510 } 511 512 sprintf(buf, "%.2d %s %d", tm.day, monthWord, tm.year); 513 Serial.print ("Date: "); 514 Serial.println(buf); 515 u8g2.drawStr(20,10, buf); 516 517 switch (tm.dayOfWeek) 518 { 519 case 1: 520 sprintf(buf,"Monday"); 521 break; 522 case 2: 523 sprintf(buf,"Tuesday"); 524 break; 525 case 3: 526 sprintf(buf,"Wednesday"); 527 break; 528 case 4: 529 sprintf(buf,"Thursday"); 530 break; 531 case 5: 532 sprintf(buf,"Friday"); 533 break; 534 case 6: 535 sprintf(buf,"Saturday"); 536 break; 537 case 7: 538 sprintf(buf,"Sunday"); 539 break; 540 default: 541 sprintf(buf,"Unknown"); 542 break; 543 } 544 Serial.print ("Day of Week: "); 545 Serial.println(buf); 546 u8g2.drawStr(35,30, buf); 547 548 sprintf(buf, "%.2d:%.2d:%.2d", tm.hour, tm.minute, tm.second); 549 Serial.print ("Time: "); 550 Serial.println(buf); 551 u8g2.drawStr(35,50, buf); 552 553 u8g2.sendBuffer(); 554 555 delay(1000); 556 557} 558
Nice Display Clock
arduino
1// Sketch clock with Arduino and DF Robot Transparent Display 2// Carlo 3 Stramaglia 4// 26 March 2022 5// Youtube channel https://www.youtube.com/c/CarloStramaglia 6 7 8#include 9 <Arduino.h> 10#include <U8g2lib.h> 11#include <WiFiNINA.h> 12#include <UnixTime.h> 13#include 14 "arduino_secrets.h" 15 16#ifdef U8X8_HAVE_HW_SPI 17#include <SPI.h> 18#endif 19#ifdef 20 U8X8_HAVE_HW_I2C 21#include <Wire.h> 22#endif 23 24UnixTime tm (1); 25 26int 27 Year; 28int Month; 29int Day; 30int Hour; 31int Minute; 32int Second; 33int 34 Status=0; 35long SecondsToEnd; 36 37 38U8G2_SSD1309_128X64_NONAME0_F_4W_SW_SPI 39 u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 5, /* dc=*/ 7, /* reset=*/ 40 6); 41 42///////please enter your sensitive data in the Secret tab/arduino_secrets.h 43char 44 ssid[] = SECRET_SSID; // your network SSID (name) 45char pass[] = SECRET_PASS; 46 // your network password (use for WPA, or use as key for WEP) 47int keyIndex 48 = 0; // your network key index number (needed only for WEP) 49 50int 51 status = WL_IDLE_STATUS; 52 53void printWifiStatus() 54{ 55 // print the SSID 56 of the network you're attached to: 57 Serial.print("SSID: "); 58 Serial.println(WiFi.SSID()); 59 60 61 // print your board's IP address: 62 IPAddress ip = WiFi.localIP(); 63 Serial.print("IP 64 Address: "); 65 Serial.println(ip); 66 67 // print the received signal strength: 68 69 long rssi = WiFi.RSSI(); 70 Serial.print("Signal strength (RSSI):"); 71 Serial.print(rssi); 72 73 Serial.println(" dBm"); 74} 75 76 77 78void setup() { 79 Serial.begin(115200); 80 81 u8g2.begin(); 82 WiFi.begin(SECRET_SSID, SECRET_PASS); 83 delay(1000); 84 85 86 // attempt to connect to Wifi network: 87 while (status != WL_CONNECTED) 88 89 { 90 // Connect to WPA/WPA2 network. Change this line if using open or 91 WEP network: 92 status = WiFi.status(); 93 delay(1000); 94 } 95 96 printWifiStatus(); 97 98} 99 100void 101 loop() { 102 103 int i; 104 105 for (i=0; i< 20; i++) 106 display(); 107 108 for (i=0; i< 20; i++) 109 secondsToYear(); 110 for (i=0; i< 20; i++) 111 secondsToMonth(); 112 113 for (i=0; i< 20; i++) 114 secondsToDay(); 115} 116 117//Display second to end 118 of day 119void secondsToDay() 120{ 121 char buf[40]; 122 tm.getDateTime (WiFi.getTime()); 123 124 Day = tm.day; 125 Month = tm.month; 126 Year = tm.year; 127 Hour = tm.hour; 128 129 Minute = tm.minute; 130 Second = tm.second; 131 132 SecondsToEnd = (long)(23-Hour)*60*60; 133 134 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 135 SecondsToEnd = SecondsToEnd 136 + (long)60-Second; 137 138 139 Serial.print ("Time: "); 140 Serial.print 141 (Hour); 142 Serial.print (Minute); 143 Serial.println (Second); 144 Serial.print 145 ("Seconds to end of day: "); 146 sprintf(buf, "%.8ld", SecondsToEnd); 147 Serial.println 148 (buf); 149 150 u8g2.clearBuffer(); 151 u8g2.setFont(u8g2_font_ncenB18_tr); 152 153 154 u8g2.drawStr(5,50, buf); 155 sprintf(buf, "Seconds To Day End"); 156 u8g2.setFont(u8g2_font_ncenB08_tr); 157 158 u8g2.drawStr(1,10, buf); 159 u8g2.sendBuffer(); 160 161 delay(1000); 162} 163 164// 165 Display Seconds to end of the month 166void secondsToMonth() 167{ 168 char buf[40]; 169 170 tm.getDateTime (WiFi.getTime()); 171 Day = tm.day; 172 Month = tm.month; 173 174 Year = tm.year; 175 Hour = tm.hour; 176 Minute = tm.minute; 177 Second = tm.second; 178 179 180 if (Month == 1 or Month == 3 or Month == 5 or Month == 7 or Month == 8 or 181 Month == 10 or Month == 12) 182 { 183 Serial.print ("Days to end of month: 184 "); 185 Serial.println (31-Day); 186 if (Day != 31) 187 { 188 SecondsToEnd 189 = (long)(31-Day)*24*60*60; 190 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 191 192 } 193 else 194 SecondsToEnd = (long)(23-Hour)*60*60; 195 196 197 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 198 SecondsToEnd = SecondsToEnd 199 + (long)60-Second; 200 201 } 202 203 else if (Month == 2) 204 { 205 206 Serial.print ("Days to end of month: "); 207 Serial.println (27-Day); 208 209 if (Day != 28) 210 { 211 SecondsToEnd = (long)(27-Day)*24*60*60; 212 213 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 214 } 215 else 216 217 SecondsToEnd = (long)(23-Hour)*60*60; 218 219 SecondsToEnd = SecondsToEnd 220 + (long)(59-Minute)*60; 221 SecondsToEnd = SecondsToEnd + (long)60-Second; 222 223 224 } 225 else if (Month == 4 or Month == 6 or Month == 9 or Month == 11) 226 227 { 228 Serial.print ("Days to end of month: "); 229 Serial.println (29-Day); 230 231 if (Day !=30) 232 { 233 SecondsToEnd = (long)(29-Day)*24*60*60; 234 235 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 236 } 237 else 238 239 SecondsToEnd = (long)(23-Hour)*60*60; 240 241 SecondsToEnd = SecondsToEnd 242 + (long)(59-Minute)*60; 243 SecondsToEnd = SecondsToEnd + (long)60-Second; 244 245 246 } 247 248 Serial.print ("Time: "); 249 Serial.print (Hour); 250 251 Serial.print (Minute); 252 Serial.println (Second); 253 Serial.print ("Seconds 254 to end of month: "); 255 sprintf(buf, "%.8ld", SecondsToEnd); 256 Serial.println 257 (buf); 258 259 u8g2.clearBuffer(); 260 u8g2.setFont(u8g2_font_ncenB18_tr); 261 262 263 u8g2.drawStr(5,50, buf); 264 sprintf(buf, "Seconds To Month End"); 265 u8g2.setFont(u8g2_font_ncenB08_tr); 266 267 u8g2.drawStr(0,10, buf); 268 u8g2.sendBuffer(); 269 delay(1000); 270} 271 272// 273 Display Seconds to end of the year 274void secondsToYear() 275{ 276 char buf[40]; 277 278 tm.getDateTime (WiFi.getTime()); 279 Day = tm.day; 280 Month = tm.month; 281 282 Year = tm.year; 283 Hour = tm.hour; 284 Minute = tm.minute; 285 Second = tm.second; 286 287 288 if (Month == 1) 289 { 290 Serial.print ("Days to end of month: "); 291 292 Serial.println (31-Day); 293 if (Day != 31) 294 { 295 SecondsToEnd 296 = (long)(31-Day)*24*60*60; //da aggiungere 297 SecondsToEnd = SecondsToEnd 298 + (long)28*24*60*60; 299 } 300 else 301 SecondsToEnd = (long)28*24*60*60; 302 303 304 SecondsToEnd = SecondsToEnd + (long)6*31*24*60*60; 305 SecondsToEnd 306 = SecondsToEnd + (long)4*30*24*60*60; 307 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 308 309 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 310 SecondsToEnd = SecondsToEnd 311 + (long)60-Second; 312 } 313 314 else if (Month == 2) 315 { 316 Serial.print 317 ("Days to end of month: "); 318 Serial.println (27-Day); 319 if (Day != 320 28) 321 { 322 SecondsToEnd = (long)(27-Day)*24*60*60; 323 SecondsToEnd 324 = SecondsToEnd + (long)6*31*24*60*60; 325 } 326 else 327 SecondsToEnd 328 = (long)6*31*24*60*60; 329 330 SecondsToEnd = SecondsToEnd + (long)4*30*24*60*60; 331 332 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 333 SecondsToEnd = SecondsToEnd 334 + (long)(59-Minute)*60; 335 SecondsToEnd = SecondsToEnd + (long)60-Second; 336 337 338 } 339 else if (Month == 3) 340 { 341 Serial.print ("Days to end 342 of month: "); 343 Serial.println (30-Day); 344 if (Day != 31) 345 { 346 347 SecondsToEnd = (long)(30-Day)*24*60*60; 348 SecondsToEnd = SecondsToEnd 349 + (long)5*31*24*60*60; 350 } 351 else 352 SecondsToEnd = (long)5*31*24*60*60; 353 354 355 SecondsToEnd = SecondsToEnd + (long)4*30*24*60*60; 356 SecondsToEnd 357 = SecondsToEnd + (long)(23-Hour)*60*60; 358 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 359 360 SecondsToEnd = SecondsToEnd + (long)60-Second; 361 362 } 363 else if (Month 364 == 4) 365 { 366 Serial.print ("Days to end of month: "); 367 Serial.println 368 (29-Day); 369 if (Day != 30) 370 { 371 SecondsToEnd = (long)(24-Day)*24*60*60; 372 373 SecondsToEnd = SecondsToEnd + (long)5*31*24*60*60; 374 } 375 else 376 377 SecondsToEnd = (long)5*31*24*60*60; 378 379 SecondsToEnd = SecondsToEnd 380 + (long)3*30*24*60*60; 381 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 382 383 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 384 SecondsToEnd = SecondsToEnd 385 + (long)60-Second; 386 387 } 388 else if (Month == 5) 389 { 390 Serial.print 391 ("Days to end of month: "); 392 Serial.println (30-Day); 393 if (Day != 394 31) 395 { 396 SecondsToEnd = (long)(30-Day)*24*60*60; 397 SecondsToEnd 398 = SecondsToEnd + (long)4*31*24*60*60; 399 } 400 else 401 SecondsToEnd 402 = (long)4*31*24*60*60; 403 404 SecondsToEnd = SecondsToEnd + (long)3*30*24*60*60; 405 406 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 407 SecondsToEnd = SecondsToEnd 408 + (long)(59-Minute)*60; 409 SecondsToEnd = SecondsToEnd + (long)60-Second; 410 411 } 412 else if (Month == 6) 413 { 414 Serial.print ("Days to end of month: 415 "); 416 Serial.println (29-Day); 417 if (Day !=30) 418 { 419 SecondsToEnd 420 = (long)(29-Day)*24*60*60; 421 SecondsToEnd = SecondsToEnd + (long)4*31*24*60*60; 422 423 } 424 else 425 SecondsToEnd = (long)4*31*24*60*60; 426 427 SecondsToEnd 428 = SecondsToEnd + (long)2*30*24*60*60; 429 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 430 431 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 432 SecondsToEnd = SecondsToEnd 433 + (long)60-Second; 434 } 435 else if (Month == 7) 436 { 437 Serial.print ("Days 438 to end of month: "); 439 Serial.println (30-Day); 440 if (Day != 31) 441 442 { 443 SecondsToEnd = (long)(30-Day)*24*60*60; 444 SecondsToEnd = SecondsToEnd 445 + (long)3*31*24*60*60; 446 } 447 else 448 SecondsToEnd = (long)3*31*24*60*60; 449 450 451 SecondsToEnd = SecondsToEnd + (long)2*30*24*60*60; 452 SecondsToEnd 453 = SecondsToEnd + (long)(23-Hour)*60*60; 454 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 455 456 SecondsToEnd = SecondsToEnd + (long)60-Second; 457 } 458 else if (Month == 459 8) 460 { 461 Serial.print ("Days to end of month: "); 462 Serial.println 463 (30-Day); 464 if (Day != 31) 465 { 466 SecondsToEnd = (long)(30-Day)*24*60*60; 467 468 SecondsToEnd = SecondsToEnd + (long)2*31*24*60*60; 469 } 470 else 471 472 SecondsToEnd = (long)2*31*24*60*60; 473 474 SecondsToEnd = SecondsToEnd 475 + (long)2*30*24*60*60; 476 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 477 478 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 479 SecondsToEnd = SecondsToEnd 480 + (long)60-Second; 481 } 482 else if (Month == 9) 483 { 484 Serial.print ("Days 485 to end of month: "); 486 Serial.println (29-Day); 487 if (Day != 30) 488 489 { 490 SecondsToEnd = (long)(29-Day)*24*60*60; 491 SecondsToEnd = SecondsToEnd 492 + (long)2*31*24*60*60; 493 } 494 else 495 SecondsToEnd = (long)2*31*24*60*60; 496 497 498 SecondsToEnd = SecondsToEnd + (long)1*30*24*60*60; 499 SecondsToEnd 500 = SecondsToEnd + (long)(23-Hour)*60*60; 501 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 502 503 SecondsToEnd = SecondsToEnd + (long)60-Second; 504 505 } 506 else if (Month 507 == 10) 508 { 509 Serial.print ("Days to end of month: "); 510 Serial.println 511 (30-Day); 512 if (Day != 31) 513 { 514 SecondsToEnd = (long)(30-Day)*24*60*60; 515 516 SecondsToEnd = SecondsToEnd + (long)1*31*24*60*60; 517 } 518 else 519 520 SecondsToEnd = (long)1*31*24*60*60; 521 522 SecondsToEnd = SecondsToEnd 523 + (long)1*30*24*60*60; 524 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 525 526 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 527 SecondsToEnd = SecondsToEnd 528 + (long)60-Second; 529 530 } 531 else if (Month == 11) 532 { 533 Serial.print 534 ("Days to end of month: "); 535 Serial.println (29-Day); 536 if (Day != 537 30) 538 { 539 SecondsToEnd = (long)(29-Day)*24*60*60; 540 SecondsToEnd 541 = SecondsToEnd + (long)1*31*24*60*60; 542 } 543 else 544 SecondsToEnd 545 = (long)1*31*24*60*60; 546 547 SecondsToEnd = SecondsToEnd + (long)(23-Hour)*60*60; 548 549 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 550 SecondsToEnd = SecondsToEnd 551 + (long)60-Second; 552 553 } 554 else if (Month == 12) 555 { 556 Serial.print 557 ("Days to end of month: "); 558 Serial.println (30-Day); 559 if (Day != 560 31) 561 { 562 SecondsToEnd = (long)(30-Day)*24*60*60; 563 SecondsToEnd 564 = SecondsToEnd + (long)(23-Hour)*60*60; 565 } 566 else 567 SecondsToEnd 568 = (long)(23-Hour)*60*60; 569 570 SecondsToEnd = SecondsToEnd + (long)(59-Minute)*60; 571 572 SecondsToEnd = SecondsToEnd + (long)60-Second; 573 574 } 575 576 Serial.print 577 ("Months to end of year: "); 578 Serial.println (12-Month); 579 Serial.print 580 ("Year now: "); 581 Serial.println (tm.year); 582 Serial.print ("Time: "); 583 584 Serial.print (Hour); 585 Serial.print (Minute); 586 Serial.println (Second); 587 588 Serial.print ("Seconds to end of year: "); 589 sprintf(buf, "%.8ld", SecondsToEnd); 590 591 Serial.println (buf); 592 593 u8g2.clearBuffer(); 594 u8g2.setFont(u8g2_font_ncenB18_tr); 595 596 597 u8g2.drawStr(5,50, buf); 598 sprintf(buf, "Seconds To Year End"); 599 u8g2.setFont(u8g2_font_ncenB08_tr); 600 601 u8g2.drawStr(0,10, buf); 602 u8g2.sendBuffer(); 603 604 delay(1000); 605} 606 607 608// 609 Display Ora a data 610void display() 611{ 612 char buf[40]; 613 char monthWord[40]; 614 615 tm.getDateTime (WiFi.getTime()); 616 617 u8g2.clearBuffer(); 618 u8g2.setFont(u8g2_font_ncenB08_tr); 619 620 621 switch (tm.month) 622 { 623 case 1: 624 sprintf(monthWord,"January"); 625 626 break; 627 case 2: 628 sprintf(monthWord,"February"); 629 break; 630 631 case 3: 632 sprintf(monthWord,"March"); 633 break; 634 case 4: 635 636 sprintf(monthWord,"April"); 637 break; 638 case 5: 639 sprintf(monthWord,"May"); 640 641 break; 642 case 6: 643 sprintf(monthWord,"June"); 644 break; 645 646 case 7: 647 sprintf(monthWord,"July"); 648 break; 649 case 8: 650 651 sprintf(monthWord,"August"); 652 break; 653 case 9: 654 sprintf(monthWord,"September"); 655 656 break; 657 case 10: 658 sprintf(monthWord,"October"); 659 break; 660 661 case 11: 662 sprintf(monthWord,"November"); 663 break; 664 case 12: 665 666 sprintf(monthWord,"December"); 667 break; 668 default: 669 sprintf(monthWord,"Unknown"); 670 671 break; 672 } 673 674 sprintf(buf, "%.2d %s %d", tm.day, monthWord, tm.year); 675 676 Serial.print ("Date: "); 677 Serial.println(buf); 678 u8g2.drawStr(20,10, 679 buf); 680 681 switch (tm.dayOfWeek) 682 { 683 case 1: 684 sprintf(buf,"Monday"); 685 686 break; 687 case 2: 688 sprintf(buf,"Tuesday"); 689 break; 690 case 691 3: 692 sprintf(buf,"Wednesday"); 693 break; 694 case 4: 695 sprintf(buf,"Thursday"); 696 697 break; 698 case 5: 699 sprintf(buf,"Friday"); 700 break; 701 case 702 6: 703 sprintf(buf,"Saturday"); 704 break; 705 case 7: 706 sprintf(buf,"Sunday"); 707 708 break; 709 default: 710 sprintf(buf,"Unknown"); 711 break; 712 } 713 714 Serial.print ("Day of Week: "); 715 Serial.println(buf); 716 u8g2.drawStr(35,30, 717 buf); 718 719 sprintf(buf, "%.2d:%.2d:%.2d", tm.hour, tm.minute, tm.second); 720 721 Serial.print ("Time: "); 722 Serial.println(buf); 723 u8g2.drawStr(35,50, 724 buf); 725 726 u8g2.sendBuffer(); 727 728 delay(1000); 729 730} 731
Downloadable files
Diagram
Diagram
Comments
Only logged in users can leave comments