Components and supplies
Pushbutton switch 12mm
Arduino UNO
Real Time Clock (RTC)
RGB Backlight LCD - 16x2
Project description
Code
clock alarm timer
arduino
This is a compleet stand alone working clock with alarm and timer switch
1/* 2 Clock and timer developed by Ronald Siemons 3 The sketch is free software and for anybody free to use on his or her own risk, if there would be. 4 5 TO KNOW!!!!!!!!!! 6 Make a choice out of the menu what show up on the display when pushing the select button. Every 7 two secconds an other choice to make will be shown as long as the select button is pushed. By letting it go 8 the last shown choice will be made. 9 10 What it does: 11 The clock and timer switches on and off a relais. 12 The clock is made for stand alone usesage. So one can set the time and the alarm on the device itself 13 without the need of an USB connection to a computer. 14 The general idee: 15 The clock is mate as part of a system to switch on and off the lights in a shop. This part is made 16 for the neon lights at the front of the shop. The lights only work in the evening and go off every day at the same time. 17 So the alarm is set on the off switch of the lights and as the time off lighting up the lights is known the timer works 18 backwords from the known of switching time back during a certain period to the starting moment of the lights. 19 The period of lighting depends on the pereod of the year and can be set by hand or automatic. 20 As it has to work all year round without setting and resetting the system every now and again 21 daylight saving settings ar built in also. Readjustment of the daylight savings has to be done 22 for the specific country it is going to be used. 23*/ 24#include <Pushbutton.h> // part of arduino library. Search in manage libraries. 25#include <EEPROM.h> 26#include <RTClib.h> 27#include <LCD.h> 28#include <LiquidCrystal.h> 29#include <LiquidCrystal_I2C.h> 30#include <Wire.h> 31 32LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address 33RTC_DS1307 RTC; 34 35const int DS1307 = 0x68; // Address of DS1307 see data sheets //?????????? 36const int lightAlarm_pin = 6; 37const int selectButton_pin = 5; 38const int nextButton_pin = 4; 39const int upButton_pin = 3; 40const int downButton_pin = 2; 41const int alarmButton_pin = A1; // gets an other function later 42const boolean alarmSystem_pin = 7; 43const boolean timerLightOnLED_pin = 8; 44 45Pushbutton selectButton(selectButton_pin); //equal to pinMode(selectButton_Pin,INPUT_PULLUP); 46Pushbutton nextButton(nextButton_pin); 47Pushbutton upButton(upButton_pin); 48Pushbutton downButton(downButton_pin); 49Pushbutton alarmButton(alarmButton_pin); 50 51enum mainSelection { 52 ALARM_ON_OFF = 1, //switching alarm on or off = 1 53 ALARM_SEASON, //change the time of starting = 2 54 ALARM_SET, //to set the alarm start and stop time = 3 55 CLOCK_SET, //to set time and date = 4 56 AlARM_AUTO, //Light timer automatic or manual = 5 57 RUN_CLOCK //Reading and displaying time and date = 6 58}; 59enum mainSelection key = RUN_CLOCK; 60 61int alarmMinuteStart = 1; //Time timer starts according to the settings of alarmPlus and alarmxxxStop 62int alarmHourStart = 1; //Time timer starts according to the settings of alarmPlus and alarmxxxStop 63int alarmMinuteStop = 2; //Time the timer has been set to stop 64int alarmHourStop = 2; //Time teh timer has been set to stop Minimum 6 65int alarmPlus = 0; //Working hours of the lights Maximum 6 66boolean alarmOnOff = false; //Turns the alarm On/Off //off is false and on is true 67boolean autoManual = false; //Timer automatic or manual 68int alarmOffSet = 6; 69int yr = 2000; 70int mo = 1; 71int dy = 1; 72int hr = 0; 73int mn = 0; 74int setYr = 00; 75int setMo = 1; 76int setDy = 1; 77int setHr = 0; 78int setMn = 0; 79boolean daylightSaving = true; 80boolean aBlink = true; 81const int loopCheckMax = 80; //duration of the loops in the settings 82int loopCheck = 0; //to controll if if there is any interaction 83unsigned long clockDisplayCheck = millis(); //counter in the loop 84int lcdDisplayCheck = 0; //counter for controling the time the lcd will be on 85boolean timerLight = false; //set the relais for the alarm lights 86int timerLightStage = 0; 87 88 89//((((((((((((((((((((((((((((SETUP)))))))))))))))))))))))))))))))) 90void setup() { 91 pinMode(lightAlarm_pin, OUTPUT); 92 pinMode(alarmSystem_pin, OUTPUT); 93 pinMode(timerLightOnLED_pin, OUTPUT); 94 lcd.begin(16, 2); 95 Wire.begin(); 96 RTC.begin(); 97 Serial.begin(9600); 98 clockRunning(); 99 100 lcd.clear(); 101 lcd.on(); 102 displayLcdFirstLine( 2); 103 readTimeNow(); 104 displayTimeNow(2); 105 readAlarm(); 106} 107//((((((((((((((((((((((((((((----)))))))))))))))))))))))))))))))) 108//***************************^LOOP^******************************* 109void loop() { 110 key = RUN_CLOCK; 111 lightManualyOnOff(); //to switch the relay manualy when the alarm has been turned off 112 lcdLight(); //turns on the lcd on demand 113 if ((millis() - clockDisplayCheck) > 500) //Reading and displaying date and time continuesly every 0.5 second 114 { 115 clockRunning(); 116 clockDisplayCheck = millis(); //sets the timer variable 117 readTimeNow(); //reading time out of the Real Time Clock 118 displayTimeNow(2); //displaying the time now and the digit to blink 119 readAlarm(); //reading the alarm settings out off the Eeprom 120 displayLcdFirstLine( 0); //Basic display line 121 alarmActivate(); //setting the alarm 122 timerLightOnOff(); 123 } 124 125 if (selectButton.getSingleDebouncedPress()) 126 { 127 lcdDisplayCheck = 0; // lcd back to maximum time 128 lcdLight(); //turns lcd on 129 mainKeySelection(); // reads keySelection ALARM_ON_OFF,ALARM_SEASON,ALARM_SET,CLOCK_SET,RUN_CLOCK, 130 // lcd.setCursor(0, 0); 131 132 switch (key) { 133 case ALARM_ON_OFF: 134 setTimerOnOff(); 135 break; 136 137 case ALARM_SEASON: 138 setTimerPlus(); 139 break; 140 141 case ALARM_SET: 142 setHr = alarmHourStop; 143 setMn = alarmMinuteStop; 144 lcd.setCursor(0, 0); 145 lcd.print("Set alarm time "); 146 presetClockTime(alarmOffSet); //Gives an off-set to the alarm stopping time of 6? hours 147 if (setHr == 12) { //to prevent the timer passing mid-night 148 setHr = 11; 149 setMn = 59; 150 } 151 saveAlarmSet(); 152 // readAlarm(); 153 break; 154 155 case CLOCK_SET: 156 presetClockDate(); 157 readTimeNow(); //reading the current time to display in presetClockTime 158 setHr = hr; 159 setMn = mn; 160 lcd.setCursor(0, 0); 161 lcd.print("Set clock time "); 162 presetClockTime(0); //Time can be set starting at zero. The function is also in use to set the alarm stopping time 163 saveClock(); 164 break; 165 166 case AlARM_AUTO: 167 alarmAutoManual(); 168 break; 169 } 170 } 171} 172//***************************~~******************************* 173 174//function clock running// A check about the clock being running 175void clockRunning() { 176 if (!RTC.begin() || !RTC.isrunning()) { 177 while (!selectButton.getSingleDebouncedPress ()) { 178 if (digitalRead(alarmSystem_pin) == LOW) digitalWrite(alarmSystem_pin, HIGH); 179 else digitalWrite(alarmSystem_pin, LOW); 180 delay(100); 181 } 182 } 183} 184 185//function lcdTimerLights // timer for the lcd lcdDisplayCheck incrises with every half a second 186void lcdLight() { 187 if (alarmButton.getSingleDebouncedPress()) lcdDisplayCheck = 0; 188 lcdDisplayCheck++; 189 if (lcdDisplayCheck < 900) { //times half a second the display will be on 190 lcd.display(); 191 lcd.backlight(); 192 } 193 else { 194 lcd.noDisplay(); 195 lcd.noBacklight(); 196 if (lcdDisplayCheck > 2000) lcdDisplayCheck = 900; //resetting the variable to prevent the figure gets out of his boundary while the light stays off 197 } 198} 199 200///function timerLightOnOff // works in automatic and manual to set the pin for the relais 201void timerLightOnOff() { 202 if (timerLight) { 203 digitalWrite(lightAlarm_pin, LOW); // the relay activates to zero 204 digitalWrite(timerLightOnLED_pin, HIGH); 205 } else { 206 digitalWrite(lightAlarm_pin, HIGH); 207 digitalWrite(timerLightOnLED_pin, LOW); 208 } 209 210} 211 212///function lightManualyOnOff // to set the timer relais by hand 213void lightManualyOnOff() { 214 if (!alarmOnOff) { 215 if (nextButton.getSingleDebouncedPress()) { 216 Serial.print (timerLight); 217 timerLight = !timerLight; 218 } 219 } 220} 221////function readTimeNow////reading date and time 222void readTimeNow() { 223 DateTime now = RTC.now(); 224 yr = now.year(); 225 mo = now.month(); 226 dy = now.day(); 227 hr = now.hour(); 228 mn = now.minute(); 229} 230 231///function readAlarm////to read the alarm settings from the Eeprom 232void readAlarm() { 233 alarmHourStop = EEPROM.read(0); 234 delay(15); 235 alarmMinuteStop = EEPROM.read(1); 236 delay(15); 237 alarmPlus = EEPROM.read(2); 238 delay(15); 239 alarmOnOff = EEPROM.read(3); 240 delay(50); 241 autoManual = EEPROM.read(4); 242 delay(50); 243 if (alarmHourStop <= alarmOffSet) alarmHourStop = alarmOffSet + 1; //to prevent the alarm storting before the day has begun 244 alarmHourStart = alarmHourStop - alarmPlus; 245 alarmMinuteStart = alarmMinuteStop;//calculating the timer stopping time 246 if (alarmAutoManual) automaticTimer(); //Sets the timer automaticaly in the preset time scedule 247 dayLightSaving(); 248} 249 250void alarmActivate() { 251 if (alarmOnOff) { //Checking if the alarm has to be activated 252 253 if ((hr - alarmHourStart) == 0) 254 { 255 if (mn >= alarmMinuteStart) 256 timerLight = true; 257 else timerLight = false; 258 } 259 else if (hr - alarmHourStop == 0) 260 { 261 if ( mn <= alarmMinuteStop) 262 timerLight = true; 263 else timerLight = false; 264 } 265 else if ((alarmHourStop - hr < alarmPlus) && (alarmHourStop - hr >= 1)) timerLight = true; 266 else timerLight = false; 267 268 } 269 // else timerLight = false; // has to e removed ??????????????????????????????????? 270} 271 272////function display time now////display time 273void displayTimeNow(int digitBlink) { 274 char textDT[16]; 275 sprintf(textDT, "%02d:%02d %02d/%02d/%04d", hr, mn, dy, mo, yr); //puts int variabels in the rigth format into textDT 276 lcd.setCursor(0, 1); 277 lcd.print(textDT); //prints in four and two digits 278 lcd.setCursor(digitBlink, 1); 279 if (aBlink) lcd.print(" "); else lcd.print(":"); 280 aBlink = !aBlink; 281} 282 283 284////function displayLcdFirstLine////displays the first line 285void displayLcdFirstLine( int blinking) { //blinking 0 is back to RUN position 286 287 if (blinking > 0) { 288 for (int i = 1; i <= blinking; i++) { 289 lcd.setCursor(0, 0); 290 lcd.print(" "); 291 lcd.setCursor(0, 0); 292 delay(400); 293 lcd.print("TIMER-1.00 "); //Titel 294 lcd.setCursor(11, 0); 295 if (alarmOnOff == false) lcd.print("off~ "); else lcd.print(" on~ "); 296 lcd.setCursor(15, 0); 297 if (autoManual) lcd.print("A"); else lcd.print(alarmPlus); 298 299 delay(400); 300 } 301 } else { 302 lcd.setCursor(0, 0); 303 lcd.print("TIMER-1.00"); //Titel 304 lcd.setCursor(10, 0); 305 if (alarmOnOff == false) lcd.print(" off~ "); else lcd.print(" on~ "); 306 lcd.setCursor(15, 0); 307 if (autoManual) lcd.print("A"); else lcd.print(alarmPlus); 308 delay(400); 309 } 310} 311 312 313 314////function mainKeySelection////make a selection to set timer on/off, alarm season, alarm set, clock set 315void mainKeySelection() { 316 unsigned long started = millis(); 317 delay(10); 318 unsigned long moment; 319 const int laps = 2000; 320 int state = 0; 321 int lapTime = 0; 322 323 while (selectButton.isPressed()) { 324 moment = millis(); 325 delay(100); 326 lapTime = moment - started; 327 if (((lapTime) >= (0 * laps)) && ((lapTime) <= (1 * laps))) state = 1; 328 else if (((lapTime) >= (1 * laps)) && ((lapTime) <= (2 * laps))) state = 2; 329 else if (((lapTime) >= (2 * laps)) && ((lapTime) <= (3 * laps))) state = 3; 330 else if (((lapTime) >= (3 * laps)) && ((lapTime) <= (4 * laps))) state = 4; 331 else if (((lapTime) >= (4 * laps)) && ((lapTime) <= (5 * laps))) state = 5; 332 else if (((lapTime) >= (5 * laps)) && ((lapTime) <= (6 * laps))) state = 6; 333 delay(500); 334 switch (state) { 335 case 1: 336 lcd.setCursor(0, 1); 337 lcd.print("set alarm on/off"); 338 break; 339 case 2: 340 lcd.setCursor(0, 1); 341 lcd.print("Timer hours set "); 342 break; 343 case 3: 344 lcd.setCursor(0, 1); 345 lcd.print("Alarm time set! "); 346 break; 347 case 4: 348 lcd.setCursor(0, 1); 349 lcd.print("Clock time set! "); 350 break; 351 case 5: 352 lcd.setCursor(0, 1); 353 lcd.print("Alarm man/auto"); 354 break; 355 case 6: 356 lcd.setCursor(0, 1); 357 lcd.print("Back to the menu"); 358 break; 359 } 360 if ((lapTime) > (6 * laps)) { //back into the same routine 361 started = moment; 362 } 363 } 364 key = state;// keySelection ALARM_ON_OFF,ALARM_SEASON,ALARM_SET,CLOCK_SET,AlARM_AUTO,RUN_CLOCK, 365} 366 367////function setTimerOnOff////switching On Off status as a flip flop 368void setTimerOnOff() { 369 boolean setAlarmOnOff = alarmOnOff; 370 lcd.setCursor(0, 0); 371 lcd.print("switch it now "); 372 lcd.setCursor(0, 1); 373 lcd.print("Timer is: "); 374 lcd.setCursor(11, 1); 375 if (alarmOnOff == false) lcd.print("[off] "); else lcd.print("[on] "); 376 while (!selectButton.getSingleDebouncedPress ()) { 377 if (upButton.getSingleDebouncedPress() || downButton.getSingleDebouncedPress())setAlarmOnOff = !setAlarmOnOff; 378 lcd.setCursor(11, 1); 379 if (setAlarmOnOff == false) lcd.print("[off] "); else lcd.print("[on] "); 380 delay(30); 381 } 382 if (alarmOnOff != setAlarmOnOff) { 383 EEPROM.write(3, setAlarmOnOff); 384 delay(15); 385 } 386} 387 388////function setTimerPlus////expedites the moment of starting the alarm //deze functie functioneert nog niet 389void setTimerPlus() { 390 readAlarm(); 391 int setAlarmPlus = alarmPlus; // tijdelijke variable om opslaan eeprom te laten werken!!!!!!!!!!!!!!!!!! 392 lcd.setCursor(0, 0); 393 lcd.print("stop time: 00:00"); 394 if (alarmHourStop < 10) lcd.setCursor(12, 0); else lcd.setCursor(11, 0); 395 lcd.print(alarmHourStop); 396 if (alarmMinuteStop < 10) lcd.setCursor(15, 0); else lcd.setCursor(14, 0); 397 lcd.print(alarmMinuteStop); 398 lcd.setCursor(0, 1); 399 lcd.print("hours on: "); 400 lcd.setCursor(11, 1); 401 lcd.print(setAlarmPlus); 402 403 while (!selectButton.getSingleDebouncedPress ()) { 404 lcd.setCursor(11, 1); 405 if (alarmHourStop > 6) { // the stopping hour has to be set later then 6 am 406 if (upButton.isPressed() && (setAlarmPlus < 6)) setAlarmPlus++; //The maximum time for the lights to be on is six hours 407 if (downButton.isPressed() && (setAlarmPlus > 1)) setAlarmPlus--; // The minimum time for the hours to be on is one hour 408 lcd.print(setAlarmPlus); 409 } 410 else { 411 lcd.setCursor(0, 0); 412 lcd.print("Set alarm stop "); // in case the alarm is set too early in the day. Minimum after 6 am 413 lcd.setCursor(0, 1); 414 lcd.print("timer first !!!!"); 415 } 416 delay(250); 417 } 418 if (alarmPlus != setAlarmPlus) { 419 EEPROM.write(2, setAlarmPlus); 420 delay(15); 421 } 422} 423 424////function presetClockDate////setting year, month, day, according to the input 425void presetClockDate() { 426 int loops = 20; 427 boolean pBlink = false; 428 const int loopMax = 25; //Moment of refreshing the LCD 429 int loopCheck = 0; //to controll if if there is any interaction 430 enum YMD {YEAR = 1, MONTH, DAY}; 431 432 enum YMD YrMoDy = YEAR; 433 setYr = (yr - 2000); setMo = mo; setDy = dy; setHr = hr; setMn = mn; 434 lcd.setCursor(0, 0); 435 lcd.print("Set Year // Date"); 436 437 while (loopCheck < loopCheckMax) { //fast running part of the loop 438 delay(10); 439 loops++; 440 if (YrMoDy == YEAR) { 441 setYear(); 442 loopCheck = 0; 443 } 444 if (YrMoDy == MONTH) { 445 setMonth(); 446 loopCheck = 0; 447 } 448 if (YrMoDy == DAY) { 449 setDay(); 450 loopCheck = 0; 451 } 452 if (YrMoDy > DAY) loopCheck = loopCheckMax; 453 if (nextButton.getSingleDebouncedPress()) YrMoDy = YrMoDy + 1; 454 if (loops > loopMax) { // displaying whit a long interval 455 lcd.setCursor(0, 1); 456 lcd.print("Y-00 Mo-00 Dy-00"); 457 if (setYr < 10) { 458 lcd.setCursor(2, 1); 459 lcd.print("00"); 460 lcd.setCursor(3, 1); 461 } else lcd.setCursor(2, 1); 462 lcd.print(setYr); 463 if (setMo < 10) { 464 lcd.setCursor(8, 1); 465 lcd.print("00"); 466 lcd.setCursor(9, 1); 467 } 468 else lcd.setCursor(8, 1); 469 lcd.print(setMo); 470 if (setDy < 10) { 471 lcd.setCursor(14, 1); 472 lcd.print("00"); 473 lcd.setCursor(15, 1); 474 } 475 else lcd.setCursor(14, 1); 476 lcd.print(setDy); 477 478 479 if (YrMoDy == YEAR)lcd.setCursor(2, 1); 480 if (YrMoDy == MONTH)lcd.setCursor(8, 1); 481 if (YrMoDy == DAY)lcd.setCursor(14, 1); 482 if (pBlink)lcd.print(" "); 483 484 pBlink = !pBlink; 485 loops = 0; 486 loopCheck++; 487 } 488 if (selectButton.getSingleDebouncedPress ()) { //breaks the loop by the select button or by time lapsed 489 loopCheck = loopCheckMax; 490 } 491 delay(10); 492 } 493 lcd.setCursor(0, 0); 494 lcd.print(" "); 495 lcd.setCursor(0, 1); 496 lcd.print(" "); 497} 498 499////function presetClockTime////setting hour and minute according to the input 500void presetClockTime(int StartingPointHour) { 501 int loops = 20; 502 boolean pBlink = false; 503 const int loopMax = 25; //Moment of refreshing the LCD 504 int loopCheck = 0; //to controll if if there is any interaction 505 // setHr = hr; setMn = mn; 506 enum HM {HOUR = 1, MINUTE}; 507 enum HM HrMn = HOUR; 508 509 while (loopCheck < loopCheckMax) { //fast running part of the loop 510 delay(10); 511 loops++; 512 if (HrMn == HOUR) { //Setting hour function 513 setHr = hourReturn(setHr, StartingPointHour); 514 delay(10); 515 loopCheck = 0; 516 } 517 if (HrMn == MINUTE) { //Setting minute function 518 setMn = minuteReturn(setMn); 519 delay(10); 520 loopCheck = 0; 521 } 522 if (HrMn > MINUTE)loopCheck = loopCheckMax; 523 if (nextButton.getSingleDebouncedPress()) HrMn = HrMn + 1; 524 if (loops > loopMax) { // displaying whit a long interval 525 lcd.setCursor(0, 1); 526 lcd.print("hour:00 minu:00"); 527 if (setHr < 10) { 528 lcd.setCursor(5, 1); 529 lcd.print("00"); 530 lcd.setCursor(6, 1); 531 } else lcd.setCursor(5, 1); 532 lcd.print(setHr); 533 if (setMn < 10) { 534 lcd.setCursor(14, 1); 535 lcd.print("00"); 536 lcd.setCursor(15, 1); 537 } 538 else lcd.setCursor(14, 1); 539 lcd.print(setMn); 540 if (HrMn == HOUR)lcd.setCursor(5, 1); 541 if (HrMn == MINUTE)lcd.setCursor(14, 1); 542 if (pBlink)lcd.print(" "); 543 544 pBlink = !pBlink; 545 loops = 0; 546 loopCheck++; 547 } 548 if (selectButton.getSingleDebouncedPress ()) { //breaks the loop by the select button or by time lapsed 549 loopCheck = loopCheckMax; 550 } 551 delay(10); 552 } 553 lcd.setCursor(0, 0); 554 lcd.print(" "); 555 lcd.setCursor(0, 1); 556 lcd.print(" "); 557} 558 559////function lastDayOfTheMonth////days per month Leap Year included 560int lastDayOfTheMonth(int setMonth) { 561 switch (setMonth) { 562 case 1: //January 563 return 31; 564 break; 565 case 2: //February 566 if ((setYr % 4) == 0) return 29; else return 28; 567 break; 568 case 3: //March 569 return 31; 570 break; 571 case 4: //April 572 return 30; 573 break; 574 case 5: //May 575 return 31; 576 break; 577 case 6: //June 578 return 30; 579 break; 580 case 7: //Juli 581 return 31; 582 break; 583 case 8: //August 584 return 31; 585 break; 586 case 9: //September 587 return 30; 588 break; 589 case 10: //October 590 return 31; 591 break; 592 case 11: //November 593 return 30; 594 break; 595 case 12: //December 596 return 31; 597 break; 598 } 599} 600 601////function setYear// 602void setYear() { 603 int teller = 0; 604 if (setYr < 18) setYr = 17; //to set the minimum year 605 while (upButton.isPressed()) { 606 if (setYr < 99) setYr++; 607 lcd.setCursor(2, 1); 608 lcd.print(setYr); 609 teller++; 610 if (teller > 7) delay(100); else delay(250); 611 } 612 while (downButton.isPressed()) { 613 if (setYr > 17) setYr--; 614 lcd.setCursor(2, 1); 615 lcd.print(setYr); 616 teller++; 617 if (teller > 7) delay(100); else delay(250); 618 } 619} 620 621////function //setMonth 622void setMonth() { 623 while (upButton.isPressed()) { 624 if (setMo < 12) setMo++; 625 lcd.setCursor(8, 1); 626 if (setMo < 10) { 627 lcd.setCursor(8, 1); 628 lcd.print("00"); 629 lcd.setCursor(9, 1); 630 } 631 lcd.print(setMo); 632 delay(250); 633 } 634 while (downButton.isPressed()) { 635 if (setMo > 1) setMo--; 636 lcd.setCursor(8, 1); 637 if (setMo < 10) { 638 lcd.setCursor(8, 1); 639 lcd.print("00"); 640 lcd.setCursor(9, 1); 641 } 642 lcd.print(setMo); 643 delay(250); 644 } 645} 646 647////function //setDay 648void setDay() { 649 int teller = 0; 650 int maxD = maxDay(); 651 while (upButton.isPressed()) { 652 if (setDy < maxD) setDy++; 653 lcd.setCursor(14, 1); 654 if (setDy < 10) { 655 lcd.setCursor(14, 1); 656 lcd.print("00"); 657 lcd.setCursor(15, 1); 658 } 659 lcd.print(setDy); 660 teller++; 661 if (teller > 7) delay(100); else delay(250); 662 } 663 664 while (downButton.isPressed()) { 665 if (setDy > 1) setDy--; 666 lcd.setCursor(14, 1); 667 if (setDy < 10) { 668 lcd.setCursor(14, 1); 669 lcd.print("00"); 670 lcd.setCursor(15, 1); 671 } 672 lcd.print(setDy); 673 teller++; 674 if (teller > 7) delay(100); else delay(250); 675 } 676} 677 678////function //last day of the month 679int maxDay() { 680 switch (setMo) { 681 case 1: 682 return 31; 683 case 2: 684 if ((setYr % 4) == 0)return 29; else return 28; 685 case 3: 686 return 31; 687 case 4: 688 return 30; 689 case 5: 690 return 31; 691 case 6: 692 return 30; 693 case 7: 694 return 31; 695 case 8: 696 return 31; 697 case 9: 698 return 30; 699 case 10: 700 return 31; 701 case 11: 702 return 30; 703 case 12: 704 return 31; 705 } 706} 707 708////function hourReturn 709int hourReturn(int setHour, int startingPoint) { 710 if (setHour < startingPoint) setHour = startingPoint; // to set the minimum hour according to set clock or set alarm 711 while (upButton.isPressed()) { 712 if (setHour < 24) setHour++; 713 lcd.setCursor(5, 1); 714 if (setHour < 10) { 715 lcd.setCursor(5, 1); 716 lcd.print("00"); 717 lcd.setCursor(6, 1); 718 } 719 lcd.print(setHour); 720 delay(200); 721 } 722 while (downButton.isPressed()) { 723 if (setHour > startingPoint) setHour--; 724 lcd.setCursor(5, 1); 725 if (setHour < 10) { 726 lcd.setCursor(5, 1); 727 lcd.print("00"); 728 lcd.setCursor(6, 1); 729 } 730 lcd.print(setHour); 731 delay(200); 732 } 733 return setHour; 734} 735 736////function minute Return 737int minuteReturn(int setMinute) { 738 while (upButton.isPressed()) { 739 if (setMinute < 60) setMinute++; 740 lcd.setCursor(14, 1); 741 if (setMinute < 10) { 742 lcd.setCursor(14, 1); 743 lcd.print("00"); 744 lcd.setCursor(15, 1); 745 } 746 lcd.print(setMinute); 747 delay(200); 748 } 749 while (downButton.isPressed()) { 750 if (setMinute > 1) setMinute--; 751 lcd.setCursor(14, 1); 752 if (setMinute < 10) { 753 lcd.setCursor(14, 1); 754 lcd.print("00"); 755 lcd.setCursor(15, 1); 756 } 757 lcd.print(setMinute); 758 delay(200); 759 } 760 return setMinute; 761} 762 763////function save clock // saves the clock settings into the 1307 clock module 764void saveClock() { 765 boolean saveNew = true; 766 lcd.setCursor(0, 1); 767 lcd.print("00:00 00/00/2000"); //setHr is still two digets 768 if (setHr < 10) lcd.setCursor(1, 1); else lcd.setCursor(0, 1); 769 lcd.print(setHr); 770 if (setMn < 10) lcd.setCursor(4, 1); else lcd.setCursor(3, 1); 771 lcd.print(setMn); 772 if (setDy < 10) lcd.setCursor(7, 1); else lcd.setCursor(6, 1); 773 lcd.print(setDy); 774 if (setMo < 10) lcd.setCursor(10, 1); else lcd.setCursor(9, 1); 775 lcd.print(setMo); 776 lcd.setCursor(14, 1); 777 lcd.print(setYr); 778 lcd.setCursor(0, 0); 779 lcd.print("save clock?: yes"); 780 while (!selectButton.getSingleDebouncedPress ()) { 781 lcd.setCursor(13, 0); 782 if (saveNew == false) lcd.print("no "); else lcd.print("yes"); 783 if ((upButton.getSingleDebouncedPress() == 1) || (downButton.getSingleDebouncedPress() == 1))saveNew = !saveNew;; 784 delay(80); 785 } 786 if (saveNew = true) { 787 int setSec = 0; 788 int setWkDy = 1; 789 delay(100); 790 // The following codes transmits the data to the RTC 791 Wire.beginTransmission(DS1307); 792 Wire.write(byte(0)); 793 Wire.write(decToBcd(setSec)); 794 Wire.write(decToBcd(setMn)); 795 Wire.write(decToBcd(setHr)); 796 Wire.write(decToBcd(setWkDy)); //not in use 797 Wire.write(decToBcd(setDy)); 798 Wire.write(decToBcd(setMo)); 799 Wire.write(decToBcd(setYr)); //has to be two digits 800 Wire.write(byte(0)); 801 Wire.endTransmission(); 802 // Ends transmission of data 803 } 804 delay(100); 805} 806 807////function decToBcd // in use for saving the clock settings 808byte decToBcd(byte val) { 809 return ((val / 10 * 16) + (val % 10)); 810} 811 812////function save alarm // saves the alarm settings into Eeprom 813void saveAlarmSet() { 814 boolean saveNew = true; 815 lcd.setCursor(0, 1); 816 lcd.print("hour:00 minu:00"); 817 if (setHr < 10) { 818 lcd.setCursor(5, 1); 819 lcd.print("00"); 820 lcd.setCursor(6, 1); 821 } else lcd.setCursor(5, 1); 822 lcd.print(setHr); 823 if (setMn < 10) { 824 lcd.setCursor(14, 1); 825 lcd.print("00"); 826 lcd.setCursor(15, 1); 827 } 828 else lcd.setCursor(14, 1); 829 lcd.print(setMn); 830 lcd.setCursor(0, 0); 831 lcd.print("save Alarm?: yes"); 832 while (!selectButton.getSingleDebouncedPress ()) { 833 lcd.setCursor(13, 0); 834 if (saveNew == false) lcd.print("no "); else lcd.print("yes"); 835 if ((upButton.getSingleDebouncedPress() == 1) || (downButton.getSingleDebouncedPress() == 1))saveNew = !saveNew;; // dit nog styleren!!!!!!!!!!!!!!!!!!!!!!!!!!! 836 delay(80); 837 } 838 if (saveNew = true) { 839 EEPROM.write(0, setHr); 840 delay(15); 841 EEPROM.write(1, setMn); 842 delay(15); 843 } 844 delay(100); 845} 846 847////function AlarmAutoManual// setting the alarm on automatic or manual 848void alarmAutoManual() { 849 boolean autoManualNew = autoManual; //using this variable makes the display less restless 850 lcd.setCursor(0, 1); 851 lcd.print("Lights off 00:00"); 852 if (alarmHourStop < 10) { 853 lcd.setCursor(11, 1); 854 lcd.print("00"); 855 lcd.setCursor(12, 1); 856 } else lcd.setCursor(11, 1); 857 lcd.print(alarmHourStop); 858 if (alarmMinuteStop < 10) { 859 lcd.setCursor(14, 1); 860 lcd.print("00"); 861 lcd.setCursor(15, 1); 862 } 863 else lcd.setCursor(14, 1); 864 lcd.print(alarmMinuteStop); 865 lcd.setCursor(0, 0); 866 if (autoManualNew)lcd.print("Timer: automatic"); else lcd.print("Timer: manual "); 867 868 while (!selectButton.getSingleDebouncedPress ()) { 869 if (upButton.getSingleDebouncedPress() || downButton.getSingleDebouncedPress()) { 870 autoManualNew = !autoManualNew; 871 lcd.setCursor(0, 0); 872 if (autoManualNew)lcd.print("Timer: automatic"); else lcd.print("Timer: manual "); 873 } 874 delay(80); 875 } 876 877 if (autoManualNew != autoManual) { 878 EEPROM.write(4, autoManualNew); 879 delay(20); 880 } 881} 882 883//function automatic Timer // walks automaticaly trough the timer settings during the year 884void automaticTimer() { 885 switch (mo) { 886 case 1: 887 alarmOffSet = 6; 888 break; 889 case 2: 890 alarmOffSet = 5; 891 break; 892 case 3: 893 alarmOffSet = 5; 894 break; 895 case 4: 896 alarmOffSet = 4; 897 break; 898 case 5: 899 alarmOffSet = 3; 900 break; 901 case 6: 902 alarmOffSet = 2; 903 break; 904 case 7: 905 alarmOffSet = 2; 906 break; 907 case 8: 908 alarmOffSet = 3; 909 break; 910 case 9: 911 alarmOffSet = 4; 912 break; 913 case 10: 914 alarmOffSet = 5; 915 break; 916 case 11: 917 alarmOffSet = 6; 918 break; 919 case 12: 920 alarmOffSet = 6; 921 break; 922 } 923} 924// function dayLightSaving //Daylight saving periodes for The Netherlands 925void dayLightSaving() { 926 boolean daylightSavingNew = false; 927 928 switch (yr) { 929 case 2017: 930 if ((mo == 3 && dy >= 26) || (mo > 3 && mo < 10) || (mo == 10 && dy < 29)) daylightSavingNew = true; 931 break; 932 case 2018: 933 if ((mo == 3 && dy >= 25) || (mo > 3 && mo < 10) || (mo == 10 && dy < 28)) daylightSavingNew = true; 934 break; 935 case 2019: 936 if ((mo == 3 && dy >= 31) || (mo > 3 && mo < 10) || (mo == 10 && dy < 27)) daylightSavingNew = true; 937 break; 938 case 2020: 939 if ((mo == 3 && dy >= 29) || (mo > 3 && mo < 10) || (mo == 10 && dy < 25)) daylightSavingNew = true; 940 break; 941 case 2021: 942 if ((mo == 3 && dy >= 28) || (mo > 3 && mo < 10) || (mo == 10 && dy < 31)) daylightSavingNew = true; 943 break; 944 case 2022: 945 if ((mo == 3 && dy >= 27) || (mo > 3 && mo < 10) || (mo == 10 && dy < 30)) daylightSavingNew = true; 946 break; 947 case 2023: 948 if ((mo == 3 && dy >= 26) || (mo > 3 && mo < 10) || (mo == 10 && dy < 29)) daylightSavingNew = true; 949 break; 950 case 2024: 951 if ((mo == 3 && dy >= 31) || (mo > 3 && mo < 10) || (mo == 10 && dy < 27)) daylightSavingNew = true; 952 break; 953 case 2025: 954 if ((mo == 3 && dy >= 30) || (mo > 3 && mo < 10) || (mo == 10 && dy < 26)) daylightSavingNew = true; 955 break; 956 case 2026: 957 if ((mo == 3 && dy >= 29) || (mo > 3 && mo < 10) || (mo == 10 && dy < 25)) daylightSavingNew = true; 958 break; 959 case 2027: 960 if ((mo == 3 && dy >= 28) || (mo > 3 && mo < 10) || (mo == 10 && dy < 31)) daylightSavingNew = true; 961 break; 962 case 2028: 963 if ((mo == 3 && dy >= 26) || (mo > 3 && mo < 10) || (mo == 10 && dy < 29)) daylightSavingNew = true; 964 break; 965 case 2029: 966 if ((mo == 3 && dy >= 25) || (mo > 3 && mo < 10) || (mo == 10 && dy < 28)) daylightSavingNew = true; 967 break; 968 } 969 if (daylightSavingNew != daylightSaving) { 970 if (hr > 2) { 971 if (daylightSavingNew) hr = hr + 1; 972 if (!daylightSavingNew) hr = hr - 1; 973 daylightSaving = daylightSavingNew; 974 } 975 } 976} 977
clock alarm timer
arduino
This is a compleet stand alone working clock with alarm and timer switch
1/* 2 Clock and timer developed by Ronald Siemons 3 The sketch is free software and for anybody free to use on his or her own risk, if there would be. 4 5 TO KNOW!!!!!!!!!! 6 Make a choice out of the menu what show up on the display when pushing the select button. Every 7 two secconds an other choice to make will be shown as long as the select button is pushed. By letting it go 8 the last shown choice will be made. 9 10 What it does: 11 The clock and timer switches on and off a relais. 12 The clock is made for stand alone usesage. So one can set the time and the alarm on the device itself 13 without the need of an USB connection to a computer. 14 The general idee: 15 The clock is mate as part of a system to switch on and off the lights in a shop. This part is made 16 for the neon lights at the front of the shop. The lights only work in the evening and go off every day at the same time. 17 So the alarm is set on the off switch of the lights and as the time off lighting up the lights is known the timer works 18 backwords from the known of switching time back during a certain period to the starting moment of the lights. 19 The period of lighting depends on the pereod of the year and can be set by hand or automatic. 20 As it has to work all year round without setting and resetting the system every now and again 21 daylight saving settings ar built in also. Readjustment of the daylight savings has to be done 22 for the specific country it is going to be used. 23*/ 24#include <Pushbutton.h> // part of arduino library. Search in manage libraries. 25#include <EEPROM.h> 26#include <RTClib.h> 27#include <LCD.h> 28#include <LiquidCrystal.h> 29#include <LiquidCrystal_I2C.h> 30#include <Wire.h> 31 32LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address 33RTC_DS1307 RTC; 34 35const int DS1307 = 0x68; // Address of DS1307 see data sheets //?????????? 36const int lightAlarm_pin = 6; 37const int selectButton_pin = 5; 38const int nextButton_pin = 4; 39const int upButton_pin = 3; 40const int downButton_pin = 2; 41const int alarmButton_pin = A1; // gets an other function later 42const boolean alarmSystem_pin = 7; 43const boolean timerLightOnLED_pin = 8; 44 45Pushbutton selectButton(selectButton_pin); //equal to pinMode(selectButton_Pin,INPUT_PULLUP); 46Pushbutton nextButton(nextButton_pin); 47Pushbutton upButton(upButton_pin); 48Pushbutton downButton(downButton_pin); 49Pushbutton alarmButton(alarmButton_pin); 50 51enum mainSelection { 52 ALARM_ON_OFF = 1, //switching alarm on or off = 1 53 ALARM_SEASON, //change the time of starting = 2 54 ALARM_SET, //to set the alarm start and stop time = 3 55 CLOCK_SET, //to set time and date = 4 56 AlARM_AUTO, //Light timer automatic or manual = 5 57 RUN_CLOCK //Reading and displaying time and date = 6 58}; 59enum mainSelection key = RUN_CLOCK; 60 61int alarmMinuteStart = 1; //Time timer starts according to the settings of alarmPlus and alarmxxxStop 62int alarmHourStart = 1; //Time timer starts according to the settings of alarmPlus and alarmxxxStop 63int alarmMinuteStop = 2; //Time the timer has been set to stop 64int alarmHourStop = 2; //Time teh timer has been set to stop Minimum 6 65int alarmPlus = 0; //Working hours of the lights Maximum 6 66boolean alarmOnOff = false; //Turns the alarm On/Off //off is false and on is true 67boolean autoManual = false; //Timer automatic or manual 68int alarmOffSet = 6; 69int yr = 2000; 70int mo = 1; 71int dy = 1; 72int hr = 0; 73int mn = 0; 74int setYr = 00; 75int setMo = 1; 76int setDy = 1; 77int setHr = 0; 78int setMn = 0; 79boolean daylightSaving = true; 80boolean aBlink = true; 81const int loopCheckMax = 80; //duration of the loops in the settings 82int loopCheck = 0; //to controll if if there is any interaction 83unsigned long clockDisplayCheck = millis(); //counter in the loop 84int lcdDisplayCheck = 0; //counter for controling the time the lcd will be on 85boolean timerLight = false; //set the relais for the alarm lights 86int timerLightStage = 0; 87 88 89//((((((((((((((((((((((((((((SETUP)))))))))))))))))))))))))))))))) 90void setup() { 91 pinMode(lightAlarm_pin, OUTPUT); 92 pinMode(alarmSystem_pin, OUTPUT); 93 pinMode(timerLightOnLED_pin, OUTPUT); 94 lcd.begin(16, 2); 95 Wire.begin(); 96 RTC.begin(); 97 Serial.begin(9600); 98 clockRunning(); 99 100 lcd.clear(); 101 lcd.on(); 102 displayLcdFirstLine( 2); 103 readTimeNow(); 104 displayTimeNow(2); 105 readAlarm(); 106} 107//((((((((((((((((((((((((((((----)))))))))))))))))))))))))))))))) 108//***************************^LOOP^******************************* 109void loop() { 110 key = RUN_CLOCK; 111 lightManualyOnOff(); //to switch the relay manualy when the alarm has been turned off 112 lcdLight(); //turns on the lcd on demand 113 if ((millis() - clockDisplayCheck) > 500) //Reading and displaying date and time continuesly every 0.5 second 114 { 115 clockRunning(); 116 clockDisplayCheck = millis(); //sets the timer variable 117 readTimeNow(); //reading time out of the Real Time Clock 118 displayTimeNow(2); //displaying the time now and the digit to blink 119 readAlarm(); //reading the alarm settings out off the Eeprom 120 displayLcdFirstLine( 0); //Basic display line 121 alarmActivate(); //setting the alarm 122 timerLightOnOff(); 123 } 124 125 if (selectButton.getSingleDebouncedPress()) 126 { 127 lcdDisplayCheck = 0; // lcd back to maximum time 128 lcdLight(); //turns lcd on 129 mainKeySelection(); // reads keySelection ALARM_ON_OFF,ALARM_SEASON,ALARM_SET,CLOCK_SET,RUN_CLOCK, 130 // lcd.setCursor(0, 0); 131 132 switch (key) { 133 case ALARM_ON_OFF: 134 setTimerOnOff(); 135 break; 136 137 case ALARM_SEASON: 138 setTimerPlus(); 139 break; 140 141 case ALARM_SET: 142 setHr = alarmHourStop; 143 setMn = alarmMinuteStop; 144 lcd.setCursor(0, 0); 145 lcd.print("Set alarm time "); 146 presetClockTime(alarmOffSet); //Gives an off-set to the alarm stopping time of 6? hours 147 if (setHr == 12) { //to prevent the timer passing mid-night 148 setHr = 11; 149 setMn = 59; 150 } 151 saveAlarmSet(); 152 // readAlarm(); 153 break; 154 155 case CLOCK_SET: 156 presetClockDate(); 157 readTimeNow(); //reading the current time to display in presetClockTime 158 setHr = hr; 159 setMn = mn; 160 lcd.setCursor(0, 0); 161 lcd.print("Set clock time "); 162 presetClockTime(0); //Time can be set starting at zero. The function is also in use to set the alarm stopping time 163 saveClock(); 164 break; 165 166 case AlARM_AUTO: 167 alarmAutoManual(); 168 break; 169 } 170 } 171} 172//***************************~~******************************* 173 174//function clock running// A check about the clock being running 175void clockRunning() { 176 if (!RTC.begin() || !RTC.isrunning()) { 177 while (!selectButton.getSingleDebouncedPress ()) { 178 if (digitalRead(alarmSystem_pin) == LOW) digitalWrite(alarmSystem_pin, HIGH); 179 else digitalWrite(alarmSystem_pin, LOW); 180 delay(100); 181 } 182 } 183} 184 185//function lcdTimerLights // timer for the lcd lcdDisplayCheck incrises with every half a second 186void lcdLight() { 187 if (alarmButton.getSingleDebouncedPress()) lcdDisplayCheck = 0; 188 lcdDisplayCheck++; 189 if (lcdDisplayCheck < 900) { //times half a second the display will be on 190 lcd.display(); 191 lcd.backlight(); 192 } 193 else { 194 lcd.noDisplay(); 195 lcd.noBacklight(); 196 if (lcdDisplayCheck > 2000) lcdDisplayCheck = 900; //resetting the variable to prevent the figure gets out of his boundary while the light stays off 197 } 198} 199 200///function timerLightOnOff // works in automatic and manual to set the pin for the relais 201void timerLightOnOff() { 202 if (timerLight) { 203 digitalWrite(lightAlarm_pin, LOW); // the relay activates to zero 204 digitalWrite(timerLightOnLED_pin, HIGH); 205 } else { 206 digitalWrite(lightAlarm_pin, HIGH); 207 digitalWrite(timerLightOnLED_pin, LOW); 208 } 209 210} 211 212///function lightManualyOnOff // to set the timer relais by hand 213void lightManualyOnOff() { 214 if (!alarmOnOff) { 215 if (nextButton.getSingleDebouncedPress()) { 216 Serial.print (timerLight); 217 timerLight = !timerLight; 218 } 219 } 220} 221////function readTimeNow////reading date and time 222void readTimeNow() { 223 DateTime now = RTC.now(); 224 yr = now.year(); 225 mo = now.month(); 226 dy = now.day(); 227 hr = now.hour(); 228 mn = now.minute(); 229} 230 231///function readAlarm////to read the alarm settings from the Eeprom 232void readAlarm() { 233 alarmHourStop = EEPROM.read(0); 234 delay(15); 235 alarmMinuteStop = EEPROM.read(1); 236 delay(15); 237 alarmPlus = EEPROM.read(2); 238 delay(15); 239 alarmOnOff = EEPROM.read(3); 240 delay(50); 241 autoManual = EEPROM.read(4); 242 delay(50); 243 if (alarmHourStop <= alarmOffSet) alarmHourStop = alarmOffSet + 1; //to prevent the alarm storting before the day has begun 244 alarmHourStart = alarmHourStop - alarmPlus; 245 alarmMinuteStart = alarmMinuteStop;//calculating the timer stopping time 246 if (alarmAutoManual) automaticTimer(); //Sets the timer automaticaly in the preset time scedule 247 dayLightSaving(); 248} 249 250void alarmActivate() { 251 if (alarmOnOff) { //Checking if the alarm has to be activated 252 253 if ((hr - alarmHourStart) == 0) 254 { 255 if (mn >= alarmMinuteStart) 256 timerLight = true; 257 else timerLight = false; 258 } 259 else if (hr - alarmHourStop == 0) 260 { 261 if ( mn <= alarmMinuteStop) 262 timerLight = true; 263 else timerLight = false; 264 } 265 else if ((alarmHourStop - hr < alarmPlus) && (alarmHourStop - hr >= 1)) timerLight = true; 266 else timerLight = false; 267 268 } 269 // else timerLight = false; // has to e removed ??????????????????????????????????? 270} 271 272////function display time now////display time 273void displayTimeNow(int digitBlink) { 274 char textDT[16]; 275 sprintf(textDT, "%02d:%02d %02d/%02d/%04d", hr, mn, dy, mo, yr); //puts int variabels in the rigth format into textDT 276 lcd.setCursor(0, 1); 277 lcd.print(textDT); //prints in four and two digits 278 lcd.setCursor(digitBlink, 1); 279 if (aBlink) lcd.print(" "); else lcd.print(":"); 280 aBlink = !aBlink; 281} 282 283 284////function displayLcdFirstLine////displays the first line 285void displayLcdFirstLine( int blinking) { //blinking 0 is back to RUN position 286 287 if (blinking > 0) { 288 for (int i = 1; i <= blinking; i++) { 289 lcd.setCursor(0, 0); 290 lcd.print(" "); 291 lcd.setCursor(0, 0); 292 delay(400); 293 lcd.print("TIMER-1.00 "); //Titel 294 lcd.setCursor(11, 0); 295 if (alarmOnOff == false) lcd.print("off~ "); else lcd.print(" on~ "); 296 lcd.setCursor(15, 0); 297 if (autoManual) lcd.print("A"); else lcd.print(alarmPlus); 298 299 delay(400); 300 } 301 } else { 302 lcd.setCursor(0, 0); 303 lcd.print("TIMER-1.00"); //Titel 304 lcd.setCursor(10, 0); 305 if (alarmOnOff == false) lcd.print(" off~ "); else lcd.print(" on~ "); 306 lcd.setCursor(15, 0); 307 if (autoManual) lcd.print("A"); else lcd.print(alarmPlus); 308 delay(400); 309 } 310} 311 312 313 314////function mainKeySelection////make a selection to set timer on/off, alarm season, alarm set, clock set 315void mainKeySelection() { 316 unsigned long started = millis(); 317 delay(10); 318 unsigned long moment; 319 const int laps = 2000; 320 int state = 0; 321 int lapTime = 0; 322 323 while (selectButton.isPressed()) { 324 moment = millis(); 325 delay(100); 326 lapTime = moment - started; 327 if (((lapTime) >= (0 * laps)) && ((lapTime) <= (1 * laps))) state = 1; 328 else if (((lapTime) >= (1 * laps)) && ((lapTime) <= (2 * laps))) state = 2; 329 else if (((lapTime) >= (2 * laps)) && ((lapTime) <= (3 * laps))) state = 3; 330 else if (((lapTime) >= (3 * laps)) && ((lapTime) <= (4 * laps))) state = 4; 331 else if (((lapTime) >= (4 * laps)) && ((lapTime) <= (5 * laps))) state = 5; 332 else if (((lapTime) >= (5 * laps)) && ((lapTime) <= (6 * laps))) state = 6; 333 delay(500); 334 switch (state) { 335 case 1: 336 lcd.setCursor(0, 1); 337 lcd.print("set alarm on/off"); 338 break; 339 case 2: 340 lcd.setCursor(0, 1); 341 lcd.print("Timer hours set "); 342 break; 343 case 3: 344 lcd.setCursor(0, 1); 345 lcd.print("Alarm time set! "); 346 break; 347 case 4: 348 lcd.setCursor(0, 1); 349 lcd.print("Clock time set! "); 350 break; 351 case 5: 352 lcd.setCursor(0, 1); 353 lcd.print("Alarm man/auto"); 354 break; 355 case 6: 356 lcd.setCursor(0, 1); 357 lcd.print("Back to the menu"); 358 break; 359 } 360 if ((lapTime) > (6 * laps)) { //back into the same routine 361 started = moment; 362 } 363 } 364 key = state;// keySelection ALARM_ON_OFF,ALARM_SEASON,ALARM_SET,CLOCK_SET,AlARM_AUTO,RUN_CLOCK, 365} 366 367////function setTimerOnOff////switching On Off status as a flip flop 368void setTimerOnOff() { 369 boolean setAlarmOnOff = alarmOnOff; 370 lcd.setCursor(0, 0); 371 lcd.print("switch it now "); 372 lcd.setCursor(0, 1); 373 lcd.print("Timer is: "); 374 lcd.setCursor(11, 1); 375 if (alarmOnOff == false) lcd.print("[off] "); else lcd.print("[on] "); 376 while (!selectButton.getSingleDebouncedPress ()) { 377 if (upButton.getSingleDebouncedPress() || downButton.getSingleDebouncedPress())setAlarmOnOff = !setAlarmOnOff; 378 lcd.setCursor(11, 1); 379 if (setAlarmOnOff == false) lcd.print("[off] "); else lcd.print("[on] "); 380 delay(30); 381 } 382 if (alarmOnOff != setAlarmOnOff) { 383 EEPROM.write(3, setAlarmOnOff); 384 delay(15); 385 } 386} 387 388////function setTimerPlus////expedites the moment of starting the alarm //deze functie functioneert nog niet 389void setTimerPlus() { 390 readAlarm(); 391 int setAlarmPlus = alarmPlus; // tijdelijke variable om opslaan eeprom te laten werken!!!!!!!!!!!!!!!!!! 392 lcd.setCursor(0, 0); 393 lcd.print("stop time: 00:00"); 394 if (alarmHourStop < 10) lcd.setCursor(12, 0); else lcd.setCursor(11, 0); 395 lcd.print(alarmHourStop); 396 if (alarmMinuteStop < 10) lcd.setCursor(15, 0); else lcd.setCursor(14, 0); 397 lcd.print(alarmMinuteStop); 398 lcd.setCursor(0, 1); 399 lcd.print("hours on: "); 400 lcd.setCursor(11, 1); 401 lcd.print(setAlarmPlus); 402 403 while (!selectButton.getSingleDebouncedPress ()) { 404 lcd.setCursor(11, 1); 405 if (alarmHourStop > 6) { // the stopping hour has to be set later then 6 am 406 if (upButton.isPressed() && (setAlarmPlus < 6)) setAlarmPlus++; //The maximum time for the lights to be on is six hours 407 if (downButton.isPressed() && (setAlarmPlus > 1)) setAlarmPlus--; // The minimum time for the hours to be on is one hour 408 lcd.print(setAlarmPlus); 409 } 410 else { 411 lcd.setCursor(0, 0); 412 lcd.print("Set alarm stop "); // in case the alarm is set too early in the day. Minimum after 6 am 413 lcd.setCursor(0, 1); 414 lcd.print("timer first !!!!"); 415 } 416 delay(250); 417 } 418 if (alarmPlus != setAlarmPlus) { 419 EEPROM.write(2, setAlarmPlus); 420 delay(15); 421 } 422} 423 424////function presetClockDate////setting year, month, day, according to the input 425void presetClockDate() { 426 int loops = 20; 427 boolean pBlink = false; 428 const int loopMax = 25; //Moment of refreshing the LCD 429 int loopCheck = 0; //to controll if if there is any interaction 430 enum YMD {YEAR = 1, MONTH, DAY}; 431 432 enum YMD YrMoDy = YEAR; 433 setYr = (yr - 2000); setMo = mo; setDy = dy; setHr = hr; setMn = mn; 434 lcd.setCursor(0, 0); 435 lcd.print("Set Year // Date"); 436 437 while (loopCheck < loopCheckMax) { //fast running part of the loop 438 delay(10); 439 loops++; 440 if (YrMoDy == YEAR) { 441 setYear(); 442 loopCheck = 0; 443 } 444 if (YrMoDy == MONTH) { 445 setMonth(); 446 loopCheck = 0; 447 } 448 if (YrMoDy == DAY) { 449 setDay(); 450 loopCheck = 0; 451 } 452 if (YrMoDy > DAY) loopCheck = loopCheckMax; 453 if (nextButton.getSingleDebouncedPress()) YrMoDy = YrMoDy + 1; 454 if (loops > loopMax) { // displaying whit a long interval 455 lcd.setCursor(0, 1); 456 lcd.print("Y-00 Mo-00 Dy-00"); 457 if (setYr < 10) { 458 lcd.setCursor(2, 1); 459 lcd.print("00"); 460 lcd.setCursor(3, 1); 461 } else lcd.setCursor(2, 1); 462 lcd.print(setYr); 463 if (setMo < 10) { 464 lcd.setCursor(8, 1); 465 lcd.print("00"); 466 lcd.setCursor(9, 1); 467 } 468 else lcd.setCursor(8, 1); 469 lcd.print(setMo); 470 if (setDy < 10) { 471 lcd.setCursor(14, 1); 472 lcd.print("00"); 473 lcd.setCursor(15, 1); 474 } 475 else lcd.setCursor(14, 1); 476 lcd.print(setDy); 477 478 479 if (YrMoDy == YEAR)lcd.setCursor(2, 1); 480 if (YrMoDy == MONTH)lcd.setCursor(8, 1); 481 if (YrMoDy == DAY)lcd.setCursor(14, 1); 482 if (pBlink)lcd.print(" "); 483 484 pBlink = !pBlink; 485 loops = 0; 486 loopCheck++; 487 } 488 if (selectButton.getSingleDebouncedPress ()) { //breaks the loop by the select button or by time lapsed 489 loopCheck = loopCheckMax; 490 } 491 delay(10); 492 } 493 lcd.setCursor(0, 0); 494 lcd.print(" "); 495 lcd.setCursor(0, 1); 496 lcd.print(" "); 497} 498 499////function presetClockTime////setting hour and minute according to the input 500void presetClockTime(int StartingPointHour) { 501 int loops = 20; 502 boolean pBlink = false; 503 const int loopMax = 25; //Moment of refreshing the LCD 504 int loopCheck = 0; //to controll if if there is any interaction 505 // setHr = hr; setMn = mn; 506 enum HM {HOUR = 1, MINUTE}; 507 enum HM HrMn = HOUR; 508 509 while (loopCheck < loopCheckMax) { //fast running part of the loop 510 delay(10); 511 loops++; 512 if (HrMn == HOUR) { //Setting hour function 513 setHr = hourReturn(setHr, StartingPointHour); 514 delay(10); 515 loopCheck = 0; 516 } 517 if (HrMn == MINUTE) { //Setting minute function 518 setMn = minuteReturn(setMn); 519 delay(10); 520 loopCheck = 0; 521 } 522 if (HrMn > MINUTE)loopCheck = loopCheckMax; 523 if (nextButton.getSingleDebouncedPress()) HrMn = HrMn + 1; 524 if (loops > loopMax) { // displaying whit a long interval 525 lcd.setCursor(0, 1); 526 lcd.print("hour:00 minu:00"); 527 if (setHr < 10) { 528 lcd.setCursor(5, 1); 529 lcd.print("00"); 530 lcd.setCursor(6, 1); 531 } else lcd.setCursor(5, 1); 532 lcd.print(setHr); 533 if (setMn < 10) { 534 lcd.setCursor(14, 1); 535 lcd.print("00"); 536 lcd.setCursor(15, 1); 537 } 538 else lcd.setCursor(14, 1); 539 lcd.print(setMn); 540 if (HrMn == HOUR)lcd.setCursor(5, 1); 541 if (HrMn == MINUTE)lcd.setCursor(14, 1); 542 if (pBlink)lcd.print(" "); 543 544 pBlink = !pBlink; 545 loops = 0; 546 loopCheck++; 547 } 548 if (selectButton.getSingleDebouncedPress ()) { //breaks the loop by the select button or by time lapsed 549 loopCheck = loopCheckMax; 550 } 551 delay(10); 552 } 553 lcd.setCursor(0, 0); 554 lcd.print(" "); 555 lcd.setCursor(0, 1); 556 lcd.print(" "); 557} 558 559////function lastDayOfTheMonth////days per month Leap Year included 560int lastDayOfTheMonth(int setMonth) { 561 switch (setMonth) { 562 case 1: //January 563 return 31; 564 break; 565 case 2: //February 566 if ((setYr % 4) == 0) return 29; else return 28; 567 break; 568 case 3: //March 569 return 31; 570 break; 571 case 4: //April 572 return 30; 573 break; 574 case 5: //May 575 return 31; 576 break; 577 case 6: //June 578 return 30; 579 break; 580 case 7: //Juli 581 return 31; 582 break; 583 case 8: //August 584 return 31; 585 break; 586 case 9: //September 587 return 30; 588 break; 589 case 10: //October 590 return 31; 591 break; 592 case 11: //November 593 return 30; 594 break; 595 case 12: //December 596 return 31; 597 break; 598 } 599} 600 601////function setYear// 602void setYear() { 603 int teller = 0; 604 if (setYr < 18) setYr = 17; //to set the minimum year 605 while (upButton.isPressed()) { 606 if (setYr < 99) setYr++; 607 lcd.setCursor(2, 1); 608 lcd.print(setYr); 609 teller++; 610 if (teller > 7) delay(100); else delay(250); 611 } 612 while (downButton.isPressed()) { 613 if (setYr > 17) setYr--; 614 lcd.setCursor(2, 1); 615 lcd.print(setYr); 616 teller++; 617 if (teller > 7) delay(100); else delay(250); 618 } 619} 620 621////function //setMonth 622void setMonth() { 623 while (upButton.isPressed()) { 624 if (setMo < 12) setMo++; 625 lcd.setCursor(8, 1); 626 if (setMo < 10) { 627 lcd.setCursor(8, 1); 628 lcd.print("00"); 629 lcd.setCursor(9, 1); 630 } 631 lcd.print(setMo); 632 delay(250); 633 } 634 while (downButton.isPressed()) { 635 if (setMo > 1) setMo--; 636 lcd.setCursor(8, 1); 637 if (setMo < 10) { 638 lcd.setCursor(8, 1); 639 lcd.print("00"); 640 lcd.setCursor(9, 1); 641 } 642 lcd.print(setMo); 643 delay(250); 644 } 645} 646 647////function //setDay 648void setDay() { 649 int teller = 0; 650 int maxD = maxDay(); 651 while (upButton.isPressed()) { 652 if (setDy < maxD) setDy++; 653 lcd.setCursor(14, 1); 654 if (setDy < 10) { 655 lcd.setCursor(14, 1); 656 lcd.print("00"); 657 lcd.setCursor(15, 1); 658 } 659 lcd.print(setDy); 660 teller++; 661 if (teller > 7) delay(100); else delay(250); 662 } 663 664 while (downButton.isPressed()) { 665 if (setDy > 1) setDy--; 666 lcd.setCursor(14, 1); 667 if (setDy < 10) { 668 lcd.setCursor(14, 1); 669 lcd.print("00"); 670 lcd.setCursor(15, 1); 671 } 672 lcd.print(setDy); 673 teller++; 674 if (teller > 7) delay(100); else delay(250); 675 } 676} 677 678////function //last day of the month 679int maxDay() { 680 switch (setMo) { 681 case 1: 682 return 31; 683 case 2: 684 if ((setYr % 4) == 0)return 29; else return 28; 685 case 3: 686 return 31; 687 case 4: 688 return 30; 689 case 5: 690 return 31; 691 case 6: 692 return 30; 693 case 7: 694 return 31; 695 case 8: 696 return 31; 697 case 9: 698 return 30; 699 case 10: 700 return 31; 701 case 11: 702 return 30; 703 case 12: 704 return 31; 705 } 706} 707 708////function hourReturn 709int hourReturn(int setHour, int startingPoint) { 710 if (setHour < startingPoint) setHour = startingPoint; // to set the minimum hour according to set clock or set alarm 711 while (upButton.isPressed()) { 712 if (setHour < 24) setHour++; 713 lcd.setCursor(5, 1); 714 if (setHour < 10) { 715 lcd.setCursor(5, 1); 716 lcd.print("00"); 717 lcd.setCursor(6, 1); 718 } 719 lcd.print(setHour); 720 delay(200); 721 } 722 while (downButton.isPressed()) { 723 if (setHour > startingPoint) setHour--; 724 lcd.setCursor(5, 1); 725 if (setHour < 10) { 726 lcd.setCursor(5, 1); 727 lcd.print("00"); 728 lcd.setCursor(6, 1); 729 } 730 lcd.print(setHour); 731 delay(200); 732 } 733 return setHour; 734} 735 736////function minute Return 737int minuteReturn(int setMinute) { 738 while (upButton.isPressed()) { 739 if (setMinute < 60) setMinute++; 740 lcd.setCursor(14, 1); 741 if (setMinute < 10) { 742 lcd.setCursor(14, 1); 743 lcd.print("00"); 744 lcd.setCursor(15, 1); 745 } 746 lcd.print(setMinute); 747 delay(200); 748 } 749 while (downButton.isPressed()) { 750 if (setMinute > 1) setMinute--; 751 lcd.setCursor(14, 1); 752 if (setMinute < 10) { 753 lcd.setCursor(14, 1); 754 lcd.print("00"); 755 lcd.setCursor(15, 1); 756 } 757 lcd.print(setMinute); 758 delay(200); 759 } 760 return setMinute; 761} 762 763////function save clock // saves the clock settings into the 1307 clock module 764void saveClock() { 765 boolean saveNew = true; 766 lcd.setCursor(0, 1); 767 lcd.print("00:00 00/00/2000"); //setHr is still two digets 768 if (setHr < 10) lcd.setCursor(1, 1); else lcd.setCursor(0, 1); 769 lcd.print(setHr); 770 if (setMn < 10) lcd.setCursor(4, 1); else lcd.setCursor(3, 1); 771 lcd.print(setMn); 772 if (setDy < 10) lcd.setCursor(7, 1); else lcd.setCursor(6, 1); 773 lcd.print(setDy); 774 if (setMo < 10) lcd.setCursor(10, 1); else lcd.setCursor(9, 1); 775 lcd.print(setMo); 776 lcd.setCursor(14, 1); 777 lcd.print(setYr); 778 lcd.setCursor(0, 0); 779 lcd.print("save clock?: yes"); 780 while (!selectButton.getSingleDebouncedPress ()) { 781 lcd.setCursor(13, 0); 782 if (saveNew == false) lcd.print("no "); else lcd.print("yes"); 783 if ((upButton.getSingleDebouncedPress() == 1) || (downButton.getSingleDebouncedPress() == 1))saveNew = !saveNew;; 784 delay(80); 785 } 786 if (saveNew = true) { 787 int setSec = 0; 788 int setWkDy = 1; 789 delay(100); 790 // The following codes transmits the data to the RTC 791 Wire.beginTransmission(DS1307); 792 Wire.write(byte(0)); 793 Wire.write(decToBcd(setSec)); 794 Wire.write(decToBcd(setMn)); 795 Wire.write(decToBcd(setHr)); 796 Wire.write(decToBcd(setWkDy)); //not in use 797 Wire.write(decToBcd(setDy)); 798 Wire.write(decToBcd(setMo)); 799 Wire.write(decToBcd(setYr)); //has to be two digits 800 Wire.write(byte(0)); 801 Wire.endTransmission(); 802 // Ends transmission of data 803 } 804 delay(100); 805} 806 807////function decToBcd // in use for saving the clock settings 808byte decToBcd(byte val) { 809 return ((val / 10 * 16) + (val % 10)); 810} 811 812////function save alarm // saves the alarm settings into Eeprom 813void saveAlarmSet() { 814 boolean saveNew = true; 815 lcd.setCursor(0, 1); 816 lcd.print("hour:00 minu:00"); 817 if (setHr < 10) { 818 lcd.setCursor(5, 1); 819 lcd.print("00"); 820 lcd.setCursor(6, 1); 821 } else lcd.setCursor(5, 1); 822 lcd.print(setHr); 823 if (setMn < 10) { 824 lcd.setCursor(14, 1); 825 lcd.print("00"); 826 lcd.setCursor(15, 1); 827 } 828 else lcd.setCursor(14, 1); 829 lcd.print(setMn); 830 lcd.setCursor(0, 0); 831 lcd.print("save Alarm?: yes"); 832 while (!selectButton.getSingleDebouncedPress ()) { 833 lcd.setCursor(13, 0); 834 if (saveNew == false) lcd.print("no "); else lcd.print("yes"); 835 if ((upButton.getSingleDebouncedPress() == 1) || (downButton.getSingleDebouncedPress() == 1))saveNew = !saveNew;; // dit nog styleren!!!!!!!!!!!!!!!!!!!!!!!!!!! 836 delay(80); 837 } 838 if (saveNew = true) { 839 EEPROM.write(0, setHr); 840 delay(15); 841 EEPROM.write(1, setMn); 842 delay(15); 843 } 844 delay(100); 845} 846 847////function AlarmAutoManual// setting the alarm on automatic or manual 848void alarmAutoManual() { 849 boolean autoManualNew = autoManual; //using this variable makes the display less restless 850 lcd.setCursor(0, 1); 851 lcd.print("Lights off 00:00"); 852 if (alarmHourStop < 10) { 853 lcd.setCursor(11, 1); 854 lcd.print("00"); 855 lcd.setCursor(12, 1); 856 } else lcd.setCursor(11, 1); 857 lcd.print(alarmHourStop); 858 if (alarmMinuteStop < 10) { 859 lcd.setCursor(14, 1); 860 lcd.print("00"); 861 lcd.setCursor(15, 1); 862 } 863 else lcd.setCursor(14, 1); 864 lcd.print(alarmMinuteStop); 865 lcd.setCursor(0, 0); 866 if (autoManualNew)lcd.print("Timer: automatic"); else lcd.print("Timer: manual "); 867 868 while (!selectButton.getSingleDebouncedPress ()) { 869 if (upButton.getSingleDebouncedPress() || downButton.getSingleDebouncedPress()) { 870 autoManualNew = !autoManualNew; 871 lcd.setCursor(0, 0); 872 if (autoManualNew)lcd.print("Timer: automatic"); else lcd.print("Timer: manual "); 873 } 874 delay(80); 875 } 876 877 if (autoManualNew != autoManual) { 878 EEPROM.write(4, autoManualNew); 879 delay(20); 880 } 881} 882 883//function automatic Timer // walks automaticaly trough the timer settings during the year 884void automaticTimer() { 885 switch (mo) { 886 case 1: 887 alarmOffSet = 6; 888 break; 889 case 2: 890 alarmOffSet = 5; 891 break; 892 case 3: 893 alarmOffSet = 5; 894 break; 895 case 4: 896 alarmOffSet = 4; 897 break; 898 case 5: 899 alarmOffSet = 3; 900 break; 901 case 6: 902 alarmOffSet = 2; 903 break; 904 case 7: 905 alarmOffSet = 2; 906 break; 907 case 8: 908 alarmOffSet = 3; 909 break; 910 case 9: 911 alarmOffSet = 4; 912 break; 913 case 10: 914 alarmOffSet = 5; 915 break; 916 case 11: 917 alarmOffSet = 6; 918 break; 919 case 12: 920 alarmOffSet = 6; 921 break; 922 } 923} 924// function dayLightSaving //Daylight saving periodes for The Netherlands 925void dayLightSaving() { 926 boolean daylightSavingNew = false; 927 928 switch (yr) { 929 case 2017: 930 if ((mo == 3 && dy >= 26) || (mo > 3 && mo < 10) || (mo == 10 && dy < 29)) daylightSavingNew = true; 931 break; 932 case 2018: 933 if ((mo == 3 && dy >= 25) || (mo > 3 && mo < 10) || (mo == 10 && dy < 28)) daylightSavingNew = true; 934 break; 935 case 2019: 936 if ((mo == 3 && dy >= 31) || (mo > 3 && mo < 10) || (mo == 10 && dy < 27)) daylightSavingNew = true; 937 break; 938 case 2020: 939 if ((mo == 3 && dy >= 29) || (mo > 3 && mo < 10) || (mo == 10 && dy < 25)) daylightSavingNew = true; 940 break; 941 case 2021: 942 if ((mo == 3 && dy >= 28) || (mo > 3 && mo < 10) || (mo == 10 && dy < 31)) daylightSavingNew = true; 943 break; 944 case 2022: 945 if ((mo == 3 && dy >= 27) || (mo > 3 && mo < 10) || (mo == 10 && dy < 30)) daylightSavingNew = true; 946 break; 947 case 2023: 948 if ((mo == 3 && dy >= 26) || (mo > 3 && mo < 10) || (mo == 10 && dy < 29)) daylightSavingNew = true; 949 break; 950 case 2024: 951 if ((mo == 3 && dy >= 31) || (mo > 3 && mo < 10) || (mo == 10 && dy < 27)) daylightSavingNew = true; 952 break; 953 case 2025: 954 if ((mo == 3 && dy >= 30) || (mo > 3 && mo < 10) || (mo == 10 && dy < 26)) daylightSavingNew = true; 955 break; 956 case 2026: 957 if ((mo == 3 && dy >= 29) || (mo > 3 && mo < 10) || (mo == 10 && dy < 25)) daylightSavingNew = true; 958 break; 959 case 2027: 960 if ((mo == 3 && dy >= 28) || (mo > 3 && mo < 10) || (mo == 10 && dy < 31)) daylightSavingNew = true; 961 break; 962 case 2028: 963 if ((mo == 3 && dy >= 26) || (mo > 3 && mo < 10) || (mo == 10 && dy < 29)) daylightSavingNew = true; 964 break; 965 case 2029: 966 if ((mo == 3 && dy >= 25) || (mo > 3 && mo < 10) || (mo == 10 && dy < 28)) daylightSavingNew = true; 967 break; 968 } 969 if (daylightSavingNew != daylightSaving) { 970 if (hr > 2) { 971 if (daylightSavingNew) hr = hr + 1; 972 if (!daylightSavingNew) hr = hr - 1; 973 daylightSaving = daylightSavingNew; 974 } 975 } 976} 977
Downloadable files
clock alarm schema
clock alarm schema
clock alarm schema
clock alarm schema
Bread board
the lcd is one with an i2c board atached to it
Bread board
Comments
Only logged in users can leave comments
ozyRonald
0 Followers
•0 Projects
Table of contents
Intro
24
0