Components and supplies
LCD Keypad Shield
DS3231M - ±5ppm, I2C Real-Time Clock
Solderless Breadboard Half Size
Arduino Nano R3
Project description
Code
DS3231_Set_Time
arduino
Use an LCD Keypad Shield to set up and DS3231 RTC
1/* 2DS3231_set_time.pde 3Guy Lowndes 42020-12-18 5 6Information taken from DS3231_set and DS3231_test by Eric Ayars 4/11 7 8Uses an LCD with keypad to set the time for a DS3231 RTC 9 10uses code for the buttons from https://forum.arduino.cc/index.php?topic=354260.0 by Mark Bramwell, July 2010 11 12Uses tips and tricks from https://www.baldengineer.com/arduino-lcd-display-tips.html 13 14*/ 15 16#include <DS3231.h> 17#include <Wire.h> 18#include <LiquidCrystal.h> 19 20DS3231 Clock; 21LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 22 23/*****************************************************************************************************************/ 24 25// These are the ALARM Bits that can be used 26// They need to be combined into a single value (see below) 27// Found here: https://github.com/mlepard/ArduinoChicken/blob/master/roboCoop/alarmControl.ino 28const byte ALRM1_MATCH_EVERY_SEC = 0b1111; // once a second 29const byte ALRM1_MATCH_SEC = 0b1110; // when seconds match 30const byte ALRM1_MATCH_MIN_SEC = 0b1100; // when minutes and seconds match 31const byte ALRM1_MATCH_HR_MIN_SEC = 0b1000; // when hours, minutes, and seconds match 32const byte ALRM1_MATCH_DY_HR_MIN_SEC = 0b0000; // when hours, minutes, and seconds match 33 34const byte ALRM2_ONCE_PER_MIN = 0b111; // once per minute (00 seconds of every minute) 35const byte ALRM2_MATCH_MIN = 0b110; // when minutes match 36const byte ALRM2_MATCH_HR_MIN = 0b100; // when hours and minutes match 37 38/*****************************************************************************************************************/ 39 40// define some values used by the menu controller 41const byte modeSHOWDATETIME = 0b0000; 42const byte modeSHOWALARM1 = 0b0001; 43const byte modeSHOWALARM2 = 0b0010; 44const byte modeSETDATE = 0b0011; 45const byte modeSETTIME = 0b0100; 46const byte modeSETALARM1ON = 0b0101; 47const byte modeSETALARM1 = 0b0110; 48const byte modeSETALARM1METHOD = 0b0111; 49const byte modeSETALARM2ON = 0b1000; 50const byte modeSETALARM2 = 0b1001; 51const byte modeSETALARM2METHOD = 0b1010; 52byte currentMode = modeSHOWDATETIME; 53 54// buffers for the display 55char line0[21]; 56char line1[21]; 57 58// define some values used by the panel and buttons 59byte lcd_key = 0b000; 60int adc_key_in = 0; 61const int btnRIGHT =0b000; 62const int btnUP =0b001; 63const int btnDOWN =0b010; 64const int btnLEFT =0b011; 65const int btnSELECT =0b100; 66const int btnNONE =0b101; 67int bounceDelay; 68byte oldKey = 0b000; 69 70//Variables to control blinking 71bool blinkNow=false; 72int blinkInt=1; 73int maxBlinkInt=3; 74uint32_t blinkStart=0; 75uint32_t blinkDelay=500; 76 77/*************************************************************** 78 * Set up and Loop 79 ***************************************************************/ 80 81void setup() { 82 83 // Set the bounce delay 84 bounceDelay=50; 85 86 // Start the lcd and set the cursor 87 lcd.begin(16,2); 88 lcd.setCursor(0,0); 89 90 // Start the I2C interface 91 Wire.begin(); 92 93} 94 95void loop() { 96 97 //Set the menu item or display text for the user 98 setMenu(); 99 100 // read the buttons 101 lcd_key = read_LCD_buttons(); 102 103 // check the blink counter isn't too high 104 if (blinkInt > maxBlinkInt){blinkInt=maxBlinkInt;} 105 106 // Set the current blink status 107 if (currentMode>modeSHOWALARM2 && ((millis()-blinkStart)>blinkDelay)){ 108 blinkNow=!blinkNow; 109 blinkStart=millis(); 110 } 111 else if (currentMode<=modeSHOWALARM2) 112 {blinkNow=false;} 113 114 // check the key press against the previous key press to avoid issues from long key presses 115 if (oldKey!=lcd_key) { 116 // depending on which button was pushed, we perform an action 117 switch (lcd_key) 118 { 119 case btnRIGHT: 120 { 121 oldKey = btnRIGHT; 122 if (blinkInt<maxBlinkInt) {blinkInt+=1;} 123 if (currentMode==modeSHOWDATETIME){currentMode=modeSHOWALARM1;} 124 else if (currentMode==modeSHOWALARM1){currentMode=modeSHOWALARM2;} 125 else if (currentMode==modeSHOWALARM2){currentMode=modeSHOWDATETIME;} 126 delay(50); 127 break; 128 } 129 case btnLEFT: 130 { 131 oldKey = btnLEFT; 132 if (blinkInt>1) {blinkInt-=1;} 133 delay(50); 134 break; 135 } 136 case btnUP: 137 { 138 oldKey = btnUP; 139 if (currentMode==modeSETDATE){ 140 if (blinkInt==1){increaseDate();} 141 if (blinkInt==2){increaseMonth();} 142 if (blinkInt==3){increaseYear();} 143 } 144 if (currentMode==modeSETTIME){ 145 if (blinkInt==1){increaseHour();} 146 if (blinkInt==2){increaseMinute();} 147 } 148 if (currentMode==modeSETALARM1ON) { 149 if (Clock.checkAlarmEnabled(1)) {AlarmOn(1, false);} 150 } 151 if (currentMode==modeSETALARM1) { 152 if (blinkInt==1){changeAlarmDayOption(1);} 153 if (blinkInt==2){ChangeAlarm(1, 1, 0, 0,0);} 154 if (blinkInt==3){ChangeAlarm(1, 0, 1, 0,0);} 155 if (blinkInt==4){ChangeAlarm(1, 0, 0, 1,0);} 156 if (blinkInt==5){ChangeAlarm(1, 0, 0, 0,1);} 157 } 158 if (currentMode==modeSETALARM1METHOD) { 159 changeAlarmMethod(1, 1); 160 } 161 if (currentMode==modeSETALARM2ON) { 162 if (Clock.checkAlarmEnabled(2)) {AlarmOn(2, false);} 163 } 164 if (currentMode==modeSETALARM2) { 165 if (blinkInt==1){changeAlarmDayOption(2);} 166 if (blinkInt==2){ChangeAlarm(2, 1, 0, 0,0);} 167 if (blinkInt==3){ChangeAlarm(2, 0, 1, 0,0);} 168 if (blinkInt==4){ChangeAlarm(2, 0, 0, 1,0);} 169 if (blinkInt==5){ChangeAlarm(2, 0, 0, 0,1);} 170 } 171 if (currentMode==modeSETALARM2METHOD) { 172 changeAlarmMethod(2, 1); 173 } 174 delay(50); 175 break; 176 } 177 case btnDOWN: 178 { 179 oldKey = btnDOWN; 180 if (currentMode==modeSETDATE){ 181 if (blinkInt==1){decreaseDate();} 182 if (blinkInt==2){decreaseMonth();} 183 if (blinkInt==3){decreaseYear();} 184 } 185 if (currentMode==modeSETTIME){ 186 if (blinkInt==1){decreaseHour();} 187 if (blinkInt==2){decreaseMinute();} 188 } 189 if (currentMode==modeSETALARM1ON) { 190 if (!Clock.checkAlarmEnabled(1)) {AlarmOn(1, true);} 191 } 192 if (currentMode==modeSETALARM1) { 193 if (blinkInt==1){changeAlarmDayOption(1);} 194 if (blinkInt==2){ChangeAlarm(1, -1, 0, 0,0);} 195 if (blinkInt==3){ChangeAlarm(1, 0, -1, 0,0);} 196 if (blinkInt==4){ChangeAlarm(1, 0, 0, -1,0);} 197 if (blinkInt==5){ChangeAlarm(1, 0, 0, 0,-1);} 198 } 199 if (currentMode==modeSETALARM1METHOD) { 200 changeAlarmMethod(1, 0); 201 } 202 if (currentMode==modeSETALARM2ON) { 203 if (!Clock.checkAlarmEnabled(2)) {AlarmOn(2, true);} 204 } 205 if (currentMode==modeSETALARM2) { 206 if (blinkInt==1){changeAlarmDayOption(2);} 207 if (blinkInt==2){ChangeAlarm(2, -1, 0, 0,0);} 208 if (blinkInt==3){ChangeAlarm(2, 0, -1, 0,0);} 209 if (blinkInt==4){ChangeAlarm(2, 0, 0, -1,0);} 210 if (blinkInt==5){ChangeAlarm(2, 0, 0, 0,-1);} 211 } 212 if (currentMode==modeSETALARM2METHOD) { 213 changeAlarmMethod(2, 0); 214 } 215 delay(50); 216 break; 217 } 218 case btnSELECT: 219 { 220 blinkInt=1; 221 oldKey = btnSELECT; 222 if (currentMode==modeSHOWDATETIME){currentMode=modeSETDATE;} 223 else if (currentMode==modeSHOWALARM1){currentMode=modeSETALARM1ON;} 224 else if (currentMode==modeSHOWALARM2){currentMode=modeSETALARM2ON;} 225 else if (currentMode==modeSETDATE){currentMode=modeSETTIME;} 226 else if (currentMode==modeSETTIME){currentMode=modeSHOWDATETIME;} 227 else if (currentMode==modeSETALARM1ON && Clock.checkAlarmEnabled(1)){currentMode=modeSETALARM1;} 228 else if (currentMode==modeSETALARM1ON && !Clock.checkAlarmEnabled(1)){currentMode=modeSHOWALARM1;} 229 else if (currentMode==modeSETALARM1){currentMode=modeSETALARM1METHOD;} 230 else if (currentMode==modeSETALARM1METHOD){currentMode=modeSHOWALARM1;} 231 else if (currentMode==modeSETALARM2ON && Clock.checkAlarmEnabled(2)){currentMode=modeSETALARM2;} 232 else if (currentMode==modeSETALARM2ON && !Clock.checkAlarmEnabled(2)){currentMode=modeSHOWALARM2;} 233 else if (currentMode==modeSETALARM2){currentMode=modeSETALARM2METHOD;} 234 else if (currentMode==modeSETALARM2METHOD){currentMode=modeSHOWALARM2;}; 235 break; 236 } 237 case btnNONE: 238 { 239 oldKey=btnNONE; 240 break; 241 } 242 } 243 } 244} 245 246/*************************************************************** 247 * Menu Control 248 ***************************************************************/ 249 250void setMenu(){ 251 252 switch (currentMode) 253 { 254 case modeSHOWDATETIME: 255 { 256 showDateTime(); 257 break; 258 } 259 case modeSHOWALARM1: 260 { 261 showAlarm1(); 262 break; 263 } 264 case modeSHOWALARM2: 265 { 266 showAlarm2(); 267 break; 268 } 269 case modeSETDATE: 270 { 271 setDate(); 272 break; 273 } 274 case modeSETTIME: 275 { 276 setTime(); 277 break; 278 } 279 case modeSETALARM1ON: 280 { 281 setAlarmOnOff(1); 282 break; 283 } 284 case modeSETALARM1: 285 { 286 setAlarm(1); 287 break; 288 } 289 case modeSETALARM1METHOD: 290 { 291 setAlarmMethod(1); 292 break; 293 } 294 case modeSETALARM2ON: 295 { 296 setAlarmOnOff(2); 297 break; 298 } 299 case modeSETALARM2: 300 { 301 setAlarm(2); 302 break; 303 } 304 case modeSETALARM2METHOD: 305 { 306 setAlarmMethod(2); 307 break; 308 } 309 } 310} 311 312/*************************************************************** 313 * Functions to read the buttons 314 ***************************************************************/ 315 316// read the buttons 317byte read_LCD_buttons() 318{ 319 adc_key_in = analogRead(0); // read the value from the sensor 320 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741 321 // we add approx 50 to those values and check to see if we are close 322 if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result 323 // For V1.1 us this threshold 324 if (adc_key_in < 50) return btnRIGHT; 325 if (adc_key_in < 250) return btnUP; 326 if (adc_key_in < 450) return btnDOWN; 327 if (adc_key_in < 650) return btnLEFT; 328 if (adc_key_in < 850) return btnSELECT; 329 return btnNONE; // when all others fail, return this... 330 331} 332 333/*************************************************************** 334 * Functions to read and display the time 335 ***************************************************************/ 336 337void displayText(String line0Text, String line1Text){ 338 lcd.setCursor(0,0); 339 sprintf(line0,"%-21s", line0Text.c_str()); 340 lcd.print(String(line0)); 341 lcd.setCursor(0,1); 342 sprintf(line1,"%-21s", line1Text.c_str()); 343 lcd.print(String(line1)); 344} 345 346String dateText() { 347 String result="Date: "; 348 bool Century=false; 349 if (blinkInt!=1 || blinkNow==false) 350 {result+=twoDigitNumber(Clock.getDate());} 351 else 352 {result+=" ";} 353 result+="/"; 354 if (blinkInt!=2 || blinkNow==false) 355 {result+=twoDigitNumber(Clock.getMonth(Century));} 356 else 357 {result+=" ";} 358 result+="/"; 359 if (blinkInt!=3 || blinkNow==false) 360 {result+=twoDigitNumber(Clock.getYear());} 361 else 362 {result+=" ";} 363 return result; 364} 365 366String timeText() { 367 String result="Time: "; 368 bool h12, PM; 369 if (blinkInt!=1 || blinkNow==false) 370 {result+=twoDigitNumber(Clock.getHour(h12, PM));} 371 else 372 {result+=" ";} 373 result+=":"; 374 if (blinkInt!=2 || blinkNow==false) 375 {result+=twoDigitNumber(Clock.getMinute());} 376 else 377 {result+=" ";} 378 result+=":"; 379 result+=twoDigitNumber(Clock.getSecond()); 380 return result; 381} 382 383void showDateTime(){ 384 displayText(dateText(), timeText()); 385} 386 387void showAlarm1(){ 388 389 byte ADay, AHour, AMinute, ASecond, ABits; 390 bool ADy, A12h, Apm; 391 392 String result1="Alarm1: "; 393 if (Clock.checkAlarmEnabled(1)) { 394 Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm); 395 if (blinkInt!=1 || blinkNow==false){ 396 if (ADy) { 397 result1+="DOW "; 398 } 399 else{ 400 result1+="DATE "; 401 } 402 } 403 else {result1+=" ";} 404 if (blinkInt!=2 || blinkNow==false){ 405 result1+=ADay; } 406 String result2="Alarm1: "; 407 if (blinkInt!=3 || blinkNow==false){ 408 result2+=twoDigitNumber(AHour);} 409 else {result2+=" ";} 410 result2+=":"; 411 if (blinkInt!=4 || blinkNow==false){ 412 result2+=twoDigitNumber(AMinute);} 413 else {result2+=" ";} 414 result2+=":"; 415 if (blinkInt!=5 || blinkNow==false){ 416 result2+=twoDigitNumber(ASecond); } 417 418 displayText(result1, result2); 419 } 420 else { 421 displayText("Alarm1: Off", ""); 422 } 423} 424 425void showAlarm2(){ 426 427 byte ADay, AHour, AMinute, ASecond, ABits; 428 bool ADy, A12h, Apm; 429 430 if (Clock.checkAlarmEnabled(1)) { 431 String result1="Alarm2: "; 432 Clock.getA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm); 433 if (blinkInt!=1 || blinkNow==false){ 434 if (ADy) { 435 result1+="DOW "; 436 } 437 else{ 438 result1+="DATE "; 439 } 440 } 441 else {result1+=" ";} 442 if (blinkInt!=2 || blinkNow==false){ 443 result1+=ADay; } 444 else {result1+=" ";} 445 String result2="Alarm2: "; 446 if (blinkInt!=3 || blinkNow==false){ 447 result2+=twoDigitNumber(AHour); 448 } 449 else {result2+=" ";} 450 result2+=":"; 451 if (blinkInt!=4 || blinkNow==false){ 452 result2+=twoDigitNumber(AMinute); 453 } 454 else {result2+=" ";} 455 displayText(result1, result2); 456 } 457 else { 458 displayText("Alarm2: Off", ""); 459 } 460} 461 462void showAlarmMethod(int alarmNum) { 463 464 String myString1=""; 465 String myString2=""; 466 467 byte ADay, AHour, AMinute, ASecond, ABitsOP=0b0; 468 bool ADy, A12h, Apm; 469 470 if (alarmNum==1){ 471 472 myString1 = "Alarm 1 Method:"; 473 474 Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABitsOP, ADy, A12h, Apm); 475 476 ABitsOP = ABitsOP & 0b1111; 477 478 if (ABitsOP==ALRM1_MATCH_EVERY_SEC) {myString2 = "Once per Second";} 479 else if (ABitsOP==ALRM1_MATCH_SEC) {myString2 = "Seconds Match";} 480 else if (ABitsOP==ALRM1_MATCH_MIN_SEC) {myString2 = "Min & Secs Match";} 481 else if (ABitsOP==ALRM1_MATCH_HR_MIN_SEC) {myString2 = "Hr, Min & Sec Match";} 482 else if (ABitsOP==ALRM1_MATCH_DY_HR_MIN_SEC) {myString2 = "Dy, Hr, Mn & Sec";} 483 } else { 484 485 Clock.getA2Time(ADay, AHour, AMinute, ABitsOP, ADy, A12h, Apm); 486 487 myString1 = "Alarm 2 Method:"; 488 489 if ((ABitsOP>>4)==ALRM2_ONCE_PER_MIN) {myString2 = "Once per Minute";} 490 else if ((ABitsOP>>4)==ALRM2_MATCH_MIN) {myString2 = "Match Minute";} 491 else {myString2 = "Match Hour & Min";} 492 } 493 displayText(myString1 , myString2); 494} 495 496String twoDigitNumber(byte number) 497{ 498 char buffer[3]; 499 snprintf(buffer,sizeof(buffer), "%02d", number ); 500 return String(buffer); 501} 502 503 504/*************************************************************** 505 * Functions to set the time 506 ***************************************************************/ 507 508void setDate(){ 509 displayText("Set the date:", dateText()); 510 maxBlinkInt=3; 511} 512 513void setTime(){ 514 displayText("Set the time:", timeText()); 515 maxBlinkInt=2; 516} 517 518void setAlarmOnOff(int alarmNum){ 519 if (alarmNum>0 && alarmNum<3) { 520 maxBlinkInt=1; 521 if(Clock.checkAlarmEnabled(alarmNum)){ 522 blinkInt=2;} 523 else { 524 blinkInt=1;} 525 if (blinkInt==1 && blinkNow==true) 526 {displayText("", "Alarm" + String(alarmNum) + ": ON");} 527 else if (blinkInt==2 && blinkNow==true) 528 {displayText("Alarm" + String(alarmNum) + ": OFF", "");} 529 else 530 { displayText("Alarm" + String(alarmNum) + ": OFF", "Alarm" + String(alarmNum) + ": ON");} 531 } 532} 533 534 535void setAlarm(int alarmNum){ 536 if (alarmNum>0 && alarmNum<3) { 537 if (alarmNum==1) 538 { 539 maxBlinkInt=5; 540 showAlarm1(); 541 } 542 if (alarmNum==2) 543 { 544 maxBlinkInt=4; 545 showAlarm2(); 546 } 547 } 548} 549 550void setAlarmMethod(int alarmNum){ 551 552 if (alarmNum>0 && alarmNum<3) { 553 554 if (alarmNum==1) 555 { 556 maxBlinkInt=1; 557 showAlarmMethod(1); 558 } 559 if (alarmNum==2) 560 { 561 maxBlinkInt=1; 562 showAlarmMethod(2); 563 } 564 } 565} 566 567/*************************************************************** 568 * Functions to increase and decrease time elements 569 ***************************************************************/ 570 571void increaseYear(){ 572 bool Century=false; 573 byte Year=Clock.getYear(); 574 if (Year<99) 575 {Year = Year + 1;} 576 else 577 {Year = 00;} 578 Clock.setYear(Year); 579 580 if (Clock.getDate()>monthMaxDays(Clock.getMonth(Century))){ 581 Clock.setDate(monthMaxDays(Clock.getMonth(Century))); 582 } 583} 584 585void decreaseYear(){ 586 bool Century=false; 587 byte Year=Clock.getYear(); 588 if (Year>1) 589 {Year = Year - 1;} 590 else 591 {Year = 99;} 592 Clock.setYear(Year); 593 594 if (Clock.getDate()>monthMaxDays(Clock.getMonth(Century))){ 595 Clock.setDate(monthMaxDays(Clock.getMonth(Century))); 596 } 597} 598 599void increaseMonth(){ 600 bool Century=false; 601 byte Month=Clock.getMonth(Century); 602 if (Month<12) { 603 Month = Month + 1; 604 } 605 else 606 { 607 Month = 1; 608 } 609 610 Clock.setMonth(Month); 611 612 if (Clock.getDate()>monthMaxDays(Clock.getMonth(Century))){ 613 Clock.setDate(monthMaxDays(Clock.getMonth(Century))); 614 } 615} 616 617void decreaseMonth(){ 618 bool Century=false; 619 byte Month=Clock.getMonth(Century); 620 if (Month>1) { 621 Month = Month - 1; 622 } 623 else 624 { 625 Month = 12; 626 } 627 Clock.setMonth(Month); 628 if (Clock.getDate()>monthMaxDays(Clock.getMonth(Century))){ 629 Clock.setDate(monthMaxDays(Clock.getMonth(Century))); 630 } 631} 632 633void increaseDate(){ 634 bool Century=false; 635 byte Date=Clock.getDate(); 636 if (Date<monthMaxDays(Clock.getMonth(Century))) { 637 Date = Date + 1; 638 } 639 else 640 { 641 Date = 1; 642 } 643 Clock.setDate(Date); 644} 645 646void decreaseDate(){ 647 bool Century=false; 648 byte Date=Clock.getDate(); 649 if(Date>1) { 650 Date = Date- 1; 651 } 652 else { 653 Date = monthMaxDays(Clock.getMonth(Century)); 654 } 655 Clock.setDate(Date); 656} 657 658void increaseHour(){ 659 bool h12, PM; 660 byte Hour=Clock.getHour(h12, PM); 661 if (Hour<24) 662 {Hour = Hour + 1;} 663 else 664 {Hour = 1;} 665 Clock.setHour(Hour); 666} 667 668void decreaseHour(){ 669 bool h12, PM; 670 byte Hour=Clock.getHour(h12, PM); 671 if (Hour>1) 672 {Hour = Hour - 1;} 673 else 674 {Hour = 24;} 675 Clock.setHour(Hour); 676} 677 678void increaseMinute(){ 679 byte Minute=Clock.getMinute(); 680 if (Minute<60) 681 {Minute = Minute + 1;} 682 else 683 {Minute = 1;} 684 Clock.setMinute(Minute); 685 Clock.setSecond(0); 686} 687 688void decreaseMinute(){ 689 byte Minute=Clock.getMinute(); 690 if (Minute>0) 691 {Minute = Minute - 1;} 692 else 693 {Minute = 60;} 694 Clock.setMinute(Minute); 695 Clock.setSecond(0); 696} 697 698int monthMaxDays(int monthNumber){ 699 switch (monthNumber){ 700 case 1: 701 case 3: 702 case 5: 703 case 7: 704 case 8: 705 case 10: 706 case 12: 707 return 31; 708 case 4: 709 case 6: 710 case 9: 711 case 11: 712 return 30; 713 case 2: 714 int remainingYears; 715 remainingYears=((Clock.getYear()-2020)%4); 716 if (remainingYears==0){ 717 return 29; 718 } 719 else{ 720 return 28; 721 } 722 default: 723 return 0; 724 } 725} 726 727/*************************************************************** 728 * Functions to set Alarms 729 ***************************************************************/ 730 731 void AlarmOn(int alarmNum, bool setOn){ 732 if (alarmNum>0 && alarmNum<3) { 733 if (setOn){Clock.turnOnAlarm(alarmNum);} 734 else {Clock.turnOffAlarm(alarmNum);} 735 } 736 } 737 738 void changeAlarmDayOption(int alarmNum){ 739 740 byte ADay, AHour, AMinute, ASecond, ABits; 741 bool ADy, A12h, Apm; 742 743 //Collect the current alarm settings 744 if (alarmNum==1){Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);} 745 if (alarmNum==2){Clock.getA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm);} 746 747 ADy=!ADy; 748 749 if (ADy && ADay>7) {ADay=7;} 750 751 //Reset the alarm settings 752 if (alarmNum==1){Clock.setA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);} 753 if (alarmNum==2){Clock.setA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm);} 754} 755 756 757 void ChangeAlarm(int alarmNum, int dayAdjust, int hourAdjust, int minAdjust,int secAdjust){ 758 759 byte ADay, AHour, AMinute, ASecond, ABits; 760 bool ADy, A12h, Apm; 761 762 //Collect the current alarm settings 763 if (alarmNum==1){Clock.getA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);} 764 if (alarmNum==2){Clock.getA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm);} 765 766 //Adjust the date 767 ADay+=dayAdjust; 768 if (ADy){ 769 if (ADay<1){ADay=7;} 770 if (ADay>7){ADay=1;} 771 } 772 else { 773 if (ADay<1){ADay=31;} 774 if (ADay>31){ADay=1;} 775 } 776 777 //Adjust the hour 778 AHour+=hourAdjust; 779 if (AHour<0){AHour=23;} 780 if (AHour>23){AHour=0;} 781 782 //Adjust the minute 783 AMinute+=minAdjust; 784 if (AMinute<0){AMinute=59;} 785 if (AMinute>59){AMinute=0;} 786 787 //Adjust the second 788 if (alarmNum==1){ 789 ASecond+=secAdjust; 790 if (ASecond<0){ASecond=59;} 791 if (ASecond>59){ASecond=0;} 792 } 793 794 //Reset the alarm settings 795 if (alarmNum==1){Clock.setA1Time(ADay, AHour, AMinute, ASecond, ABits, ADy, A12h, Apm);} 796 if (alarmNum==2){Clock.setA2Time(ADay, AHour, AMinute, ABits, ADy, A12h, Apm);} 797 798 } 799 800void changeAlarmMethod(int alarmNum, int dir) { 801 802 byte ADay1, AHour1, AMinute1, ASecond1, ADay2, AHour2, AMinute2, ABits=0b0; 803 bool ADy1, A12h1, Apm1, ADy2, A12h2, Apm2; 804 805 int AlarmBits; 806 807 if (alarmNum==1){ 808 Clock.getA1Time(ADay1, AHour1, AMinute1, ASecond1, ABits, ADy1, A12h1, Apm1); 809 ABits = ABits & 0b1111; 810 811 if (dir == 1) { 812 if (ABits==ALRM1_MATCH_EVERY_SEC) {AlarmBits |= ALRM1_MATCH_SEC;} 813 else if (ABits==ALRM1_MATCH_SEC) {AlarmBits |= ALRM1_MATCH_MIN_SEC;} 814 else if (ABits==ALRM1_MATCH_MIN_SEC) {AlarmBits |= ALRM1_MATCH_HR_MIN_SEC;} 815 else if (ABits==ALRM1_MATCH_HR_MIN_SEC) {AlarmBits |= ALRM1_MATCH_DY_HR_MIN_SEC;} 816 else if (ABits==ALRM1_MATCH_DY_HR_MIN_SEC) {AlarmBits |= ALRM1_MATCH_EVERY_SEC;} 817 } 818 else if (dir == 0) { 819 if (ABits==ALRM1_MATCH_EVERY_SEC) {AlarmBits |= ALRM1_MATCH_DY_HR_MIN_SEC;} 820 else if (ABits==ALRM1_MATCH_SEC) {AlarmBits |= ALRM1_MATCH_EVERY_SEC;} 821 else if (ABits==ALRM1_MATCH_MIN_SEC) {AlarmBits |= ALRM1_MATCH_SEC;} 822 else if (ABits==ALRM1_MATCH_HR_MIN_SEC) {AlarmBits |= ALRM1_MATCH_MIN_SEC;} 823 else {AlarmBits |= ALRM1_MATCH_HR_MIN_SEC;} 824 } 825 else {AlarmBits |= ABits;} 826 Clock.setA1Time(ADay1, AHour1, AMinute1, ASecond1, AlarmBits, ADy1, A12h1, Apm1); 827 828 } else { 829 830 Clock.getA2Time(ADay2, AHour2, AMinute2, ABits, ADy2, A12h2, Apm2); 831 ABits = ABits >> 4; 832 833 if (dir == 1) { 834 if (ABits==ALRM2_ONCE_PER_MIN) {AlarmBits = ALRM2_MATCH_MIN;} 835 else if (ABits==ALRM2_MATCH_MIN) {AlarmBits = ALRM2_MATCH_HR_MIN;} 836 else {AlarmBits = ALRM2_ONCE_PER_MIN;} 837 } 838 if (dir == 0) { 839 if (ABits==ALRM2_ONCE_PER_MIN) {AlarmBits = ALRM2_MATCH_HR_MIN;} 840 else if (ABits==ALRM2_MATCH_HR_MIN) {AlarmBits = ALRM2_MATCH_MIN;} 841 else {AlarmBits = ALRM2_ONCE_PER_MIN;} 842 } 843 AlarmBits = AlarmBits << 4; 844 Clock.setA2Time(ADay2, AHour2, AMinute2, AlarmBits, ADy2, A12h2, Apm2); 845 846 } 847} 848
Downloadable files
Circuit
The circuit for the finished item. Note: Pin 10 was NOT connected as it showed as a bad circuit when tested - this is a common issue with cheaper generic shields
Circuit
Circuit
The circuit for the finished item. Note: Pin 10 was NOT connected as it showed as a bad circuit when tested - this is a common issue with cheaper generic shields
Circuit
Documentation
Holder STL File
Holder STL File
Holder STL File
Panel STL File
Panel STL File
Panel STL File
Holder STL File
Holder STL File
Holder STL File
Panel STL File
Panel STL File
Panel STL File
Comments
Only logged in users can leave comments
guy-l
0 Followers
•0 Projects
Table of contents
Intro
8
0