Components and supplies
1
Arduino UNO
1
LCM 1602 IIC
1
dc water pump
1
Alphanumeric LCD, 16 x 2
1
Breadboard (generic)
Project description
Code
untitled
arduino
1#include <Wire.h> 2#include <RTClib.h> 3#include <LiquidCrystal_I2C.h> 4 5//************************************// 6LiquidCrystal_I2C lcd(0x27,20,4); // Display I2C 20 x 4 7RTC_DS1307 RTC; 8 9//************Button*****************// 10int P1=6; // Button SET MENU' 11int P2=7; // Button + 12int P3=8; // Button - 13int P4=9; // SWITCH Alarm 14 15//**************Alarm***************// 16#define LED 13 17#define dc water pump 10 18 19//************Variables**************// 20int hourupg; 21int minupg; 22int yearupg; 23int monthupg; 24int dayupg; 25int menu =0; 26int setAll =0; 27 28uint8_t alarmHours = 0, alarmMinutes = 0; // Holds the current alarm time 29 30 31void setup() 32{ 33 34 lcd.begin(); 35 lcd.backlight(); 36 lcd.clear(); 37 38 pinMode(P1,INPUT_PULLUP); // https://www.arduino.cc/en/Tutorial/InputPullupSerial 39 pinMode(P2,INPUT_PULLUP); 40 pinMode(P3,INPUT_PULLUP); 41 pinMode(P4,INPUT_PULLUP); 42 pinMode(LED,OUTPUT); 43 pinMode(dc water pump, OUTPUT); // Set dc water pump as an output 44 printAllOff(); 45 Serial.begin(9600); 46 Wire.begin(); 47 RTC.begin(); 48 49 if (! RTC.isrunning()) { 50 Serial.println("RTC is NOT running!"); 51 // Set the date and time at compile time 52 RTC.adjust(DateTime(__DATE__, __TIME__)); 53 } 54 // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time 55 // The default display shows the date and time 56 int menu=0; 57} 58 59void loop() 60{ 61 62// check if you press the SET button and increase the menu index 63 if(digitalRead(P1)== LOW) 64 { 65 menu=menu+1; 66 } 67 if((digitalRead(P2)== LOW)&&(digitalRead(P3)== LOW)) 68 { 69 70 DisplaySetHourAll(); 71 DisplaySetMinuteAll(); 72 lcd.clear(); 73 lcd.setCursor(5,0); 74 lcd.print("ALARM"); 75 lcd.setCursor(5,1); 76 lcd.print(alarmHours, DEC); 77 lcd.print(":"); 78 lcd.print(alarmMinutes, DEC); 79 delay(1000); 80 lcd.clear(); 81 } 82// in which subroutine should we go? 83 if (menu==0) 84 { 85 DisplayDateTime(); // void DisplayDateTime 86 Alarm(); // Alarm control 87 } 88 if (menu==1) 89 { 90 DisplaySetHour(); 91 } 92 if (menu==2) 93 { 94 DisplaySetMinute(); 95 } 96 if (menu==3) 97 { 98 DisplaySetYear(); 99 } 100 if (menu==4) 101 { 102 DisplaySetMonth(); 103 } 104 if (menu==5) 105 { 106 DisplaySetDay(); 107 } 108 if (menu==6) 109 { 110 StoreAgg(); 111 delay(500); 112 menu=0; 113 } 114 delay(100); 115} 116 117void DisplayDateTime () 118{ 119// We show the current date and time 120 DateTime now = RTC.now(); 121 122 lcd.setCursor(0, 2); 123 lcd.print("Hour : "); 124 125 if (now.hour()<=9) 126 { 127 lcd.print("0"); 128 } 129 lcd.print(now.hour(), DEC); 130 hourupg=now.hour(); 131 lcd.print(":"); 132 if (now.minute()<=9) 133 { 134 lcd.print("0"); 135 } 136 lcd.print(now.minute(), DEC); 137 minupg=now.minute(); 138 lcd.print(":"); 139 if (now.second()<=9) 140 { 141 lcd.print("0"); 142 } 143 lcd.print(now.second(), DEC); 144 145 146 lcd.setCursor(0, 1); 147 lcd.print("Date : "); 148 if (now.day()<=9) 149 { 150 lcd.print("0"); 151 } 152 lcd.print(now.day(), DEC); 153 dayupg=now.day(); 154 lcd.print("/"); 155 if (now.month()<=9) 156 { 157 lcd.print("0"); 158 } 159 lcd.print(now.month(), DEC); 160 monthupg=now.month(); 161 lcd.print("/"); 162 lcd.print(now.year(), DEC); 163 yearupg=now.year(); 164 165 char DOW[][10]={"Sunday ","Monday ","Tuesday ","Wednesday","Thursday ","Friday ","Saturday "}; 166 lcd.setCursor(0, 0); 167 lcd.print("Day : "); 168 lcd.print(DOW[now.dayOfTheWeek()]); // if it appears error in the code, enter the code given below 169 //lcd.print(DOW[now.dayOfWeek()]); 170} 171 172void DisplaySetHour() 173{ 174// time setting 175 lcd.clear(); 176 DateTime now = RTC.now(); 177 if(digitalRead(P2)==LOW) 178 { 179 if(hourupg==23) 180 { 181 hourupg=0; 182 } 183 else 184 { 185 hourupg=hourupg+1; 186 } 187 } 188 if(digitalRead(P3)==LOW) 189 { 190 if(hourupg==0) 191 { 192 hourupg=23; 193 } 194 else 195 { 196 hourupg=hourupg-1; 197 } 198 } 199 lcd.setCursor(0,0); 200 lcd.print("Set time:"); 201 lcd.setCursor(0,1); 202 lcd.print(hourupg,DEC); 203 delay(200); 204} 205 206void DisplaySetMinute() 207{ 208// Setting the minutes 209 lcd.clear(); 210 if(digitalRead(P2)==LOW) 211 { 212 if (minupg==59) 213 { 214 minupg=0; 215 } 216 else 217 { 218 minupg=minupg+1; 219 } 220 } 221 if(digitalRead(P3)==LOW) 222 { 223 if (minupg==0) 224 { 225 minupg=59; 226 } 227 else 228 { 229 minupg=minupg-1; 230 } 231 } 232 lcd.setCursor(0,0); 233 lcd.print("Set Minutes:"); 234 lcd.setCursor(0,1); 235 lcd.print(minupg,DEC); 236 delay(200); 237} 238 239void DisplaySetYear() 240{ 241// setting the year 242 lcd.clear(); 243 if(digitalRead(P2)==LOW) 244 { 245 yearupg=yearupg+1; 246 } 247 if(digitalRead(P3)==LOW) 248 { 249 yearupg=yearupg-1; 250 } 251 lcd.setCursor(0,0); 252 lcd.print("Set Year:"); 253 lcd.setCursor(0,1); 254 lcd.print(yearupg,DEC); 255 delay(200); 256} 257 258void DisplaySetMonth() 259{ 260// Setting the month 261 lcd.clear(); 262 if(digitalRead(P2)==LOW) 263 { 264 if (monthupg==12) 265 { 266 monthupg=1; 267 } 268 else 269 { 270 monthupg=monthupg+1; 271 } 272 } 273 if(digitalRead(P3)==LOW) 274 { 275 if (monthupg==1) 276 { 277 monthupg=12; 278 } 279 else 280 { 281 monthupg=monthupg-1; 282 } 283 } 284 lcd.setCursor(0,0); 285 lcd.print("Set Month:"); 286 lcd.setCursor(0,1); 287 lcd.print(monthupg,DEC); 288 delay(200); 289} 290 291void DisplaySetDay() 292{ 293// Setting the day 294 lcd.clear(); 295 if(digitalRead(P2)==LOW) 296 { 297 if (dayupg==31) 298 { 299 dayupg=1; 300 } 301 else 302 { 303 dayupg=dayupg+1; 304 } 305 } 306 if(digitalRead(P3)==LOW) 307 { 308 if (dayupg==1) 309 { 310 dayupg=31; 311 } 312 else 313 { 314 dayupg=dayupg-1; 315 } 316 } 317 lcd.setCursor(0,0); 318 lcd.print("Set Day:"); 319 lcd.setCursor(0,1); 320 lcd.print(dayupg,DEC); 321 delay(200); 322} 323 324void StoreAgg() 325{ 326// Variable saving 327 lcd.clear(); 328 lcd.setCursor(0,0); 329 lcd.print("SAVING IN"); 330 lcd.setCursor(0,1); 331 lcd.print("PROGRESS"); 332 RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0)); 333 delay(200); 334} 335void DisplaySetHourAll()// Setting the alarm minutes 336{ 337 while(digitalRead(P1)==HIGH){ 338 339 lcd.clear(); 340 341 if(digitalRead(P2)==LOW) 342 { 343 if(alarmHours==23) 344 { 345 alarmHours=0; 346 } 347 else 348 { 349 alarmHours=alarmHours+1; 350 } 351 } 352 if(digitalRead(P3)==LOW) 353 { 354 if(alarmHours==0) 355 { 356 alarmHours=23; 357 } 358 else 359 { 360 alarmHours=alarmHours-1; 361 } 362 } 363 lcd.setCursor(0,0); 364 lcd.print("Set HOUR Alarm:"); 365 lcd.setCursor(0,1); 366 lcd.print(alarmHours,DEC); 367 delay(200); 368 } 369 delay(200); 370} 371 372void DisplaySetMinuteAll()// Setting the alarm minutes 373 { 374 while(digitalRead(P1)==HIGH){ 375 376 lcd.clear(); 377 if(digitalRead(P2)==LOW) 378 { 379 if (alarmMinutes==59) 380 { 381 alarmMinutes=0; 382 } 383 else 384 { 385 alarmMinutes=alarmMinutes+1; 386 } 387 } 388 if(digitalRead(P3)==LOW) 389 { 390 if (alarmMinutes==0) 391 { 392 alarmMinutes=59; 393 } 394 else 395 { 396 alarmMinutes=alarmMinutes-1; 397 } 398 } 399 lcd.setCursor(0,0); 400 lcd.print("Set MIN. Alarm:"); 401 lcd.setCursor(0,1); 402 lcd.print(alarmMinutes,DEC); 403 delay(200); 404 } 405 delay(200); 406} 407void printAllOn(){ 408 lcd.setCursor(0,3); 409 lcd.print("Alarm: "); 410 411 412 413 if (alarmHours <= 9) 414 { 415 lcd.print("0"); 416 } 417 lcd.print(alarmHours, DEC); 418 419 lcd.print(":"); 420 if (alarmMinutes <= 9) 421 { 422 lcd.print("0"); 423 } 424 lcd.print(alarmMinutes, DEC); 425 426} 427void printAllOff() { 428 lcd.setCursor(0, 3); 429 lcd.print("Alarm: Off "); 430} 431void Alarm(){ 432 if(digitalRead(P4)== LOW) 433 { 434 setAll=setAll+1; 435 } 436 if (setAll==0) 437 { 438 printAllOff(); 439 noTone (dc water pump); 440 digitalWrite(LED,LOW); 441 } 442 if (setAll==1) 443 { 444 445 printAllOn(); 446 447 DateTime now = RTC.now(); 448 if ( now.hour() == alarmHours && now.minute() == alarmMinutes ) 449 { 450 lcd.noBacklight(); 451 DateTime now = RTC.now(); 452 digitalWrite(LED,HIGH); 453 tone(dc water pump,880); //play the note "A5" (LA5) 454 delay (300); 455 tone(dc water pump,698); //play the note "F6" (FA5) 456 lcd.backlight(); 457 } 458 else{ 459 noTone (dc water pump); 460 digitalWrite(LED,LOW); 461 } 462 463 } 464 if (setAll==2) 465 { 466 setAll=0; 467 } 468 delay(200); 469}#include <Wire.h> 470#include <RTClib.h> 471#include <LiquidCrystal_I2C.h> 472 473//************************************// 474LiquidCrystal_I2C lcd(0x27,20,4); // Display I2C 20 x 4 475RTC_DS1307 RTC; 476 477//************Button*****************// 478int P1=6; // Button SET MENU' 479int P2=7; // Button + 480int P3=8; // Button - 481int P4=9; // SWITCH Alarm 482 483//**************Alarm***************// 484#define LED 13 485#define dc water pump 10 486 487//************Variables**************// 488int hourupg; 489int minupg; 490int yearupg; 491int monthupg; 492int dayupg; 493int menu =0; 494int setAll =0; 495 496uint8_t alarmHours = 0, alarmMinutes = 0; // Holds the current alarm time 497 498 499void setup() 500{ 501 502 lcd.begin(); 503 lcd.backlight(); 504 lcd.clear(); 505 506 pinMode(P1,INPUT_PULLUP); // https://www.arduino.cc/en/Tutorial/InputPullupSerial 507 pinMode(P2,INPUT_PULLUP); 508 pinMode(P3,INPUT_PULLUP); 509 pinMode(P4,INPUT_PULLUP); 510 pinMode(LED,OUTPUT); 511 pinMode(dc water pump, OUTPUT); // Set dc water pump as an output 512 printAllOff(); 513 Serial.begin(9600); 514 Wire.begin(); 515 RTC.begin(); 516 517 if (! RTC.isrunning()) { 518 Serial.println("RTC is NOT running!"); 519 // Set the date and time at compile time 520 RTC.adjust(DateTime(__DATE__, __TIME__)); 521 } 522 // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time 523 // The default display shows the date and time 524 int menu=0; 525} 526 527void loop() 528{ 529 530// check if you press the SET button and increase the menu index 531 if(digitalRead(P1)== LOW) 532 { 533 menu=menu+1; 534 } 535 if((digitalRead(P2)== LOW)&&(digitalRead(P3)== LOW)) 536 { 537 538 DisplaySetHourAll(); 539 DisplaySetMinuteAll(); 540 lcd.clear(); 541 lcd.setCursor(5,0); 542 lcd.print("ALARM"); 543 lcd.setCursor(5,1); 544 lcd.print(alarmHours, DEC); 545 lcd.print(":"); 546 lcd.print(alarmMinutes, DEC); 547 delay(1000); 548 lcd.clear(); 549 } 550// in which subroutine should we go? 551 if (menu==0) 552 { 553 DisplayDateTime(); // void DisplayDateTime 554 Alarm(); // Alarm control 555 } 556 if (menu==1) 557 { 558 DisplaySetHour(); 559 } 560 if (menu==2) 561 { 562 DisplaySetMinute(); 563 } 564 if (menu==3) 565 { 566 DisplaySetYear(); 567 } 568 if (menu==4) 569 { 570 DisplaySetMonth(); 571 } 572 if (menu==5) 573 { 574 DisplaySetDay(); 575 } 576 if (menu==6) 577 { 578 StoreAgg(); 579 delay(500); 580 menu=0; 581 } 582 delay(100); 583} 584 585void DisplayDateTime () 586{ 587// We show the current date and time 588 DateTime now = RTC.now(); 589 590 lcd.setCursor(0, 2); 591 lcd.print("Hour : "); 592 593 if (now.hour()<=9) 594 { 595 lcd.print("0"); 596 } 597 lcd.print(now.hour(), DEC); 598 hourupg=now.hour(); 599 lcd.print(":"); 600 if (now.minute()<=9) 601 { 602 lcd.print("0"); 603 } 604 lcd.print(now.minute(), DEC); 605 minupg=now.minute(); 606 lcd.print(":"); 607 if (now.second()<=9) 608 { 609 lcd.print("0"); 610 } 611 lcd.print(now.second(), DEC); 612 613 614 lcd.setCursor(0, 1); 615 lcd.print("Date : "); 616 if (now.day()<=9) 617 { 618 lcd.print("0"); 619 } 620 lcd.print(now.day(), DEC); 621 dayupg=now.day(); 622 lcd.print("/"); 623 if (now.month()<=9) 624 { 625 lcd.print("0"); 626 } 627 lcd.print(now.month(), DEC); 628 monthupg=now.month(); 629 lcd.print("/"); 630 lcd.print(now.year(), DEC); 631 yearupg=now.year(); 632 633 char DOW[][10]={"Sunday ","Monday ","Tuesday ","Wednesday","Thursday ","Friday ","Saturday "}; 634 lcd.setCursor(0, 0); 635 lcd.print("Day : "); 636 lcd.print(DOW[now.dayOfTheWeek()]); // if it appears error in the code, enter the code given below 637 //lcd.print(DOW[now.dayOfWeek()]); 638} 639 640void DisplaySetHour() 641{ 642// time setting 643 lcd.clear(); 644 DateTime now = RTC.now(); 645 if(digitalRead(P2)==LOW) 646 { 647 if(hourupg==23) 648 { 649 hourupg=0; 650 } 651 else 652 { 653 hourupg=hourupg+1; 654 } 655 } 656 if(digitalRead(P3)==LOW) 657 { 658 if(hourupg==0) 659 { 660 hourupg=23; 661 } 662 else 663 { 664 hourupg=hourupg-1; 665 } 666 } 667 lcd.setCursor(0,0); 668 lcd.print("Set time:"); 669 lcd.setCursor(0,1); 670 lcd.print(hourupg,DEC); 671 delay(200); 672} 673 674void DisplaySetMinute() 675{ 676// Setting the minutes 677 lcd.clear(); 678 if(digitalRead(P2)==LOW) 679 { 680 if (minupg==59) 681 { 682 minupg=0; 683 } 684 else 685 { 686 minupg=minupg+1; 687 } 688 } 689 if(digitalRead(P3)==LOW) 690 { 691 if (minupg==0) 692 { 693 minupg=59; 694 } 695 else 696 { 697 minupg=minupg-1; 698 } 699 } 700 lcd.setCursor(0,0); 701 lcd.print("Set Minutes:"); 702 lcd.setCursor(0,1); 703 lcd.print(minupg,DEC); 704 delay(200); 705} 706 707void DisplaySetYear() 708{ 709// setting the year 710 lcd.clear(); 711 if(digitalRead(P2)==LOW) 712 { 713 yearupg=yearupg+1; 714 } 715 if(digitalRead(P3)==LOW) 716 { 717 yearupg=yearupg-1; 718 } 719 lcd.setCursor(0,0); 720 lcd.print("Set Year:"); 721 lcd.setCursor(0,1); 722 lcd.print(yearupg,DEC); 723 delay(200); 724} 725 726void DisplaySetMonth() 727{ 728// Setting the month 729 lcd.clear(); 730 if(digitalRead(P2)==LOW) 731 { 732 if (monthupg==12) 733 { 734 monthupg=1; 735 } 736 else 737 { 738 monthupg=monthupg+1; 739 } 740 } 741 if(digitalRead(P3)==LOW) 742 { 743 if (monthupg==1) 744 { 745 monthupg=12; 746 } 747 else 748 { 749 monthupg=monthupg-1; 750 } 751 } 752 lcd.setCursor(0,0); 753 lcd.print("Set Month:"); 754 lcd.setCursor(0,1); 755 lcd.print(monthupg,DEC); 756 delay(200); 757} 758 759void DisplaySetDay() 760{ 761// Setting the day 762 lcd.clear(); 763 if(digitalRead(P2)==LOW) 764 { 765 if (dayupg==31) 766 { 767 dayupg=1; 768 } 769 else 770 { 771 dayupg=dayupg+1; 772 } 773 } 774 if(digitalRead(P3)==LOW) 775 { 776 if (dayupg==1) 777 { 778 dayupg=31; 779 } 780 else 781 { 782 dayupg=dayupg-1; 783 } 784 } 785 lcd.setCursor(0,0); 786 lcd.print("Set Day:"); 787 lcd.setCursor(0,1); 788 lcd.print(dayupg,DEC); 789 delay(200); 790} 791 792void StoreAgg() 793{ 794// Variable saving 795 lcd.clear(); 796 lcd.setCursor(0,0); 797 lcd.print("SAVING IN"); 798 lcd.setCursor(0,1); 799 lcd.print("PROGRESS"); 800 RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0)); 801 delay(200); 802} 803void DisplaySetHourAll()// Setting the alarm minutes 804{ 805 while(digitalRead(P1)==HIGH){ 806 807 lcd.clear(); 808 809 if(digitalRead(P2)==LOW) 810 { 811 if(alarmHours==23) 812 { 813 alarmHours=0; 814 } 815 else 816 { 817 alarmHours=alarmHours+1; 818 } 819 } 820 if(digitalRead(P3)==LOW) 821 { 822 if(alarmHours==0) 823 { 824 alarmHours=23; 825 } 826 else 827 { 828 alarmHours=alarmHours-1; 829 } 830 } 831 lcd.setCursor(0,0); 832 lcd.print("Set HOUR Alarm:"); 833 lcd.setCursor(0,1); 834 lcd.print(alarmHours,DEC); 835 delay(200); 836 } 837 delay(200); 838} 839 840void DisplaySetMinuteAll()// Setting the alarm minutes 841 { 842 while(digitalRead(P1)==HIGH){ 843 844 lcd.clear(); 845 if(digitalRead(P2)==LOW) 846 { 847 if (alarmMinutes==59) 848 { 849 alarmMinutes=0; 850 } 851 else 852 { 853 alarmMinutes=alarmMinutes+1; 854 } 855 } 856 if(digitalRead(P3)==LOW) 857 { 858 if (alarmMinutes==0) 859 { 860 alarmMinutes=59; 861 } 862 else 863 { 864 alarmMinutes=alarmMinutes-1; 865 } 866 } 867 lcd.setCursor(0,0); 868 lcd.print("Set MIN. Alarm:"); 869 lcd.setCursor(0,1); 870 lcd.print(alarmMinutes,DEC); 871 delay(200); 872 } 873 delay(200); 874} 875void printAllOn(){ 876 lcd.setCursor(0,3); 877 lcd.print("Alarm: "); 878 879 880 881 if (alarmHours <= 9) 882 { 883 lcd.print("0"); 884 } 885 lcd.print(alarmHours, DEC); 886 887 lcd.print(":"); 888 if (alarmMinutes <= 9) 889 { 890 lcd.print("0"); 891 } 892 lcd.print(alarmMinutes, DEC); 893 894} 895void printAllOff() { 896 lcd.setCursor(0, 3); 897 lcd.print("Alarm: Off "); 898} 899void Alarm(){ 900 if(digitalRead(P4)== LOW) 901 { 902 setAll=setAll+1; 903 } 904 if (setAll==0) 905 { 906 printAllOff(); 907 noTone (dc water pump); 908 digitalWrite(LED,LOW); 909 } 910 if (setAll==1) 911 { 912 913 printAllOn(); 914 915 DateTime now = RTC.now(); 916 if ( now.hour() == alarmHours && now.minute() == alarmMinutes ) 917 { 918 lcd.noBacklight(); 919 DateTime now = RTC.now(); 920 digitalWrite(LED,HIGH); 921 tone(dc water pump,880); //play the note "A5" (LA5) 922 delay (300); 923 tone(dc water pump,698); //play the note "F6" (FA5) 924 lcd.backlight(); 925 } 926 else{ 927 noTone (dc water pump); 928 digitalWrite(LED,LOW); 929 } 930 931 } 932 if (setAll==2) 933 { 934 setAll=0; 935 } 936 delay(200); 937}#include <Wire.h> 938#include <RTClib.h> 939#include <LiquidCrystal_I2C.h> 940 941//************************************// 942LiquidCrystal_I2C lcd(0x27,20,4); // Display I2C 20 x 4 943RTC_DS1307 RTC; 944 945//************Button*****************// 946int P1=6; // Button SET MENU' 947int P2=7; // Button + 948int P3=8; // Button - 949int P4=9; // SWITCH Alarm 950 951//**************Alarm***************// 952#define LED 13 953#define dc water pump 10 954 955//************Variables**************// 956int hourupg; 957int minupg; 958int yearupg; 959int monthupg; 960int dayupg; 961int menu =0; 962int setAll =0; 963 964uint8_t alarmHours = 0, alarmMinutes = 0; // Holds the current alarm time 965 966 967void setup() 968{ 969 970 lcd.begin(); 971 lcd.backlight(); 972 lcd.clear(); 973 974 pinMode(P1,INPUT_PULLUP); // https://www.arduino.cc/en/Tutorial/InputPullupSerial 975 pinMode(P2,INPUT_PULLUP); 976 pinMode(P3,INPUT_PULLUP); 977 pinMode(P4,INPUT_PULLUP); 978 pinMode(LED,OUTPUT); 979 pinMode(dc water pump, OUTPUT); // Set dc water pump as an output 980 printAllOff(); 981 Serial.begin(9600); 982 Wire.begin(); 983 RTC.begin(); 984 985 if (! RTC.isrunning()) { 986 Serial.println("RTC is NOT running!"); 987 // Set the date and time at compile time 988 RTC.adjust(DateTime(__DATE__, __TIME__)); 989 } 990 // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time 991 // The default display shows the date and time 992 int menu=0; 993} 994 995void loop() 996{ 997 998// check if you press the SET button and increase the menu index 999 if(digitalRead(P1)== LOW) 1000 { 1001 menu=menu+1; 1002 } 1003 if((digitalRead(P2)== LOW)&&(digitalRead(P3)== LOW)) 1004 { 1005 1006 DisplaySetHourAll(); 1007 DisplaySetMinuteAll(); 1008 lcd.clear(); 1009 lcd.setCursor(5,0); 1010 lcd.print("ALARM"); 1011 lcd.setCursor(5,1); 1012 lcd.print(alarmHours, DEC); 1013 lcd.print(":"); 1014 lcd.print(alarmMinutes, DEC); 1015 delay(1000); 1016 lcd.clear(); 1017 } 1018// in which subroutine should we go? 1019 if (menu==0) 1020 { 1021 DisplayDateTime(); // void DisplayDateTime 1022 Alarm(); // Alarm control 1023 } 1024 if (menu==1) 1025 { 1026 DisplaySetHour(); 1027 } 1028 if (menu==2) 1029 { 1030 DisplaySetMinute(); 1031 } 1032 if (menu==3) 1033 { 1034 DisplaySetYear(); 1035 } 1036 if (menu==4) 1037 { 1038 DisplaySetMonth(); 1039 } 1040 if (menu==5) 1041 { 1042 DisplaySetDay(); 1043 } 1044 if (menu==6) 1045 { 1046 StoreAgg(); 1047 delay(500); 1048 menu=0; 1049 } 1050 delay(100); 1051} 1052 1053void DisplayDateTime () 1054{ 1055// We show the current date and time 1056 DateTime now = RTC.now(); 1057 1058 lcd.setCursor(0, 2); 1059 lcd.print("Hour : "); 1060 1061 if (now.hour()<=9) 1062 { 1063 lcd.print("0"); 1064 } 1065 lcd.print(now.hour(), DEC); 1066 hourupg=now.hour(); 1067 lcd.print(":"); 1068 if (now.minute()<=9) 1069 { 1070 lcd.print("0"); 1071 } 1072 lcd.print(now.minute(), DEC); 1073 minupg=now.minute(); 1074 lcd.print(":"); 1075 if (now.second()<=9) 1076 { 1077 lcd.print("0"); 1078 } 1079 lcd.print(now.second(), DEC); 1080 1081 1082 lcd.setCursor(0, 1); 1083 lcd.print("Date : "); 1084 if (now.day()<=9) 1085 { 1086 lcd.print("0"); 1087 } 1088 lcd.print(now.day(), DEC); 1089 dayupg=now.day(); 1090 lcd.print("/"); 1091 if (now.month()<=9) 1092 { 1093 lcd.print("0"); 1094 } 1095 lcd.print(now.month(), DEC); 1096 monthupg=now.month(); 1097 lcd.print("/"); 1098 lcd.print(now.year(), DEC); 1099 yearupg=now.year(); 1100 1101 char DOW[][10]={"Sunday ","Monday ","Tuesday ","Wednesday","Thursday ","Friday ","Saturday "}; 1102 lcd.setCursor(0, 0); 1103 lcd.print("Day : "); 1104 lcd.print(DOW[now.dayOfTheWeek()]); // if it appears error in the code, enter the code given below 1105 //lcd.print(DOW[now.dayOfWeek()]); 1106} 1107 1108void DisplaySetHour() 1109{ 1110// time setting 1111 lcd.clear(); 1112 DateTime now = RTC.now(); 1113 if(digitalRead(P2)==LOW) 1114 { 1115 if(hourupg==23) 1116 { 1117 hourupg=0; 1118 } 1119 else 1120 { 1121 hourupg=hourupg+1; 1122 } 1123 } 1124 if(digitalRead(P3)==LOW) 1125 { 1126 if(hourupg==0) 1127 { 1128 hourupg=23; 1129 } 1130 else 1131 { 1132 hourupg=hourupg-1; 1133 } 1134 } 1135 lcd.setCursor(0,0); 1136 lcd.print("Set time:"); 1137 lcd.setCursor(0,1); 1138 lcd.print(hourupg,DEC); 1139 delay(200); 1140} 1141 1142void DisplaySetMinute() 1143{ 1144// Setting the minutes 1145 lcd.clear(); 1146 if(digitalRead(P2)==LOW) 1147 { 1148 if (minupg==59) 1149 { 1150 minupg=0; 1151 } 1152 else 1153 { 1154 minupg=minupg+1; 1155 } 1156 } 1157 if(digitalRead(P3)==LOW) 1158 { 1159 if (minupg==0) 1160 { 1161 minupg=59; 1162 } 1163 else 1164 { 1165 minupg=minupg-1; 1166 } 1167 } 1168 lcd.setCursor(0,0); 1169 lcd.print("Set Minutes:"); 1170 lcd.setCursor(0,1); 1171 lcd.print(minupg,DEC); 1172 delay(200); 1173} 1174 1175void DisplaySetYear() 1176{ 1177// setting the year 1178 lcd.clear(); 1179 if(digitalRead(P2)==LOW) 1180 { 1181 yearupg=yearupg+1; 1182 } 1183 if(digitalRead(P3)==LOW) 1184 { 1185 yearupg=yearupg-1; 1186 } 1187 lcd.setCursor(0,0); 1188 lcd.print("Set Year:"); 1189 lcd.setCursor(0,1); 1190 lcd.print(yearupg,DEC); 1191 delay(200); 1192} 1193 1194void DisplaySetMonth() 1195{ 1196// Setting the month 1197 lcd.clear(); 1198 if(digitalRead(P2)==LOW) 1199 { 1200 if (monthupg==12) 1201 { 1202 monthupg=1; 1203 } 1204 else 1205 { 1206 monthupg=monthupg+1; 1207 } 1208 } 1209 if(digitalRead(P3)==LOW) 1210 { 1211 if (monthupg==1) 1212 { 1213 monthupg=12; 1214 } 1215 else 1216 { 1217 monthupg=monthupg-1; 1218 } 1219 } 1220 lcd.setCursor(0,0); 1221 lcd.print("Set Month:"); 1222 lcd.setCursor(0,1); 1223 lcd.print(monthupg,DEC); 1224 delay(200); 1225} 1226 1227void DisplaySetDay() 1228{ 1229// Setting the day 1230 lcd.clear(); 1231 if(digitalRead(P2)==LOW) 1232 { 1233 if (dayupg==31) 1234 { 1235 dayupg=1; 1236 } 1237 else 1238 { 1239 dayupg=dayupg+1; 1240 } 1241 } 1242 if(digitalRead(P3)==LOW) 1243 { 1244 if (dayupg==1) 1245 { 1246 dayupg=31; 1247 } 1248 else 1249 { 1250 dayupg=dayupg-1; 1251 } 1252 } 1253 lcd.setCursor(0,0); 1254 lcd.print("Set Day:"); 1255 lcd.setCursor(0,1); 1256 lcd.print(dayupg,DEC); 1257 delay(200); 1258} 1259 1260void StoreAgg() 1261{ 1262// Variable saving 1263 lcd.clear(); 1264 lcd.setCursor(0,0); 1265 lcd.print("SAVING IN"); 1266 lcd.setCursor(0,1); 1267 lcd.print("PROGRESS"); 1268 RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0)); 1269 delay(200); 1270} 1271void DisplaySetHourAll()// Setting the alarm minutes 1272{ 1273 while(digitalRead(P1)==HIGH){ 1274 1275 lcd.clear(); 1276 1277 if(digitalRead(P2)==LOW) 1278 { 1279 if(alarmHours==23) 1280 { 1281 alarmHours=0; 1282 } 1283 else 1284 { 1285 alarmHours=alarmHours+1; 1286 } 1287 } 1288 if(digitalRead(P3)==LOW) 1289 { 1290 if(alarmHours==0) 1291 { 1292 alarmHours=23; 1293 } 1294 else 1295 { 1296 alarmHours=alarmHours-1; 1297 } 1298 } 1299 lcd.setCursor(0,0); 1300 lcd.print("Set HOUR Alarm:"); 1301 lcd.setCursor(0,1); 1302 lcd.print(alarmHours,DEC); 1303 delay(200); 1304 } 1305 delay(200); 1306} 1307 1308void DisplaySetMinuteAll()// Setting the alarm minutes 1309 { 1310 while(digitalRead(P1)==HIGH){ 1311 1312 lcd.clear(); 1313 if(digitalRead(P2)==LOW) 1314 { 1315 if (alarmMinutes==59) 1316 { 1317 alarmMinutes=0; 1318 } 1319 else 1320 { 1321 alarmMinutes=alarmMinutes+1; 1322 } 1323 } 1324 if(digitalRead(P3)==LOW) 1325 { 1326 if (alarmMinutes==0) 1327 { 1328 alarmMinutes=59; 1329 } 1330 else 1331 { 1332 alarmMinutes=alarmMinutes-1; 1333 } 1334 } 1335 lcd.setCursor(0,0); 1336 lcd.print("Set MIN. Alarm:"); 1337 lcd.setCursor(0,1); 1338 lcd.print(alarmMinutes,DEC); 1339 delay(200); 1340 } 1341 delay(200); 1342} 1343void printAllOn(){ 1344 lcd.setCursor(0,3); 1345 lcd.print("Alarm: "); 1346 1347 1348 1349 if (alarmHours <= 9) 1350 { 1351 lcd.print("0"); 1352 } 1353 lcd.print(alarmHours, DEC); 1354 1355 lcd.print(":"); 1356 if (alarmMinutes <= 9) 1357 { 1358 lcd.print("0"); 1359 } 1360 lcd.print(alarmMinutes, DEC); 1361 1362} 1363void printAllOff() { 1364 lcd.setCursor(0, 3); 1365 lcd.print("Alarm: Off "); 1366} 1367void Alarm(){ 1368 if(digitalRead(P4)== LOW) 1369 { 1370 setAll=setAll+1; 1371 } 1372 if (setAll==0) 1373 { 1374 printAllOff(); 1375 noTone (dc water pump); 1376 digitalWrite(LED,LOW); 1377 } 1378 if (setAll==1) 1379 { 1380 1381 printAllOn(); 1382 1383 DateTime now = RTC.now(); 1384 if ( now.hour() == alarmHours && now.minute() == alarmMinutes ) 1385 { 1386 lcd.noBacklight(); 1387 DateTime now = RTC.now(); 1388 digitalWrite(LED,HIGH); 1389 tone(dc water pump,880); //play the note "A5" (LA5) 1390 delay (300); 1391 tone(dc water pump,698); //play the note "F6" (FA5) 1392 lcd.backlight(); 1393 } 1394 else{ 1395 noTone (dc water pump); 1396 digitalWrite(LED,LOW); 1397 } 1398 1399 } 1400 if (setAll==2) 1401 { 1402 setAll=0; 1403 } 1404 delay(200); 1405}
untitled
arduino
1#include <Wire.h> 2#include <RTClib.h> 3#include <LiquidCrystal_I2C.h> 4 5//************************************// 6LiquidCrystal_I2C lcd(0x27,20,4); // Display I2C 20 x 4 7RTC_DS1307 RTC; 8 9//************Button*****************// 10int P1=6; // Button SET MENU' 11int P2=7; // Button + 12int P3=8; // Button - 13int P4=9; // SWITCH Alarm 14 15//**************Alarm***************// 16#define LED 13 17#define dc water pump 10 18 19//************Variables**************// 20int hourupg; 21int minupg; 22int yearupg; 23int monthupg; 24int dayupg; 25int menu =0; 26int setAll =0; 27 28uint8_t alarmHours = 0, alarmMinutes = 0; // Holds the current alarm time 29 30 31void setup() 32{ 33 34 lcd.begin(); 35 lcd.backlight(); 36 lcd.clear(); 37 38 pinMode(P1,INPUT_PULLUP); // https://www.arduino.cc/en/Tutorial/InputPullupSerial 39 pinMode(P2,INPUT_PULLUP); 40 pinMode(P3,INPUT_PULLUP); 41 pinMode(P4,INPUT_PULLUP); 42 pinMode(LED,OUTPUT); 43 pinMode(dc water pump, OUTPUT); // Set dc water pump as an output 44 printAllOff(); 45 Serial.begin(9600); 46 Wire.begin(); 47 RTC.begin(); 48 49 if (! RTC.isrunning()) { 50 Serial.println("RTC is NOT running!"); 51 // Set the date and time at compile time 52 RTC.adjust(DateTime(__DATE__, __TIME__)); 53 } 54 // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time 55 // The default display shows the date and time 56 int menu=0; 57} 58 59void loop() 60{ 61 62// check if you press the SET button and increase the menu index 63 if(digitalRead(P1)== LOW) 64 { 65 menu=menu+1; 66 } 67 if((digitalRead(P2)== LOW)&&(digitalRead(P3)== LOW)) 68 { 69 70 DisplaySetHourAll(); 71 DisplaySetMinuteAll(); 72 lcd.clear(); 73 lcd.setCursor(5,0); 74 lcd.print("ALARM"); 75 lcd.setCursor(5,1); 76 lcd.print(alarmHours, DEC); 77 lcd.print(":"); 78 lcd.print(alarmMinutes, DEC); 79 delay(1000); 80 lcd.clear(); 81 } 82// in which subroutine should we go? 83 if (menu==0) 84 { 85 DisplayDateTime(); // void DisplayDateTime 86 Alarm(); // Alarm control 87 } 88 if (menu==1) 89 { 90 DisplaySetHour(); 91 } 92 if (menu==2) 93 { 94 DisplaySetMinute(); 95 } 96 if (menu==3) 97 { 98 DisplaySetYear(); 99 } 100 if (menu==4) 101 { 102 DisplaySetMonth(); 103 } 104 if (menu==5) 105 { 106 DisplaySetDay(); 107 } 108 if (menu==6) 109 { 110 StoreAgg(); 111 delay(500); 112 menu=0; 113 } 114 delay(100); 115} 116 117void DisplayDateTime () 118{ 119// We show the current date and time 120 DateTime now = RTC.now(); 121 122 lcd.setCursor(0, 2); 123 lcd.print("Hour : "); 124 125 if (now.hour()<=9) 126 { 127 lcd.print("0"); 128 } 129 lcd.print(now.hour(), DEC); 130 hourupg=now.hour(); 131 lcd.print(":"); 132 if (now.minute()<=9) 133 { 134 lcd.print("0"); 135 } 136 lcd.print(now.minute(), DEC); 137 minupg=now.minute(); 138 lcd.print(":"); 139 if (now.second()<=9) 140 { 141 lcd.print("0"); 142 } 143 lcd.print(now.second(), DEC); 144 145 146 lcd.setCursor(0, 1); 147 lcd.print("Date : "); 148 if (now.day()<=9) 149 { 150 lcd.print("0"); 151 } 152 lcd.print(now.day(), DEC); 153 dayupg=now.day(); 154 lcd.print("/"); 155 if (now.month()<=9) 156 { 157 lcd.print("0"); 158 } 159 lcd.print(now.month(), DEC); 160 monthupg=now.month(); 161 lcd.print("/"); 162 lcd.print(now.year(), DEC); 163 yearupg=now.year(); 164 165 char DOW[][10]={"Sunday ","Monday ","Tuesday ","Wednesday","Thursday ","Friday ","Saturday "}; 166 lcd.setCursor(0, 0); 167 lcd.print("Day : "); 168 lcd.print(DOW[now.dayOfTheWeek()]); // if it appears error in the code, enter the code given below 169 //lcd.print(DOW[now.dayOfWeek()]); 170} 171 172void DisplaySetHour() 173{ 174// time setting 175 lcd.clear(); 176 DateTime now = RTC.now(); 177 if(digitalRead(P2)==LOW) 178 { 179 if(hourupg==23) 180 { 181 hourupg=0; 182 } 183 else 184 { 185 hourupg=hourupg+1; 186 } 187 } 188 if(digitalRead(P3)==LOW) 189 { 190 if(hourupg==0) 191 { 192 hourupg=23; 193 } 194 else 195 { 196 hourupg=hourupg-1; 197 } 198 } 199 lcd.setCursor(0,0); 200 lcd.print("Set time:"); 201 lcd.setCursor(0,1); 202 lcd.print(hourupg,DEC); 203 delay(200); 204} 205 206void DisplaySetMinute() 207{ 208// Setting the minutes 209 lcd.clear(); 210 if(digitalRead(P2)==LOW) 211 { 212 if (minupg==59) 213 { 214 minupg=0; 215 } 216 else 217 { 218 minupg=minupg+1; 219 } 220 } 221 if(digitalRead(P3)==LOW) 222 { 223 if (minupg==0) 224 { 225 minupg=59; 226 } 227 else 228 { 229 minupg=minupg-1; 230 } 231 } 232 lcd.setCursor(0,0); 233 lcd.print("Set Minutes:"); 234 lcd.setCursor(0,1); 235 lcd.print(minupg,DEC); 236 delay(200); 237} 238 239void DisplaySetYear() 240{ 241// setting the year 242 lcd.clear(); 243 if(digitalRead(P2)==LOW) 244 { 245 yearupg=yearupg+1; 246 } 247 if(digitalRead(P3)==LOW) 248 { 249 yearupg=yearupg-1; 250 } 251 lcd.setCursor(0,0); 252 lcd.print("Set Year:"); 253 lcd.setCursor(0,1); 254 lcd.print(yearupg,DEC); 255 delay(200); 256} 257 258void DisplaySetMonth() 259{ 260// Setting the month 261 lcd.clear(); 262 if(digitalRead(P2)==LOW) 263 { 264 if (monthupg==12) 265 { 266 monthupg=1; 267 } 268 else 269 { 270 monthupg=monthupg+1; 271 } 272 } 273 if(digitalRead(P3)==LOW) 274 { 275 if (monthupg==1) 276 { 277 monthupg=12; 278 } 279 else 280 { 281 monthupg=monthupg-1; 282 } 283 } 284 lcd.setCursor(0,0); 285 lcd.print("Set Month:"); 286 lcd.setCursor(0,1); 287 lcd.print(monthupg,DEC); 288 delay(200); 289} 290 291void DisplaySetDay() 292{ 293// Setting the day 294 lcd.clear(); 295 if(digitalRead(P2)==LOW) 296 { 297 if (dayupg==31) 298 { 299 dayupg=1; 300 } 301 else 302 { 303 dayupg=dayupg+1; 304 } 305 } 306 if(digitalRead(P3)==LOW) 307 { 308 if (dayupg==1) 309 { 310 dayupg=31; 311 } 312 else 313 { 314 dayupg=dayupg-1; 315 } 316 } 317 lcd.setCursor(0,0); 318 lcd.print("Set Day:"); 319 lcd.setCursor(0,1); 320 lcd.print(dayupg,DEC); 321 delay(200); 322} 323 324void StoreAgg() 325{ 326// Variable saving 327 lcd.clear(); 328 lcd.setCursor(0,0); 329 lcd.print("SAVING IN"); 330 lcd.setCursor(0,1); 331 lcd.print("PROGRESS"); 332 RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0)); 333 delay(200); 334} 335void DisplaySetHourAll()// Setting the alarm minutes 336{ 337 while(digitalRead(P1)==HIGH){ 338 339 lcd.clear(); 340 341 if(digitalRead(P2)==LOW) 342 { 343 if(alarmHours==23) 344 { 345 alarmHours=0; 346 } 347 else 348 { 349 alarmHours=alarmHours+1; 350 } 351 } 352 if(digitalRead(P3)==LOW) 353 { 354 if(alarmHours==0) 355 { 356 alarmHours=23; 357 } 358 else 359 { 360 alarmHours=alarmHours-1; 361 } 362 } 363 lcd.setCursor(0,0); 364 lcd.print("Set HOUR Alarm:"); 365 lcd.setCursor(0,1); 366 lcd.print(alarmHours,DEC); 367 delay(200); 368 } 369 delay(200); 370} 371 372void DisplaySetMinuteAll()// Setting the alarm minutes 373 { 374 while(digitalRead(P1)==HIGH){ 375 376 lcd.clear(); 377 if(digitalRead(P2)==LOW) 378 { 379 if (alarmMinutes==59) 380 { 381 alarmMinutes=0; 382 } 383 else 384 { 385 alarmMinutes=alarmMinutes+1; 386 } 387 } 388 if(digitalRead(P3)==LOW) 389 { 390 if (alarmMinutes==0) 391 { 392 alarmMinutes=59; 393 } 394 else 395 { 396 alarmMinutes=alarmMinutes-1; 397 } 398 } 399 lcd.setCursor(0,0); 400 lcd.print("Set MIN. Alarm:"); 401 lcd.setCursor(0,1); 402 lcd.print(alarmMinutes,DEC); 403 delay(200); 404 } 405 delay(200); 406} 407void printAllOn(){ 408 lcd.setCursor(0,3); 409 lcd.print("Alarm: "); 410 411 412 413 if (alarmHours <= 9) 414 { 415 lcd.print("0"); 416 } 417 lcd.print(alarmHours, DEC); 418 419 lcd.print(":"); 420 if (alarmMinutes <= 9) 421 { 422 lcd.print("0"); 423 } 424 lcd.print(alarmMinutes, DEC); 425 426} 427void printAllOff() { 428 lcd.setCursor(0, 3); 429 lcd.print("Alarm: Off "); 430} 431void Alarm(){ 432 if(digitalRead(P4)== LOW) 433 { 434 setAll=setAll+1; 435 } 436 if (setAll==0) 437 { 438 printAllOff(); 439 noTone (dc water pump); 440 digitalWrite(LED,LOW); 441 } 442 if (setAll==1) 443 { 444 445 printAllOn(); 446 447 DateTime now = RTC.now(); 448 if ( now.hour() == alarmHours && now.minute() == alarmMinutes ) 449 { 450 lcd.noBacklight(); 451 DateTime now = RTC.now(); 452 digitalWrite(LED,HIGH); 453 tone(dc water pump,880); //play the note "A5" (LA5) 454 delay (300); 455 tone(dc water pump,698); //play the note "F6" (FA5) 456 lcd.backlight(); 457 } 458 else{ 459 noTone (dc water pump); 460 digitalWrite(LED,LOW); 461 } 462 463 } 464 if (setAll==2) 465 { 466 setAll=0; 467 } 468 delay(200); 469}#include <Wire.h> 470#include <RTClib.h> 471#include <LiquidCrystal_I2C.h> 472 473//************************************// 474LiquidCrystal_I2C lcd(0x27,20,4); // Display I2C 20 x 4 475RTC_DS1307 RTC; 476 477//************Button*****************// 478int P1=6; // Button SET MENU' 479int P2=7; // Button + 480int P3=8; // Button - 481int P4=9; // SWITCH Alarm 482 483//**************Alarm***************// 484#define LED 13 485#define dc water pump 10 486 487//************Variables**************// 488int hourupg; 489int minupg; 490int yearupg; 491int monthupg; 492int dayupg; 493int menu =0; 494int setAll =0; 495 496uint8_t alarmHours = 0, alarmMinutes = 0; // Holds the current alarm time 497 498 499void setup() 500{ 501 502 lcd.begin(); 503 lcd.backlight(); 504 lcd.clear(); 505 506 pinMode(P1,INPUT_PULLUP); // https://www.arduino.cc/en/Tutorial/InputPullupSerial 507 pinMode(P2,INPUT_PULLUP); 508 pinMode(P3,INPUT_PULLUP); 509 pinMode(P4,INPUT_PULLUP); 510 pinMode(LED,OUTPUT); 511 pinMode(dc water pump, OUTPUT); // Set dc water pump as an output 512 printAllOff(); 513 Serial.begin(9600); 514 Wire.begin(); 515 RTC.begin(); 516 517 if (! RTC.isrunning()) { 518 Serial.println("RTC is NOT running!"); 519 // Set the date and time at compile time 520 RTC.adjust(DateTime(__DATE__, __TIME__)); 521 } 522 // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time 523 // The default display shows the date and time 524 int menu=0; 525} 526 527void loop() 528{ 529 530// check if you press the SET button and increase the menu index 531 if(digitalRead(P1)== LOW) 532 { 533 menu=menu+1; 534 } 535 if((digitalRead(P2)== LOW)&&(digitalRead(P3)== LOW)) 536 { 537 538 DisplaySetHourAll(); 539 DisplaySetMinuteAll(); 540 lcd.clear(); 541 lcd.setCursor(5,0); 542 lcd.print("ALARM"); 543 lcd.setCursor(5,1); 544 lcd.print(alarmHours, DEC); 545 lcd.print(":"); 546 lcd.print(alarmMinutes, DEC); 547 delay(1000); 548 lcd.clear(); 549 } 550// in which subroutine should we go? 551 if (menu==0) 552 { 553 DisplayDateTime(); // void DisplayDateTime 554 Alarm(); // Alarm control 555 } 556 if (menu==1) 557 { 558 DisplaySetHour(); 559 } 560 if (menu==2) 561 { 562 DisplaySetMinute(); 563 } 564 if (menu==3) 565 { 566 DisplaySetYear(); 567 } 568 if (menu==4) 569 { 570 DisplaySetMonth(); 571 } 572 if (menu==5) 573 { 574 DisplaySetDay(); 575 } 576 if (menu==6) 577 { 578 StoreAgg(); 579 delay(500); 580 menu=0; 581 } 582 delay(100); 583} 584 585void DisplayDateTime () 586{ 587// We show the current date and time 588 DateTime now = RTC.now(); 589 590 lcd.setCursor(0, 2); 591 lcd.print("Hour : "); 592 593 if (now.hour()<=9) 594 { 595 lcd.print("0"); 596 } 597 lcd.print(now.hour(), DEC); 598 hourupg=now.hour(); 599 lcd.print(":"); 600 if (now.minute()<=9) 601 { 602 lcd.print("0"); 603 } 604 lcd.print(now.minute(), DEC); 605 minupg=now.minute(); 606 lcd.print(":"); 607 if (now.second()<=9) 608 { 609 lcd.print("0"); 610 } 611 lcd.print(now.second(), DEC); 612 613 614 lcd.setCursor(0, 1); 615 lcd.print("Date : "); 616 if (now.day()<=9) 617 { 618 lcd.print("0"); 619 } 620 lcd.print(now.day(), DEC); 621 dayupg=now.day(); 622 lcd.print("/"); 623 if (now.month()<=9) 624 { 625 lcd.print("0"); 626 } 627 lcd.print(now.month(), DEC); 628 monthupg=now.month(); 629 lcd.print("/"); 630 lcd.print(now.year(), DEC); 631 yearupg=now.year(); 632 633 char DOW[][10]={"Sunday ","Monday ","Tuesday ","Wednesday","Thursday ","Friday ","Saturday "}; 634 lcd.setCursor(0, 0); 635 lcd.print("Day : "); 636 lcd.print(DOW[now.dayOfTheWeek()]); // if it appears error in the code, enter the code given below 637 //lcd.print(DOW[now.dayOfWeek()]); 638} 639 640void DisplaySetHour() 641{ 642// time setting 643 lcd.clear(); 644 DateTime now = RTC.now(); 645 if(digitalRead(P2)==LOW) 646 { 647 if(hourupg==23) 648 { 649 hourupg=0; 650 } 651 else 652 { 653 hourupg=hourupg+1; 654 } 655 } 656 if(digitalRead(P3)==LOW) 657 { 658 if(hourupg==0) 659 { 660 hourupg=23; 661 } 662 else 663 { 664 hourupg=hourupg-1; 665 } 666 } 667 lcd.setCursor(0,0); 668 lcd.print("Set time:"); 669 lcd.setCursor(0,1); 670 lcd.print(hourupg,DEC); 671 delay(200); 672} 673 674void DisplaySetMinute() 675{ 676// Setting the minutes 677 lcd.clear(); 678 if(digitalRead(P2)==LOW) 679 { 680 if (minupg==59) 681 { 682 minupg=0; 683 } 684 else 685 { 686 minupg=minupg+1; 687 } 688 } 689 if(digitalRead(P3)==LOW) 690 { 691 if (minupg==0) 692 { 693 minupg=59; 694 } 695 else 696 { 697 minupg=minupg-1; 698 } 699 } 700 lcd.setCursor(0,0); 701 lcd.print("Set Minutes:"); 702 lcd.setCursor(0,1); 703 lcd.print(minupg,DEC); 704 delay(200); 705} 706 707void DisplaySetYear() 708{ 709// setting the year 710 lcd.clear(); 711 if(digitalRead(P2)==LOW) 712 { 713 yearupg=yearupg+1; 714 } 715 if(digitalRead(P3)==LOW) 716 { 717 yearupg=yearupg-1; 718 } 719 lcd.setCursor(0,0); 720 lcd.print("Set Year:"); 721 lcd.setCursor(0,1); 722 lcd.print(yearupg,DEC); 723 delay(200); 724} 725 726void DisplaySetMonth() 727{ 728// Setting the month 729 lcd.clear(); 730 if(digitalRead(P2)==LOW) 731 { 732 if (monthupg==12) 733 { 734 monthupg=1; 735 } 736 else 737 { 738 monthupg=monthupg+1; 739 } 740 } 741 if(digitalRead(P3)==LOW) 742 { 743 if (monthupg==1) 744 { 745 monthupg=12; 746 } 747 else 748 { 749 monthupg=monthupg-1; 750 } 751 } 752 lcd.setCursor(0,0); 753 lcd.print("Set Month:"); 754 lcd.setCursor(0,1); 755 lcd.print(monthupg,DEC); 756 delay(200); 757} 758 759void DisplaySetDay() 760{ 761// Setting the day 762 lcd.clear(); 763 if(digitalRead(P2)==LOW) 764 { 765 if (dayupg==31) 766 { 767 dayupg=1; 768 } 769 else 770 { 771 dayupg=dayupg+1; 772 } 773 } 774 if(digitalRead(P3)==LOW) 775 { 776 if (dayupg==1) 777 { 778 dayupg=31; 779 } 780 else 781 { 782 dayupg=dayupg-1; 783 } 784 } 785 lcd.setCursor(0,0); 786 lcd.print("Set Day:"); 787 lcd.setCursor(0,1); 788 lcd.print(dayupg,DEC); 789 delay(200); 790} 791 792void StoreAgg() 793{ 794// Variable saving 795 lcd.clear(); 796 lcd.setCursor(0,0); 797 lcd.print("SAVING IN"); 798 lcd.setCursor(0,1); 799 lcd.print("PROGRESS"); 800 RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0)); 801 delay(200); 802} 803void DisplaySetHourAll()// Setting the alarm minutes 804{ 805 while(digitalRead(P1)==HIGH){ 806 807 lcd.clear(); 808 809 if(digitalRead(P2)==LOW) 810 { 811 if(alarmHours==23) 812 { 813 alarmHours=0; 814 } 815 else 816 { 817 alarmHours=alarmHours+1; 818 } 819 } 820 if(digitalRead(P3)==LOW) 821 { 822 if(alarmHours==0) 823 { 824 alarmHours=23; 825 } 826 else 827 { 828 alarmHours=alarmHours-1; 829 } 830 } 831 lcd.setCursor(0,0); 832 lcd.print("Set HOUR Alarm:"); 833 lcd.setCursor(0,1); 834 lcd.print(alarmHours,DEC); 835 delay(200); 836 } 837 delay(200); 838} 839 840void DisplaySetMinuteAll()// Setting the alarm minutes 841 { 842 while(digitalRead(P1)==HIGH){ 843 844 lcd.clear(); 845 if(digitalRead(P2)==LOW) 846 { 847 if (alarmMinutes==59) 848 { 849 alarmMinutes=0; 850 } 851 else 852 { 853 alarmMinutes=alarmMinutes+1; 854 } 855 } 856 if(digitalRead(P3)==LOW) 857 { 858 if (alarmMinutes==0) 859 { 860 alarmMinutes=59; 861 } 862 else 863 { 864 alarmMinutes=alarmMinutes-1; 865 } 866 } 867 lcd.setCursor(0,0); 868 lcd.print("Set MIN. Alarm:"); 869 lcd.setCursor(0,1); 870 lcd.print(alarmMinutes,DEC); 871 delay(200); 872 } 873 delay(200); 874} 875void printAllOn(){ 876 lcd.setCursor(0,3); 877 lcd.print("Alarm: "); 878 879 880 881 if (alarmHours <= 9) 882 { 883 lcd.print("0"); 884 } 885 lcd.print(alarmHours, DEC); 886 887 lcd.print(":"); 888 if (alarmMinutes <= 9) 889 { 890 lcd.print("0"); 891 } 892 lcd.print(alarmMinutes, DEC); 893 894} 895void printAllOff() { 896 lcd.setCursor(0, 3); 897 lcd.print("Alarm: Off "); 898} 899void Alarm(){ 900 if(digitalRead(P4)== LOW) 901 { 902 setAll=setAll+1; 903 } 904 if (setAll==0) 905 { 906 printAllOff(); 907 noTone (dc water pump); 908 digitalWrite(LED,LOW); 909 } 910 if (setAll==1) 911 { 912 913 printAllOn(); 914 915 DateTime now = RTC.now(); 916 if ( now.hour() == alarmHours && now.minute() == alarmMinutes ) 917 { 918 lcd.noBacklight(); 919 DateTime now = RTC.now(); 920 digitalWrite(LED,HIGH); 921 tone(dc water pump,880); //play the note "A5" (LA5) 922 delay (300); 923 tone(dc water pump,698); //play the note "F6" (FA5) 924 lcd.backlight(); 925 } 926 else{ 927 noTone (dc water pump); 928 digitalWrite(LED,LOW); 929 } 930 931 } 932 if (setAll==2) 933 { 934 setAll=0; 935 } 936 delay(200); 937}#include <Wire.h> 938#include <RTClib.h> 939#include <LiquidCrystal_I2C.h> 940 941//************************************// 942LiquidCrystal_I2C lcd(0x27,20,4); // Display I2C 20 x 4 943RTC_DS1307 RTC; 944 945//************Button*****************// 946int P1=6; // Button SET MENU' 947int P2=7; // Button + 948int P3=8; // Button - 949int P4=9; // SWITCH Alarm 950 951//**************Alarm***************// 952#define LED 13 953#define dc water pump 10 954 955//************Variables**************// 956int hourupg; 957int minupg; 958int yearupg; 959int monthupg; 960int dayupg; 961int menu =0; 962int setAll =0; 963 964uint8_t alarmHours = 0, alarmMinutes = 0; // Holds the current alarm time 965 966 967void setup() 968{ 969 970 lcd.begin(); 971 lcd.backlight(); 972 lcd.clear(); 973 974 pinMode(P1,INPUT_PULLUP); // https://www.arduino.cc/en/Tutorial/InputPullupSerial 975 pinMode(P2,INPUT_PULLUP); 976 pinMode(P3,INPUT_PULLUP); 977 pinMode(P4,INPUT_PULLUP); 978 pinMode(LED,OUTPUT); 979 pinMode(dc water pump, OUTPUT); // Set dc water pump as an output 980 printAllOff(); 981 Serial.begin(9600); 982 Wire.begin(); 983 RTC.begin(); 984 985 if (! RTC.isrunning()) { 986 Serial.println("RTC is NOT running!"); 987 // Set the date and time at compile time 988 RTC.adjust(DateTime(__DATE__, __TIME__)); 989 } 990 // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time 991 // The default display shows the date and time 992 int menu=0; 993} 994 995void loop() 996{ 997 998// check if you press the SET button and increase the menu index 999 if(digitalRead(P1)== LOW) 1000 { 1001 menu=menu+1; 1002 } 1003 if((digitalRead(P2)== LOW)&&(digitalRead(P3)== LOW)) 1004 { 1005 1006 DisplaySetHourAll(); 1007 DisplaySetMinuteAll(); 1008 lcd.clear(); 1009 lcd.setCursor(5,0); 1010 lcd.print("ALARM"); 1011 lcd.setCursor(5,1); 1012 lcd.print(alarmHours, DEC); 1013 lcd.print(":"); 1014 lcd.print(alarmMinutes, DEC); 1015 delay(1000); 1016 lcd.clear(); 1017 } 1018// in which subroutine should we go? 1019 if (menu==0) 1020 { 1021 DisplayDateTime(); // void DisplayDateTime 1022 Alarm(); // Alarm control 1023 } 1024 if (menu==1) 1025 { 1026 DisplaySetHour(); 1027 } 1028 if (menu==2) 1029 { 1030 DisplaySetMinute(); 1031 } 1032 if (menu==3) 1033 { 1034 DisplaySetYear(); 1035 } 1036 if (menu==4) 1037 { 1038 DisplaySetMonth(); 1039 } 1040 if (menu==5) 1041 { 1042 DisplaySetDay(); 1043 } 1044 if (menu==6) 1045 { 1046 StoreAgg(); 1047 delay(500); 1048 menu=0; 1049 } 1050 delay(100); 1051} 1052 1053void DisplayDateTime () 1054{ 1055// We show the current date and time 1056 DateTime now = RTC.now(); 1057 1058 lcd.setCursor(0, 2); 1059 lcd.print("Hour : "); 1060 1061 if (now.hour()<=9) 1062 { 1063 lcd.print("0"); 1064 } 1065 lcd.print(now.hour(), DEC); 1066 hourupg=now.hour(); 1067 lcd.print(":"); 1068 if (now.minute()<=9) 1069 { 1070 lcd.print("0"); 1071 } 1072 lcd.print(now.minute(), DEC); 1073 minupg=now.minute(); 1074 lcd.print(":"); 1075 if (now.second()<=9) 1076 { 1077 lcd.print("0"); 1078 } 1079 lcd.print(now.second(), DEC); 1080 1081 1082 lcd.setCursor(0, 1); 1083 lcd.print("Date : "); 1084 if (now.day()<=9) 1085 { 1086 lcd.print("0"); 1087 } 1088 lcd.print(now.day(), DEC); 1089 dayupg=now.day(); 1090 lcd.print("/"); 1091 if (now.month()<=9) 1092 { 1093 lcd.print("0"); 1094 } 1095 lcd.print(now.month(), DEC); 1096 monthupg=now.month(); 1097 lcd.print("/"); 1098 lcd.print(now.year(), DEC); 1099 yearupg=now.year(); 1100 1101 char DOW[][10]={"Sunday ","Monday ","Tuesday ","Wednesday","Thursday ","Friday ","Saturday "}; 1102 lcd.setCursor(0, 0); 1103 lcd.print("Day : "); 1104 lcd.print(DOW[now.dayOfTheWeek()]); // if it appears error in the code, enter the code given below 1105 //lcd.print(DOW[now.dayOfWeek()]); 1106} 1107 1108void DisplaySetHour() 1109{ 1110// time setting 1111 lcd.clear(); 1112 DateTime now = RTC.now(); 1113 if(digitalRead(P2)==LOW) 1114 { 1115 if(hourupg==23) 1116 { 1117 hourupg=0; 1118 } 1119 else 1120 { 1121 hourupg=hourupg+1; 1122 } 1123 } 1124 if(digitalRead(P3)==LOW) 1125 { 1126 if(hourupg==0) 1127 { 1128 hourupg=23; 1129 } 1130 else 1131 { 1132 hourupg=hourupg-1; 1133 } 1134 } 1135 lcd.setCursor(0,0); 1136 lcd.print("Set time:"); 1137 lcd.setCursor(0,1); 1138 lcd.print(hourupg,DEC); 1139 delay(200); 1140} 1141 1142void DisplaySetMinute() 1143{ 1144// Setting the minutes 1145 lcd.clear(); 1146 if(digitalRead(P2)==LOW) 1147 { 1148 if (minupg==59) 1149 { 1150 minupg=0; 1151 } 1152 else 1153 { 1154 minupg=minupg+1; 1155 } 1156 } 1157 if(digitalRead(P3)==LOW) 1158 { 1159 if (minupg==0) 1160 { 1161 minupg=59; 1162 } 1163 else 1164 { 1165 minupg=minupg-1; 1166 } 1167 } 1168 lcd.setCursor(0,0); 1169 lcd.print("Set Minutes:"); 1170 lcd.setCursor(0,1); 1171 lcd.print(minupg,DEC); 1172 delay(200); 1173} 1174 1175void DisplaySetYear() 1176{ 1177// setting the year 1178 lcd.clear(); 1179 if(digitalRead(P2)==LOW) 1180 { 1181 yearupg=yearupg+1; 1182 } 1183 if(digitalRead(P3)==LOW) 1184 { 1185 yearupg=yearupg-1; 1186 } 1187 lcd.setCursor(0,0); 1188 lcd.print("Set Year:"); 1189 lcd.setCursor(0,1); 1190 lcd.print(yearupg,DEC); 1191 delay(200); 1192} 1193 1194void DisplaySetMonth() 1195{ 1196// Setting the month 1197 lcd.clear(); 1198 if(digitalRead(P2)==LOW) 1199 { 1200 if (monthupg==12) 1201 { 1202 monthupg=1; 1203 } 1204 else 1205 { 1206 monthupg=monthupg+1; 1207 } 1208 } 1209 if(digitalRead(P3)==LOW) 1210 { 1211 if (monthupg==1) 1212 { 1213 monthupg=12; 1214 } 1215 else 1216 { 1217 monthupg=monthupg-1; 1218 } 1219 } 1220 lcd.setCursor(0,0); 1221 lcd.print("Set Month:"); 1222 lcd.setCursor(0,1); 1223 lcd.print(monthupg,DEC); 1224 delay(200); 1225} 1226 1227void DisplaySetDay() 1228{ 1229// Setting the day 1230 lcd.clear(); 1231 if(digitalRead(P2)==LOW) 1232 { 1233 if (dayupg==31) 1234 { 1235 dayupg=1; 1236 } 1237 else 1238 { 1239 dayupg=dayupg+1; 1240 } 1241 } 1242 if(digitalRead(P3)==LOW) 1243 { 1244 if (dayupg==1) 1245 { 1246 dayupg=31; 1247 } 1248 else 1249 { 1250 dayupg=dayupg-1; 1251 } 1252 } 1253 lcd.setCursor(0,0); 1254 lcd.print("Set Day:"); 1255 lcd.setCursor(0,1); 1256 lcd.print(dayupg,DEC); 1257 delay(200); 1258} 1259 1260void StoreAgg() 1261{ 1262// Variable saving 1263 lcd.clear(); 1264 lcd.setCursor(0,0); 1265 lcd.print("SAVING IN"); 1266 lcd.setCursor(0,1); 1267 lcd.print("PROGRESS"); 1268 RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0)); 1269 delay(200); 1270} 1271void DisplaySetHourAll()// Setting the alarm minutes 1272{ 1273 while(digitalRead(P1)==HIGH){ 1274 1275 lcd.clear(); 1276 1277 if(digitalRead(P2)==LOW) 1278 { 1279 if(alarmHours==23) 1280 { 1281 alarmHours=0; 1282 } 1283 else 1284 { 1285 alarmHours=alarmHours+1; 1286 } 1287 } 1288 if(digitalRead(P3)==LOW) 1289 { 1290 if(alarmHours==0) 1291 { 1292 alarmHours=23; 1293 } 1294 else 1295 { 1296 alarmHours=alarmHours-1; 1297 } 1298 } 1299 lcd.setCursor(0,0); 1300 lcd.print("Set HOUR Alarm:"); 1301 lcd.setCursor(0,1); 1302 lcd.print(alarmHours,DEC); 1303 delay(200); 1304 } 1305 delay(200); 1306} 1307 1308void DisplaySetMinuteAll()// Setting the alarm minutes 1309 { 1310 while(digitalRead(P1)==HIGH){ 1311 1312 lcd.clear(); 1313 if(digitalRead(P2)==LOW) 1314 { 1315 if (alarmMinutes==59) 1316 { 1317 alarmMinutes=0; 1318 } 1319 else 1320 { 1321 alarmMinutes=alarmMinutes+1; 1322 } 1323 } 1324 if(digitalRead(P3)==LOW) 1325 { 1326 if (alarmMinutes==0) 1327 { 1328 alarmMinutes=59; 1329 } 1330 else 1331 { 1332 alarmMinutes=alarmMinutes-1; 1333 } 1334 } 1335 lcd.setCursor(0,0); 1336 lcd.print("Set MIN. Alarm:"); 1337 lcd.setCursor(0,1); 1338 lcd.print(alarmMinutes,DEC); 1339 delay(200); 1340 } 1341 delay(200); 1342} 1343void printAllOn(){ 1344 lcd.setCursor(0,3); 1345 lcd.print("Alarm: "); 1346 1347 1348 1349 if (alarmHours <= 9) 1350 { 1351 lcd.print("0"); 1352 } 1353 lcd.print(alarmHours, DEC); 1354 1355 lcd.print(":"); 1356 if (alarmMinutes <= 9) 1357 { 1358 lcd.print("0"); 1359 } 1360 lcd.print(alarmMinutes, DEC); 1361 1362} 1363void printAllOff() { 1364 lcd.setCursor(0, 3); 1365 lcd.print("Alarm: Off "); 1366} 1367void Alarm(){ 1368 if(digitalRead(P4)== LOW) 1369 { 1370 setAll=setAll+1; 1371 } 1372 if (setAll==0) 1373 { 1374 printAllOff(); 1375 noTone (dc water pump); 1376 digitalWrite(LED,LOW); 1377 } 1378 if (setAll==1) 1379 { 1380 1381 printAllOn(); 1382 1383 DateTime now = RTC.now(); 1384 if ( now.hour() == alarmHours && now.minute() == alarmMinutes ) 1385 { 1386 lcd.noBacklight(); 1387 DateTime now = RTC.now(); 1388 digitalWrite(LED,HIGH); 1389 tone(dc water pump,880); //play the note "A5" (LA5) 1390 delay (300); 1391 tone(dc water pump,698); //play the note "F6" (FA5) 1392 lcd.backlight(); 1393 } 1394 else{ 1395 noTone (dc water pump); 1396 digitalWrite(LED,LOW); 1397 } 1398 1399 } 1400 if (setAll==2) 1401 { 1402 setAll=0; 1403 } 1404 delay(200); 1405}
untitled
arduino
1#include <Wire.h> 2#include <RTClib.h> 3#include <LiquidCrystal_I2C.h> 4 5//************************************// 6LiquidCrystal_I2C 7 lcd(0x27,20,4); // Display I2C 20 x 4 8RTC_DS1307 RTC; 9 10//************Button*****************// 11int 12 P1=6; // Button SET MENU' 13int P2=7; // Button + 14int P3=8; // Button - 15int 16 P4=9; // SWITCH Alarm 17 18//**************Alarm***************// 19#define LED 20 13 21#define dc water pump 10 22 23//************Variables**************// 24int 25 hourupg; 26int minupg; 27int yearupg; 28int monthupg; 29int dayupg; 30int menu 31 =0; 32int setAll =0; 33 34uint8_t alarmHours = 0, alarmMinutes = 0; // Holds 35 the current alarm time 36 37 38void setup() 39{ 40 41 lcd.begin(); 42 lcd.backlight(); 43 44 lcd.clear(); 45 46 pinMode(P1,INPUT_PULLUP); // https://www.arduino.cc/en/Tutorial/InputPullupSerial 47 48 pinMode(P2,INPUT_PULLUP); 49 pinMode(P3,INPUT_PULLUP); 50 pinMode(P4,INPUT_PULLUP); 51 52 pinMode(LED,OUTPUT); 53 pinMode(dc water pump, OUTPUT); // Set dc water pump 54 as an output 55 printAllOff(); 56 Serial.begin(9600); 57 Wire.begin(); 58 59 RTC.begin(); 60 61 if (! RTC.isrunning()) { 62 Serial.println("RTC is 63 NOT running!"); 64 // Set the date and time at compile time 65 RTC.adjust(DateTime(__DATE__, 66 __TIME__)); 67 } 68 // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing 69 "//" to adjust the time 70 // The default display shows the date and time 71 72 int menu=0; 73} 74 75void loop() 76{ 77 78// check if you press the SET 79 button and increase the menu index 80 if(digitalRead(P1)== LOW) 81 { 82 menu=menu+1; 83 84 } 85 if((digitalRead(P2)== LOW)&&(digitalRead(P3)== LOW)) 86 87 { 88 89 DisplaySetHourAll(); 90 DisplaySetMinuteAll(); 91 lcd.clear(); 92 93 lcd.setCursor(5,0); 94 lcd.print("ALARM"); 95 lcd.setCursor(5,1); 96 97 lcd.print(alarmHours, DEC); 98 lcd.print(":"); 99 lcd.print(alarmMinutes, 100 DEC); 101 delay(1000); 102 lcd.clear(); 103 } 104// in which subroutine should 105 we go? 106 if (menu==0) 107 { 108 DisplayDateTime(); // void DisplayDateTime 109 110 Alarm(); // Alarm control 111 } 112 if (menu==1) 113 { 114 DisplaySetHour(); 115 116 } 117 if (menu==2) 118 { 119 DisplaySetMinute(); 120 } 121 if (menu==3) 122 123 { 124 DisplaySetYear(); 125 } 126 if (menu==4) 127 { 128 DisplaySetMonth(); 129 130 } 131 if (menu==5) 132 { 133 DisplaySetDay(); 134 } 135 if (menu==6) 136 137 { 138 StoreAgg(); 139 delay(500); 140 menu=0; 141 } 142 delay(100); 143} 144 145void 146 DisplayDateTime () 147{ 148// We show the current date and time 149 DateTime now 150 = RTC.now(); 151 152 lcd.setCursor(0, 2); 153 lcd.print("Hour : "); 154 155 156 if (now.hour()<=9) 157 { 158 lcd.print("0"); 159 } 160 lcd.print(now.hour(), 161 DEC); 162 hourupg=now.hour(); 163 lcd.print(":"); 164 if (now.minute()<=9) 165 166 { 167 lcd.print("0"); 168 } 169 lcd.print(now.minute(), DEC); 170 minupg=now.minute(); 171 172 lcd.print(":"); 173 if (now.second()<=9) 174 { 175 lcd.print("0"); 176 177 } 178 lcd.print(now.second(), DEC); 179 180 181 lcd.setCursor(0, 1); 182 lcd.print("Date 183 : "); 184 if (now.day()<=9) 185 { 186 lcd.print("0"); 187 } 188 lcd.print(now.day(), 189 DEC); 190 dayupg=now.day(); 191 lcd.print("/"); 192 if (now.month()<=9) 193 194 { 195 lcd.print("0"); 196 } 197 lcd.print(now.month(), DEC); 198 monthupg=now.month(); 199 200 lcd.print("/"); 201 lcd.print(now.year(), DEC); 202 yearupg=now.year(); 203 204 205 char DOW[][10]={"Sunday ","Monday ","Tuesday ","Wednesday","Thursday 206 ","Friday ","Saturday "}; 207 lcd.setCursor(0, 0); 208 lcd.print("Day 209 : "); 210 lcd.print(DOW[now.dayOfTheWeek()]); // if it appears error in the 211 code, enter the code given below 212 //lcd.print(DOW[now.dayOfWeek()]); 213} 214 215void 216 DisplaySetHour() 217{ 218// time setting 219 lcd.clear(); 220 DateTime now = RTC.now(); 221 222 if(digitalRead(P2)==LOW) 223 { 224 if(hourupg==23) 225 { 226 hourupg=0; 227 228 } 229 else 230 { 231 hourupg=hourupg+1; 232 } 233 } 234 if(digitalRead(P3)==LOW) 235 236 { 237 if(hourupg==0) 238 { 239 hourupg=23; 240 } 241 else 242 243 { 244 hourupg=hourupg-1; 245 } 246 } 247 lcd.setCursor(0,0); 248 lcd.print("Set 249 time:"); 250 lcd.setCursor(0,1); 251 lcd.print(hourupg,DEC); 252 delay(200); 253} 254 255void 256 DisplaySetMinute() 257{ 258// Setting the minutes 259 lcd.clear(); 260 if(digitalRead(P2)==LOW) 261 262 { 263 if (minupg==59) 264 { 265 minupg=0; 266 } 267 else 268 269 { 270 minupg=minupg+1; 271 } 272 } 273 if(digitalRead(P3)==LOW) 274 275 { 276 if (minupg==0) 277 { 278 minupg=59; 279 } 280 else 281 282 { 283 minupg=minupg-1; 284 } 285 } 286 lcd.setCursor(0,0); 287 lcd.print("Set 288 Minutes:"); 289 lcd.setCursor(0,1); 290 lcd.print(minupg,DEC); 291 delay(200); 292} 293 294 295void DisplaySetYear() 296{ 297// setting the year 298 lcd.clear(); 299 if(digitalRead(P2)==LOW) 300 301 { 302 yearupg=yearupg+1; 303 } 304 if(digitalRead(P3)==LOW) 305 { 306 307 yearupg=yearupg-1; 308 } 309 lcd.setCursor(0,0); 310 lcd.print("Set Year:"); 311 312 lcd.setCursor(0,1); 313 lcd.print(yearupg,DEC); 314 delay(200); 315} 316 317void 318 DisplaySetMonth() 319{ 320// Setting the month 321 lcd.clear(); 322 if(digitalRead(P2)==LOW) 323 324 { 325 if (monthupg==12) 326 { 327 monthupg=1; 328 } 329 else 330 331 { 332 monthupg=monthupg+1; 333 } 334 } 335 if(digitalRead(P3)==LOW) 336 337 { 338 if (monthupg==1) 339 { 340 monthupg=12; 341 } 342 else 343 344 { 345 monthupg=monthupg-1; 346 } 347 } 348 lcd.setCursor(0,0); 349 350 lcd.print("Set Month:"); 351 lcd.setCursor(0,1); 352 lcd.print(monthupg,DEC); 353 354 delay(200); 355} 356 357void DisplaySetDay() 358{ 359// Setting the day 360 lcd.clear(); 361 362 if(digitalRead(P2)==LOW) 363 { 364 if (dayupg==31) 365 { 366 dayupg=1; 367 368 } 369 else 370 { 371 dayupg=dayupg+1; 372 } 373 } 374 if(digitalRead(P3)==LOW) 375 376 { 377 if (dayupg==1) 378 { 379 dayupg=31; 380 } 381 else 382 383 { 384 dayupg=dayupg-1; 385 } 386 } 387 lcd.setCursor(0,0); 388 lcd.print("Set 389 Day:"); 390 lcd.setCursor(0,1); 391 lcd.print(dayupg,DEC); 392 delay(200); 393} 394 395void 396 StoreAgg() 397{ 398// Variable saving 399 lcd.clear(); 400 lcd.setCursor(0,0); 401 402 lcd.print("SAVING IN"); 403 lcd.setCursor(0,1); 404 lcd.print("PROGRESS"); 405 406 RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0)); 407 delay(200); 408} 409void 410 DisplaySetHourAll()// Setting the alarm minutes 411{ 412 while(digitalRead(P1)==HIGH){ 413 414 415 lcd.clear(); 416 417 if(digitalRead(P2)==LOW) 418 { 419 if(alarmHours==23) 420 421 { 422 alarmHours=0; 423 } 424 else 425 { 426 alarmHours=alarmHours+1; 427 428 } 429 } 430 if(digitalRead(P3)==LOW) 431 { 432 if(alarmHours==0) 433 434 { 435 alarmHours=23; 436 } 437 else 438 { 439 alarmHours=alarmHours-1; 440 441 } 442 } 443 lcd.setCursor(0,0); 444 lcd.print("Set HOUR Alarm:"); 445 lcd.setCursor(0,1); 446 447 lcd.print(alarmHours,DEC); 448 delay(200); 449 } 450 delay(200); 451} 452 453void 454 DisplaySetMinuteAll()// Setting the alarm minutes 455 { 456 while(digitalRead(P1)==HIGH){ 457 458 459 lcd.clear(); 460 if(digitalRead(P2)==LOW) 461 { 462 if (alarmMinutes==59) 463 464 { 465 alarmMinutes=0; 466 } 467 else 468 { 469 alarmMinutes=alarmMinutes+1; 470 471 } 472 } 473 if(digitalRead(P3)==LOW) 474 { 475 if (alarmMinutes==0) 476 477 { 478 alarmMinutes=59; 479 } 480 else 481 { 482 alarmMinutes=alarmMinutes-1; 483 484 } 485 } 486 lcd.setCursor(0,0); 487 lcd.print("Set MIN. Alarm:"); 488 lcd.setCursor(0,1); 489 490 lcd.print(alarmMinutes,DEC); 491 delay(200); 492 } 493 delay(200); 494} 495void 496 printAllOn(){ 497 lcd.setCursor(0,3); 498 lcd.print("Alarm: "); 499 500 501 502 503 if (alarmHours <= 9) 504 { 505 lcd.print("0"); 506 } 507 lcd.print(alarmHours, 508 DEC); 509 510 lcd.print(":"); 511 if (alarmMinutes <= 9) 512 { 513 lcd.print("0"); 514 515 } 516 lcd.print(alarmMinutes, DEC); 517 518} 519void printAllOff() { 520 lcd.setCursor(0, 521 3); 522 lcd.print("Alarm: Off "); 523} 524void Alarm(){ 525 if(digitalRead(P4)== 526 LOW) 527 { 528 setAll=setAll+1; 529 } 530 if (setAll==0) 531 { 532 printAllOff(); 533 534 noTone (dc water pump); 535 digitalWrite(LED,LOW); 536 } 537 if (setAll==1) 538 539 { 540 541 printAllOn(); 542 543 DateTime now = RTC.now(); 544 545 if ( now.hour() == alarmHours && now.minute() == alarmMinutes ) 546 { 547 548 lcd.noBacklight(); 549 DateTime now = RTC.now(); 550 digitalWrite(LED,HIGH); 551 552 tone(dc water pump,880); //play the note "A5" (LA5) 553 delay 554 (300); 555 tone(dc water pump,698); //play the note "F6" (FA5) 556 lcd.backlight(); 557 558 } 559 else{ 560 noTone (dc water pump); 561 digitalWrite(LED,LOW); 562 563 } 564 565 } 566 if (setAll==2) 567 { 568 setAll=0; 569 } 570 571 delay(200); 572}#include <Wire.h> 573#include <RTClib.h> 574#include <LiquidCrystal_I2C.h> 575 576//************************************// 577LiquidCrystal_I2C 578 lcd(0x27,20,4); // Display I2C 20 x 4 579RTC_DS1307 RTC; 580 581//************Button*****************// 582int 583 P1=6; // Button SET MENU' 584int P2=7; // Button + 585int P3=8; // Button - 586int 587 P4=9; // SWITCH Alarm 588 589//**************Alarm***************// 590#define LED 591 13 592#define dc water pump 10 593 594//************Variables**************// 595int 596 hourupg; 597int minupg; 598int yearupg; 599int monthupg; 600int dayupg; 601int menu 602 =0; 603int setAll =0; 604 605uint8_t alarmHours = 0, alarmMinutes = 0; // Holds 606 the current alarm time 607 608 609void setup() 610{ 611 612 lcd.begin(); 613 lcd.backlight(); 614 615 lcd.clear(); 616 617 pinMode(P1,INPUT_PULLUP); // https://www.arduino.cc/en/Tutorial/InputPullupSerial 618 619 pinMode(P2,INPUT_PULLUP); 620 pinMode(P3,INPUT_PULLUP); 621 pinMode(P4,INPUT_PULLUP); 622 623 pinMode(LED,OUTPUT); 624 pinMode(dc water pump, OUTPUT); // Set dc water pump 625 as an output 626 printAllOff(); 627 Serial.begin(9600); 628 Wire.begin(); 629 630 RTC.begin(); 631 632 if (! RTC.isrunning()) { 633 Serial.println("RTC is 634 NOT running!"); 635 // Set the date and time at compile time 636 RTC.adjust(DateTime(__DATE__, 637 __TIME__)); 638 } 639 // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing 640 "//" to adjust the time 641 // The default display shows the date and time 642 643 int menu=0; 644} 645 646void loop() 647{ 648 649// check if you press the SET 650 button and increase the menu index 651 if(digitalRead(P1)== LOW) 652 { 653 menu=menu+1; 654 655 } 656 if((digitalRead(P2)== LOW)&&(digitalRead(P3)== LOW)) 657 658 { 659 660 DisplaySetHourAll(); 661 DisplaySetMinuteAll(); 662 lcd.clear(); 663 664 lcd.setCursor(5,0); 665 lcd.print("ALARM"); 666 lcd.setCursor(5,1); 667 668 lcd.print(alarmHours, DEC); 669 lcd.print(":"); 670 lcd.print(alarmMinutes, 671 DEC); 672 delay(1000); 673 lcd.clear(); 674 } 675// in which subroutine should 676 we go? 677 if (menu==0) 678 { 679 DisplayDateTime(); // void DisplayDateTime 680 681 Alarm(); // Alarm control 682 } 683 if (menu==1) 684 { 685 DisplaySetHour(); 686 687 } 688 if (menu==2) 689 { 690 DisplaySetMinute(); 691 } 692 if (menu==3) 693 694 { 695 DisplaySetYear(); 696 } 697 if (menu==4) 698 { 699 DisplaySetMonth(); 700 701 } 702 if (menu==5) 703 { 704 DisplaySetDay(); 705 } 706 if (menu==6) 707 708 { 709 StoreAgg(); 710 delay(500); 711 menu=0; 712 } 713 delay(100); 714} 715 716void 717 DisplayDateTime () 718{ 719// We show the current date and time 720 DateTime now 721 = RTC.now(); 722 723 lcd.setCursor(0, 2); 724 lcd.print("Hour : "); 725 726 727 if (now.hour()<=9) 728 { 729 lcd.print("0"); 730 } 731 lcd.print(now.hour(), 732 DEC); 733 hourupg=now.hour(); 734 lcd.print(":"); 735 if (now.minute()<=9) 736 737 { 738 lcd.print("0"); 739 } 740 lcd.print(now.minute(), DEC); 741 minupg=now.minute(); 742 743 lcd.print(":"); 744 if (now.second()<=9) 745 { 746 lcd.print("0"); 747 748 } 749 lcd.print(now.second(), DEC); 750 751 752 lcd.setCursor(0, 1); 753 lcd.print("Date 754 : "); 755 if (now.day()<=9) 756 { 757 lcd.print("0"); 758 } 759 lcd.print(now.day(), 760 DEC); 761 dayupg=now.day(); 762 lcd.print("/"); 763 if (now.month()<=9) 764 765 { 766 lcd.print("0"); 767 } 768 lcd.print(now.month(), DEC); 769 monthupg=now.month(); 770 771 lcd.print("/"); 772 lcd.print(now.year(), DEC); 773 yearupg=now.year(); 774 775 776 char DOW[][10]={"Sunday ","Monday ","Tuesday ","Wednesday","Thursday 777 ","Friday ","Saturday "}; 778 lcd.setCursor(0, 0); 779 lcd.print("Day 780 : "); 781 lcd.print(DOW[now.dayOfTheWeek()]); // if it appears error in the 782 code, enter the code given below 783 //lcd.print(DOW[now.dayOfWeek()]); 784} 785 786void 787 DisplaySetHour() 788{ 789// time setting 790 lcd.clear(); 791 DateTime now = RTC.now(); 792 793 if(digitalRead(P2)==LOW) 794 { 795 if(hourupg==23) 796 { 797 hourupg=0; 798 799 } 800 else 801 { 802 hourupg=hourupg+1; 803 } 804 } 805 if(digitalRead(P3)==LOW) 806 807 { 808 if(hourupg==0) 809 { 810 hourupg=23; 811 } 812 else 813 814 { 815 hourupg=hourupg-1; 816 } 817 } 818 lcd.setCursor(0,0); 819 lcd.print("Set 820 time:"); 821 lcd.setCursor(0,1); 822 lcd.print(hourupg,DEC); 823 delay(200); 824} 825 826void 827 DisplaySetMinute() 828{ 829// Setting the minutes 830 lcd.clear(); 831 if(digitalRead(P2)==LOW) 832 833 { 834 if (minupg==59) 835 { 836 minupg=0; 837 } 838 else 839 840 { 841 minupg=minupg+1; 842 } 843 } 844 if(digitalRead(P3)==LOW) 845 846 { 847 if (minupg==0) 848 { 849 minupg=59; 850 } 851 else 852 853 { 854 minupg=minupg-1; 855 } 856 } 857 lcd.setCursor(0,0); 858 lcd.print("Set 859 Minutes:"); 860 lcd.setCursor(0,1); 861 lcd.print(minupg,DEC); 862 delay(200); 863} 864 865 866void DisplaySetYear() 867{ 868// setting the year 869 lcd.clear(); 870 if(digitalRead(P2)==LOW) 871 872 { 873 yearupg=yearupg+1; 874 } 875 if(digitalRead(P3)==LOW) 876 { 877 878 yearupg=yearupg-1; 879 } 880 lcd.setCursor(0,0); 881 lcd.print("Set Year:"); 882 883 lcd.setCursor(0,1); 884 lcd.print(yearupg,DEC); 885 delay(200); 886} 887 888void 889 DisplaySetMonth() 890{ 891// Setting the month 892 lcd.clear(); 893 if(digitalRead(P2)==LOW) 894 895 { 896 if (monthupg==12) 897 { 898 monthupg=1; 899 } 900 else 901 902 { 903 monthupg=monthupg+1; 904 } 905 } 906 if(digitalRead(P3)==LOW) 907 908 { 909 if (monthupg==1) 910 { 911 monthupg=12; 912 } 913 else 914 915 { 916 monthupg=monthupg-1; 917 } 918 } 919 lcd.setCursor(0,0); 920 921 lcd.print("Set Month:"); 922 lcd.setCursor(0,1); 923 lcd.print(monthupg,DEC); 924 925 delay(200); 926} 927 928void DisplaySetDay() 929{ 930// Setting the day 931 lcd.clear(); 932 933 if(digitalRead(P2)==LOW) 934 { 935 if (dayupg==31) 936 { 937 dayupg=1; 938 939 } 940 else 941 { 942 dayupg=dayupg+1; 943 } 944 } 945 if(digitalRead(P3)==LOW) 946 947 { 948 if (dayupg==1) 949 { 950 dayupg=31; 951 } 952 else 953 954 { 955 dayupg=dayupg-1; 956 } 957 } 958 lcd.setCursor(0,0); 959 lcd.print("Set 960 Day:"); 961 lcd.setCursor(0,1); 962 lcd.print(dayupg,DEC); 963 delay(200); 964} 965 966void 967 StoreAgg() 968{ 969// Variable saving 970 lcd.clear(); 971 lcd.setCursor(0,0); 972 973 lcd.print("SAVING IN"); 974 lcd.setCursor(0,1); 975 lcd.print("PROGRESS"); 976 977 RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0)); 978 delay(200); 979} 980void 981 DisplaySetHourAll()// Setting the alarm minutes 982{ 983 while(digitalRead(P1)==HIGH){ 984 985 986 lcd.clear(); 987 988 if(digitalRead(P2)==LOW) 989 { 990 if(alarmHours==23) 991 992 { 993 alarmHours=0; 994 } 995 else 996 { 997 alarmHours=alarmHours+1; 998 999 } 1000 } 1001 if(digitalRead(P3)==LOW) 1002 { 1003 if(alarmHours==0) 1004 1005 { 1006 alarmHours=23; 1007 } 1008 else 1009 { 1010 alarmHours=alarmHours-1; 1011 1012 } 1013 } 1014 lcd.setCursor(0,0); 1015 lcd.print("Set HOUR Alarm:"); 1016 lcd.setCursor(0,1); 1017 1018 lcd.print(alarmHours,DEC); 1019 delay(200); 1020 } 1021 delay(200); 1022} 1023 1024void 1025 DisplaySetMinuteAll()// Setting the alarm minutes 1026 { 1027 while(digitalRead(P1)==HIGH){ 1028 1029 1030 lcd.clear(); 1031 if(digitalRead(P2)==LOW) 1032 { 1033 if (alarmMinutes==59) 1034 1035 { 1036 alarmMinutes=0; 1037 } 1038 else 1039 { 1040 alarmMinutes=alarmMinutes+1; 1041 1042 } 1043 } 1044 if(digitalRead(P3)==LOW) 1045 { 1046 if (alarmMinutes==0) 1047 1048 { 1049 alarmMinutes=59; 1050 } 1051 else 1052 { 1053 alarmMinutes=alarmMinutes-1; 1054 1055 } 1056 } 1057 lcd.setCursor(0,0); 1058 lcd.print("Set MIN. Alarm:"); 1059 lcd.setCursor(0,1); 1060 1061 lcd.print(alarmMinutes,DEC); 1062 delay(200); 1063 } 1064 delay(200); 1065} 1066void 1067 printAllOn(){ 1068 lcd.setCursor(0,3); 1069 lcd.print("Alarm: "); 1070 1071 1072 1073 1074 if (alarmHours <= 9) 1075 { 1076 lcd.print("0"); 1077 } 1078 lcd.print(alarmHours, 1079 DEC); 1080 1081 lcd.print(":"); 1082 if (alarmMinutes <= 9) 1083 { 1084 lcd.print("0"); 1085 1086 } 1087 lcd.print(alarmMinutes, DEC); 1088 1089} 1090void printAllOff() { 1091 lcd.setCursor(0, 1092 3); 1093 lcd.print("Alarm: Off "); 1094} 1095void Alarm(){ 1096 if(digitalRead(P4)== 1097 LOW) 1098 { 1099 setAll=setAll+1; 1100 } 1101 if (setAll==0) 1102 { 1103 printAllOff(); 1104 1105 noTone (dc water pump); 1106 digitalWrite(LED,LOW); 1107 } 1108 if (setAll==1) 1109 1110 { 1111 1112 printAllOn(); 1113 1114 DateTime now = RTC.now(); 1115 1116 if ( now.hour() == alarmHours && now.minute() == alarmMinutes ) 1117 { 1118 1119 lcd.noBacklight(); 1120 DateTime now = RTC.now(); 1121 digitalWrite(LED,HIGH); 1122 1123 tone(dc water pump,880); //play the note "A5" (LA5) 1124 delay 1125 (300); 1126 tone(dc water pump,698); //play the note "F6" (FA5) 1127 lcd.backlight(); 1128 1129 } 1130 else{ 1131 noTone (dc water pump); 1132 digitalWrite(LED,LOW); 1133 1134 } 1135 1136 } 1137 if (setAll==2) 1138 { 1139 setAll=0; 1140 } 1141 1142 delay(200); 1143}#include <Wire.h> 1144#include <RTClib.h> 1145#include <LiquidCrystal_I2C.h> 1146 1147//************************************// 1148LiquidCrystal_I2C 1149 lcd(0x27,20,4); // Display I2C 20 x 4 1150RTC_DS1307 RTC; 1151 1152//************Button*****************// 1153int 1154 P1=6; // Button SET MENU' 1155int P2=7; // Button + 1156int P3=8; // Button - 1157int 1158 P4=9; // SWITCH Alarm 1159 1160//**************Alarm***************// 1161#define LED 1162 13 1163#define dc water pump 10 1164 1165//************Variables**************// 1166int 1167 hourupg; 1168int minupg; 1169int yearupg; 1170int monthupg; 1171int dayupg; 1172int menu 1173 =0; 1174int setAll =0; 1175 1176uint8_t alarmHours = 0, alarmMinutes = 0; // Holds 1177 the current alarm time 1178 1179 1180void setup() 1181{ 1182 1183 lcd.begin(); 1184 lcd.backlight(); 1185 1186 lcd.clear(); 1187 1188 pinMode(P1,INPUT_PULLUP); // https://www.arduino.cc/en/Tutorial/InputPullupSerial 1189 1190 pinMode(P2,INPUT_PULLUP); 1191 pinMode(P3,INPUT_PULLUP); 1192 pinMode(P4,INPUT_PULLUP); 1193 1194 pinMode(LED,OUTPUT); 1195 pinMode(dc water pump, OUTPUT); // Set dc water pump 1196 as an output 1197 printAllOff(); 1198 Serial.begin(9600); 1199 Wire.begin(); 1200 1201 RTC.begin(); 1202 1203 if (! RTC.isrunning()) { 1204 Serial.println("RTC is 1205 NOT running!"); 1206 // Set the date and time at compile time 1207 RTC.adjust(DateTime(__DATE__, 1208 __TIME__)); 1209 } 1210 // RTC.adjust(DateTime(__DATE__, __TIME__)); //removing 1211 "//" to adjust the time 1212 // The default display shows the date and time 1213 1214 int menu=0; 1215} 1216 1217void loop() 1218{ 1219 1220// check if you press the SET 1221 button and increase the menu index 1222 if(digitalRead(P1)== LOW) 1223 { 1224 menu=menu+1; 1225 1226 } 1227 if((digitalRead(P2)== LOW)&&(digitalRead(P3)== LOW)) 1228 1229 { 1230 1231 DisplaySetHourAll(); 1232 DisplaySetMinuteAll(); 1233 lcd.clear(); 1234 1235 lcd.setCursor(5,0); 1236 lcd.print("ALARM"); 1237 lcd.setCursor(5,1); 1238 1239 lcd.print(alarmHours, DEC); 1240 lcd.print(":"); 1241 lcd.print(alarmMinutes, 1242 DEC); 1243 delay(1000); 1244 lcd.clear(); 1245 } 1246// in which subroutine should 1247 we go? 1248 if (menu==0) 1249 { 1250 DisplayDateTime(); // void DisplayDateTime 1251 1252 Alarm(); // Alarm control 1253 } 1254 if (menu==1) 1255 { 1256 DisplaySetHour(); 1257 1258 } 1259 if (menu==2) 1260 { 1261 DisplaySetMinute(); 1262 } 1263 if (menu==3) 1264 1265 { 1266 DisplaySetYear(); 1267 } 1268 if (menu==4) 1269 { 1270 DisplaySetMonth(); 1271 1272 } 1273 if (menu==5) 1274 { 1275 DisplaySetDay(); 1276 } 1277 if (menu==6) 1278 1279 { 1280 StoreAgg(); 1281 delay(500); 1282 menu=0; 1283 } 1284 delay(100); 1285} 1286 1287void 1288 DisplayDateTime () 1289{ 1290// We show the current date and time 1291 DateTime now 1292 = RTC.now(); 1293 1294 lcd.setCursor(0, 2); 1295 lcd.print("Hour : "); 1296 1297 1298 if (now.hour()<=9) 1299 { 1300 lcd.print("0"); 1301 } 1302 lcd.print(now.hour(), 1303 DEC); 1304 hourupg=now.hour(); 1305 lcd.print(":"); 1306 if (now.minute()<=9) 1307 1308 { 1309 lcd.print("0"); 1310 } 1311 lcd.print(now.minute(), DEC); 1312 minupg=now.minute(); 1313 1314 lcd.print(":"); 1315 if (now.second()<=9) 1316 { 1317 lcd.print("0"); 1318 1319 } 1320 lcd.print(now.second(), DEC); 1321 1322 1323 lcd.setCursor(0, 1); 1324 lcd.print("Date 1325 : "); 1326 if (now.day()<=9) 1327 { 1328 lcd.print("0"); 1329 } 1330 lcd.print(now.day(), 1331 DEC); 1332 dayupg=now.day(); 1333 lcd.print("/"); 1334 if (now.month()<=9) 1335 1336 { 1337 lcd.print("0"); 1338 } 1339 lcd.print(now.month(), DEC); 1340 monthupg=now.month(); 1341 1342 lcd.print("/"); 1343 lcd.print(now.year(), DEC); 1344 yearupg=now.year(); 1345 1346 1347 char DOW[][10]={"Sunday ","Monday ","Tuesday ","Wednesday","Thursday 1348 ","Friday ","Saturday "}; 1349 lcd.setCursor(0, 0); 1350 lcd.print("Day 1351 : "); 1352 lcd.print(DOW[now.dayOfTheWeek()]); // if it appears error in the 1353 code, enter the code given below 1354 //lcd.print(DOW[now.dayOfWeek()]); 1355} 1356 1357void 1358 DisplaySetHour() 1359{ 1360// time setting 1361 lcd.clear(); 1362 DateTime now = RTC.now(); 1363 1364 if(digitalRead(P2)==LOW) 1365 { 1366 if(hourupg==23) 1367 { 1368 hourupg=0; 1369 1370 } 1371 else 1372 { 1373 hourupg=hourupg+1; 1374 } 1375 } 1376 if(digitalRead(P3)==LOW) 1377 1378 { 1379 if(hourupg==0) 1380 { 1381 hourupg=23; 1382 } 1383 else 1384 1385 { 1386 hourupg=hourupg-1; 1387 } 1388 } 1389 lcd.setCursor(0,0); 1390 lcd.print("Set 1391 time:"); 1392 lcd.setCursor(0,1); 1393 lcd.print(hourupg,DEC); 1394 delay(200); 1395} 1396 1397void 1398 DisplaySetMinute() 1399{ 1400// Setting the minutes 1401 lcd.clear(); 1402 if(digitalRead(P2)==LOW) 1403 1404 { 1405 if (minupg==59) 1406 { 1407 minupg=0; 1408 } 1409 else 1410 1411 { 1412 minupg=minupg+1; 1413 } 1414 } 1415 if(digitalRead(P3)==LOW) 1416 1417 { 1418 if (minupg==0) 1419 { 1420 minupg=59; 1421 } 1422 else 1423 1424 { 1425 minupg=minupg-1; 1426 } 1427 } 1428 lcd.setCursor(0,0); 1429 lcd.print("Set 1430 Minutes:"); 1431 lcd.setCursor(0,1); 1432 lcd.print(minupg,DEC); 1433 delay(200); 1434} 1435 1436 1437void DisplaySetYear() 1438{ 1439// setting the year 1440 lcd.clear(); 1441 if(digitalRead(P2)==LOW) 1442 1443 { 1444 yearupg=yearupg+1; 1445 } 1446 if(digitalRead(P3)==LOW) 1447 { 1448 1449 yearupg=yearupg-1; 1450 } 1451 lcd.setCursor(0,0); 1452 lcd.print("Set Year:"); 1453 1454 lcd.setCursor(0,1); 1455 lcd.print(yearupg,DEC); 1456 delay(200); 1457} 1458 1459void 1460 DisplaySetMonth() 1461{ 1462// Setting the month 1463 lcd.clear(); 1464 if(digitalRead(P2)==LOW) 1465 1466 { 1467 if (monthupg==12) 1468 { 1469 monthupg=1; 1470 } 1471 else 1472 1473 { 1474 monthupg=monthupg+1; 1475 } 1476 } 1477 if(digitalRead(P3)==LOW) 1478 1479 { 1480 if (monthupg==1) 1481 { 1482 monthupg=12; 1483 } 1484 else 1485 1486 { 1487 monthupg=monthupg-1; 1488 } 1489 } 1490 lcd.setCursor(0,0); 1491 1492 lcd.print("Set Month:"); 1493 lcd.setCursor(0,1); 1494 lcd.print(monthupg,DEC); 1495 1496 delay(200); 1497} 1498 1499void DisplaySetDay() 1500{ 1501// Setting the day 1502 lcd.clear(); 1503 1504 if(digitalRead(P2)==LOW) 1505 { 1506 if (dayupg==31) 1507 { 1508 dayupg=1; 1509 1510 } 1511 else 1512 { 1513 dayupg=dayupg+1; 1514 } 1515 } 1516 if(digitalRead(P3)==LOW) 1517 1518 { 1519 if (dayupg==1) 1520 { 1521 dayupg=31; 1522 } 1523 else 1524 1525 { 1526 dayupg=dayupg-1; 1527 } 1528 } 1529 lcd.setCursor(0,0); 1530 lcd.print("Set 1531 Day:"); 1532 lcd.setCursor(0,1); 1533 lcd.print(dayupg,DEC); 1534 delay(200); 1535} 1536 1537void 1538 StoreAgg() 1539{ 1540// Variable saving 1541 lcd.clear(); 1542 lcd.setCursor(0,0); 1543 1544 lcd.print("SAVING IN"); 1545 lcd.setCursor(0,1); 1546 lcd.print("PROGRESS"); 1547 1548 RTC.adjust(DateTime(yearupg,monthupg,dayupg,hourupg,minupg,0)); 1549 delay(200); 1550} 1551void 1552 DisplaySetHourAll()// Setting the alarm minutes 1553{ 1554 while(digitalRead(P1)==HIGH){ 1555 1556 1557 lcd.clear(); 1558 1559 if(digitalRead(P2)==LOW) 1560 { 1561 if(alarmHours==23) 1562 1563 { 1564 alarmHours=0; 1565 } 1566 else 1567 { 1568 alarmHours=alarmHours+1; 1569 1570 } 1571 } 1572 if(digitalRead(P3)==LOW) 1573 { 1574 if(alarmHours==0) 1575 1576 { 1577 alarmHours=23; 1578 } 1579 else 1580 { 1581 alarmHours=alarmHours-1; 1582 1583 } 1584 } 1585 lcd.setCursor(0,0); 1586 lcd.print("Set HOUR Alarm:"); 1587 lcd.setCursor(0,1); 1588 1589 lcd.print(alarmHours,DEC); 1590 delay(200); 1591 } 1592 delay(200); 1593} 1594 1595void 1596 DisplaySetMinuteAll()// Setting the alarm minutes 1597 { 1598 while(digitalRead(P1)==HIGH){ 1599 1600 1601 lcd.clear(); 1602 if(digitalRead(P2)==LOW) 1603 { 1604 if (alarmMinutes==59) 1605 1606 { 1607 alarmMinutes=0; 1608 } 1609 else 1610 { 1611 alarmMinutes=alarmMinutes+1; 1612 1613 } 1614 } 1615 if(digitalRead(P3)==LOW) 1616 { 1617 if (alarmMinutes==0) 1618 1619 { 1620 alarmMinutes=59; 1621 } 1622 else 1623 { 1624 alarmMinutes=alarmMinutes-1; 1625 1626 } 1627 } 1628 lcd.setCursor(0,0); 1629 lcd.print("Set MIN. Alarm:"); 1630 lcd.setCursor(0,1); 1631 1632 lcd.print(alarmMinutes,DEC); 1633 delay(200); 1634 } 1635 delay(200); 1636} 1637void 1638 printAllOn(){ 1639 lcd.setCursor(0,3); 1640 lcd.print("Alarm: "); 1641 1642 1643 1644 1645 if (alarmHours <= 9) 1646 { 1647 lcd.print("0"); 1648 } 1649 lcd.print(alarmHours, 1650 DEC); 1651 1652 lcd.print(":"); 1653 if (alarmMinutes <= 9) 1654 { 1655 lcd.print("0"); 1656 1657 } 1658 lcd.print(alarmMinutes, DEC); 1659 1660} 1661void printAllOff() { 1662 lcd.setCursor(0, 1663 3); 1664 lcd.print("Alarm: Off "); 1665} 1666void Alarm(){ 1667 if(digitalRead(P4)== 1668 LOW) 1669 { 1670 setAll=setAll+1; 1671 } 1672 if (setAll==0) 1673 { 1674 printAllOff(); 1675 1676 noTone (dc water pump); 1677 digitalWrite(LED,LOW); 1678 } 1679 if (setAll==1) 1680 1681 { 1682 1683 printAllOn(); 1684 1685 DateTime now = RTC.now(); 1686 1687 if ( now.hour() == alarmHours && now.minute() == alarmMinutes ) 1688 { 1689 1690 lcd.noBacklight(); 1691 DateTime now = RTC.now(); 1692 digitalWrite(LED,HIGH); 1693 1694 tone(dc water pump,880); //play the note "A5" (LA5) 1695 delay 1696 (300); 1697 tone(dc water pump,698); //play the note "F6" (FA5) 1698 lcd.backlight(); 1699 1700 } 1701 else{ 1702 noTone (dc water pump); 1703 digitalWrite(LED,LOW); 1704 1705 } 1706 1707 } 1708 if (setAll==2) 1709 { 1710 setAll=0; 1711 } 1712 1713 delay(200); 1714}
Downloadable files
allarm_clock_bb_cfgmnpj5c9_Ax8peduIZr.jpg
allarm_clock_bb_cfgmnpj5c9_Ax8peduIZr.jpg

Comments
Only logged in users can leave comments