Components and supplies
5 mm LED: Yellow
Rotary potentiometer (generic)
RTC DS3231 ZS-042
Arduino UNO
Resistor 1k ohm
Resistor 10k ohm
Standard LCD - 16x2 White on Blue
Relais VMA406 (Velleman)
Pushbutton momentary ON
Resistor 220 ohm
Tools and machines
Laser cutter (generic)
Soldering iron (generic)
Apps and platforms
Inkscape
Box Designer
Arduino IDE
Project description
Code
TELEPHONECLOCK.ino
arduino
Sketch for the Arduino Uno (a few Dutch words used as variables, sorry)
1/* 2 * Telephone activation clock 3 * This is a clock to activate a telephone for a programmed time. 4 * I use the DS3231 real time clock, this is more accurate than the DS1302 version from Velleman (VMA 301). 5 * The 16/2 LCD screen is the one that came with the Genuino Starter kit. 6 * For the relais I used the Velleman VMA406. For safety reasons i use the normally closed contacts to have 7 * an active telephone een when power supply to the unit fails. 8 * 9 * Erik Luyten 10 * March 11th, 2019 11 */ 12 13 14#include <DS3231.h> //for clock 15#include <LiquidCrystal.h> //for LCD display 16#include <EEPROM.h> //for EEPROM on UNO to store phone activation time settings during power failure 17DS3231 rtc(SDA, SCL); 18LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 19 20 21 22const int RelayPin = 7; //activation relay for telephone (normally closed is used so phone is active if unit is without power) 23const int LedTel = 6; //signal LED for phonerelay 24 25//************Buttons for setclock*****************// 26int P1=8; // Button SELECT to cycle through possible MENU settings (0 = display actual date/time TO 10 = store changed values) 27int P2=9; // Button + 28int P3=10; // Button - 29 30//************Variables for setclock **************// 31int hourupg; //upgrade setting for hours 32int minupg; //upgrade setting for minutes 33int yearupg; //upgrade setting for year (will be set to actual year in SETUP so we don't have to start from zero) 34int monthupg; //upgrade setting for month(number) 35int dayupg; //upgrade setting for day(number) 36int menu = 0; //cycles from 0 to 10 37int starthourupg = 00; //starting hour for telephone 38int startminupg = 00; //starting minutes for telephone 39int stophourupg = 00; //stop hour for telephone 40int stopminupg = 00; //stop minutes for telephone 41unsigned long BreakSetClock; //breakout from menu settings after 10 seconds (each buttonpress resets BreakSetClock time) 42 43//************Time to set phone on and off **********// 44Time PhoneOn; // Time to activate telephone 45Time PhoneOff; // Time to deactivate telephone 46Time ClockTijd; // Actual DayTime from Real Time Clock 47 48int DST; //Daylight Saving Time (read from EEPROM address 0: 0 = winter, 1 = Summer ) (not implemented yet) 49 50unsigned long StartPhoneSec; // time to activate telephone in seconds from midnight 51unsigned long StopPhoneSec; // time to deactivate telephone in seconds from midnight 52unsigned long DagSeconden; // actual seconds since midnight 53 54Time t; //the actual time/date info 55 56 57void setup() { 58 Serial.begin(9600); 59 pinMode(RelayPin, OUTPUT); 60 pinMode(LedTel, OUTPUT); 61 62 pinMode(P1,INPUT); 63 pinMode(P2,INPUT); 64 pinMode(P3,INPUT); 65 66 rtc.begin(); 67 lcd.begin(16,2); 68 // *********** Time and Date (no day of week) can now be set with setup buttons (P1= Menu, P2= +, P3= -) 69 //rtc.setDOW(SATURDAY); // Set Day-of-Week to SUNDAY 70 //rtc.setTime(11,19,30); // Set the time to 12:00:00 (24hr format) 71 //rtc.setDate(23, 2, 2019); // Day, Month, Year 72 //******* 73 //Default activation time for telephone (can be changed with setup buttons) 74 /* //not needed anymore since activation can be set with buttons 75 PhoneOn.hour = 18; 76 PhoneOn.min = 00; 77 PhoneOn.sec = 00; 78 79 //State deactivation time for telephone 80 PhoneOff.hour = 20; 81 PhoneOff.min = 00; 82 PhoneOff.sec = 00; 83 //store default activation and de-activation time to EEPROM in UNO 84 EEPROM.write(1,PhoneOn.hour); 85 EEPROM.write(2,PhoneOn.min); 86 EEPROM.write(3,PhoneOff.hour); 87 EEPROM.write(4,PhoneOff.min); 88 */ 89 90//Read activation and deactivation time from EEPROM at powerup 91 PhoneOn.hour = EEPROM.read(1); 92 PhoneOn.min = EEPROM.read(2); 93 PhoneOff.hour = EEPROM.read(3); 94 PhoneOff.min = EEPROM.read(4); 95 96 StartPhoneSec = seconden(PhoneOn); //recalculate Phone starttime to number of seconds since midnight 97 StopPhoneSec = seconden(PhoneOff); //recalculate Phone stoptime to number of seconds since midnight 98 99 // ??? no idea why we have to add 5 years but it works like this ???? 100 yearupg = t.year + 5; //yearupg is set to actual year to allow easy setup (we do not have to start from the beginning of (unix) time) 101 102} 103 104 105void loop() { 106 107// check if you press the SET button and increase the menu index 108 if(digitalRead(P1)) 109 { 110 menu=menu+1; 111 BreakSetClock = DagSeconden; //store keypress time to allow breakout after 10 seconds 112 } 113 if (DagSeconden > (BreakSetClock + 10)) //display time and date after 10 seconds without keypress 114 { 115 menu = 0; 116 } 117// in which subroutine should we go? 118/* 119 * Since clock time settings should be as accurate as possible we will perform them last in the cycle (just before storing the values). 120 * You cannot set seconds. This is always 0. Set minutes 1 higher and press select when the reference clock is at 59 seconds. 121 * This has to be done in the 10 seconds timeout frame so you might have to press the + and - a few times to cancel the breakout routine. 122 */ 123 switch (menu) { 124 case 0: 125 DisplayDateTime(); // void DisplayDateTime 126 break; 127 case 1: 128 DisplaySetStartHour(); // void DisplaySetStartHour (for telephone activation) 129 break; 130 case 2: 131 DisplaySetStartMin(); // void DisplaySetStartMin (for telephone activation) 132 break; 133 case 3: 134 DisplaySetStopHour(); // void DisplaySetStopHour (for telephone De-activation) 135 break; 136 case 4: 137 DisplaySetStopMin(); // void DisplaySetStopMin (for telephone De-activation) 138 break; 139 case 5: 140 DisplaySetYear(); // void DisplaySetYear 141 break; 142 case 6: 143 DisplaySetMonth(); // void DisplaySetMonth 144 break; 145 case 7: 146 DisplaySetDay(); // void DisplaySetDay 147 break; 148 case 8: 149 DisplaySetHour(); // void DisplaySetHour 150 break; 151 case 9: 152 DisplaySetMinute(); // void DisplaySetMinute 153 break; 154 case 10: 155 StoreSettings(); // void StoreSettings (only for the settings that changed: Time - Date - PhoneOn - PhoneOff) 156 menu=0; 157 break; 158 } 159 160 161 162 t = rtc.getTime(); // Read current time and date. 163 DagSeconden = seconden(t); //calculate seconds since midnight 164 165// print control info to Serial Monitor (commented out for better LCD clock response) 166/* Serial.print("Uur: "); 167 Serial.print(t.hour); 168 Serial.print(" Min: "); 169 Serial.print(t.min); 170 Serial.print(" Sec: "); 171 Serial.print(t.sec); 172 Serial.print(" Menupressed: "); 173 Serial.println(menu); 174 175 Serial.print("Seconden sinds middernacht: "); 176 Serial.print(DagSeconden); 177 Serial.print(" Start telephone: "); 178 Serial.print(StartPhoneSec); 179 Serial.print(" Stop telephone: "); 180 Serial.println(StopPhoneSec); 181 182 183 Serial.print("Starttijd: "); 184 Serial.print(PhoneOn.hour); 185 Serial.print(":"); 186 if (PhoneOn.min < 10) 187 { 188 Serial.print("0"); 189 } 190 Serial.print(PhoneOn.min); 191 Serial.print(" Stoptijd: "); 192 Serial.print(PhoneOff.hour); 193 Serial.print(":"); 194 if (PhoneOff.min < 10) 195 { 196 Serial.print("0"); 197 } 198 Serial.println(PhoneOff.min); 199*/ 200 201 if (StartPhoneSec < StopPhoneSec) //no activation possible at midnight - stoptime is larger than starttime 202 { 203 if ((DagSeconden > StartPhoneSec) && (DagSeconden < StopPhoneSec)) //ClockTime is between start and stop times - activate phone 204 { 205 digitalWrite(RelayPin, LOW); 206 digitalWrite(LedTel, HIGH); 207 } 208 else // Clocktime is outside start and stoptimes - deactivate phone 209 { 210 digitalWrite(RelayPin, HIGH); 211 digitalWrite(LedTel, LOW); 212 } 213 } 214 else if (StartPhoneSec > StopPhoneSec) //we are running over midnight to the next day - phone active at midnight 215 { 216 if (DagSeconden > StartPhoneSec) //activate phone 217 { 218 digitalWrite(RelayPin, LOW); 219 digitalWrite(LedTel, HIGH); 220 } 221 else if (DagSeconden > StopPhoneSec) //deactivate phone 222 { 223 digitalWrite(RelayPin, HIGH); 224 digitalWrite(LedTel, LOW); 225 } 226 } 227 228 229 delay(500); //wait 0.5 seconds (keypress reaction time is 0.5 second) 230 231} 232 233 234unsigned long seconden(Time dezetijd){ //calculate seconds sinds midnight for: ClockTime, PhoneStart and PhoneStop 235 unsigned long result; 236 unsigned long mijnuur = dezetijd.hour; 237 unsigned long uurseconden = 3600; 238 result = (dezetijd.sec + dezetijd.min * 60 + mijnuur * uurseconden); 239 return result; 240} 241 242void DisplayDateTime () //show Time, Date, PhoneStartTime and PhoneStopTime on LCD screen 243{ 244 lcd.clear(); 245 lcd.setCursor(1,0); 246 lcd.print(rtc.getTimeStr()); 247 lcd.print(" "); 248 lcd.print(PhoneOn.hour); 249 lcd.print(":"); 250 if (PhoneOn.min < 10) 251 { 252 lcd.print("0"); 253 } 254 lcd.print(PhoneOn.min); 255 lcd.setCursor(0,1); 256 lcd.print(rtc.getDateStr()); 257 lcd.print(" "); 258 lcd.print(PhoneOff.hour); 259 lcd.print(":"); 260 if (PhoneOff.min < 10) 261 { 262 lcd.print("0"); 263 } 264 lcd.print(PhoneOff.min); 265} 266 267void DisplaySetHour() //set clock hours 268{ 269// time setting 270 lcd.clear(); 271 if(digitalRead(P2)==HIGH) 272 { 273 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 274 if(hourupg==23) 275 { 276 hourupg=0; 277 } 278 else 279 { 280 hourupg=hourupg+1; 281 } 282 } 283 if(digitalRead(P3)==HIGH) 284 { 285 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 286 if(hourupg==0) 287 { 288 hourupg=23; 289 } 290 else 291 { 292 hourupg=hourupg-1; 293 } 294 } 295 lcd.setCursor(0,0); 296 lcd.print("Set hour:"); 297 lcd.setCursor(0,1); 298 lcd.print(hourupg,DEC); 299 delay(200); 300} 301 302void DisplaySetMinute() //set clock minutes 303{ 304// Setting the minutes 305 lcd.clear(); 306 if(digitalRead(P2)==HIGH) 307 { 308 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 309 if (minupg==59) 310 { 311 minupg=0; 312 } 313 else 314 { 315 minupg=minupg+1; 316 } 317 } 318 if(digitalRead(P3)==HIGH) 319 { 320 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 321 if (minupg==0) 322 { 323 minupg=59; 324 } 325 else 326 { 327 minupg=minupg-1; 328 } 329 } 330 lcd.setCursor(0,0); 331 lcd.print("Set Minutes:"); 332 lcd.setCursor(0,1); 333 lcd.print(minupg,DEC); 334 delay(200); 335} 336 337void DisplaySetYear() //set clock year (allready filled with year in clockmemory) 338{ 339// setting the year 340 lcd.clear(); 341 if(digitalRead(P2)==HIGH) 342 { 343 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 344 yearupg=yearupg+1; 345 } 346 if(digitalRead(P3)==HIGH) 347 { 348 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 349 yearupg=yearupg-1; 350 } 351 lcd.setCursor(0,0); 352 lcd.print("Set Year:"); 353 lcd.setCursor(0,1); 354 lcd.print(yearupg,DEC); 355 delay(200); 356} 357 358void DisplaySetMonth() //set clock month 359{ 360// Setting the month 361 lcd.clear(); 362 if(digitalRead(P2)==HIGH) 363 { 364 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 365 if (monthupg==12) 366 { 367 monthupg=1; 368 } 369 else 370 { 371 monthupg=monthupg+1; 372 } 373 } 374 if(digitalRead(P3)==HIGH) 375 { 376 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 377 if (monthupg==1) 378 { 379 monthupg=12; 380 } 381 else 382 { 383 monthupg=monthupg-1; 384 } 385 } 386 lcd.setCursor(0,0); 387 lcd.print("Set Month:"); 388 lcd.setCursor(0,1); 389 lcd.print(monthupg,DEC); 390 delay(200); 391} 392 393void DisplaySetDay() //set clock day 394{ 395// Setting the day 396 lcd.clear(); 397 if(digitalRead(P2)==HIGH) 398 { 399 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 400 if (dayupg==31) 401 { 402 dayupg=1; 403 } 404 else 405 { 406 dayupg=dayupg+1; 407 } 408 } 409 if(digitalRead(P3)==HIGH) 410 { 411 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 412 if (dayupg==1) 413 { 414 dayupg=31; 415 } 416 else 417 { 418 dayupg=dayupg-1; 419 } 420 } 421 lcd.setCursor(0,0); 422 lcd.print("Set Day:"); 423 lcd.setCursor(0,1); 424 lcd.print(dayupg,DEC); 425 delay(200); 426 427} 428 429void DisplaySetStartHour() //set phone starthour 430{ 431// time setting 432 lcd.clear(); 433 lcd.setCursor(0,0); 434 lcd.print("Set starthour:"); 435 lcd.setCursor(0,1); 436 lcd.print(starthourupg,DEC); 437 if(digitalRead(P2)==HIGH) 438 { 439 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 440 if(starthourupg==23) 441 { 442 starthourupg=0; 443 } 444 else 445 { 446 starthourupg=starthourupg+1; 447 } 448 } 449 if(digitalRead(P3)==HIGH) 450 { 451 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 452 if(starthourupg==0) 453 { 454 starthourupg=23; 455 } 456 else 457 { 458 starthourupg=starthourupg-1; 459 } 460 } 461 delay(200); 462} 463 464void DisplaySetStartMin() //set phonestartminutes 465{ 466// time setting 467 lcd.clear(); 468 lcd.setCursor(0,0); 469 lcd.print("Set startminutes:"); 470 lcd.setCursor(0,1); 471 lcd.print(startminupg,DEC); 472 if(digitalRead(P2)==HIGH) 473 { 474 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 475 if(startminupg==59) 476 { 477 startminupg=0; 478 } 479 else 480 { 481 startminupg=startminupg+1; 482 } 483 } 484 if(digitalRead(P3)==HIGH) 485 { 486 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 487 if(startminupg==0) 488 { 489 startminupg=59; 490 } 491 else 492 { 493 startminupg=startminupg-1; 494 } 495 } 496 delay(200); 497} 498 499void DisplaySetStopHour() //set phonestophour 500{ 501// time setting 502 lcd.clear(); 503 lcd.setCursor(0,0); 504 lcd.print("Set stophour:"); 505 lcd.setCursor(0,1); 506 lcd.print(stophourupg,DEC); 507 if(digitalRead(P2)==HIGH) 508 { 509 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 510 if(stophourupg==23) 511 { 512 stophourupg=0; 513 } 514 else 515 { 516 stophourupg=stophourupg+1; 517 } 518 } 519 if(digitalRead(P3)==HIGH) 520 { 521 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 522 if(stophourupg==0) 523 { 524 stophourupg=23; 525 } 526 else 527 { 528 stophourupg=stophourupg-1; 529 } 530 } 531 delay(200); 532} 533 534void DisplaySetStopMin() //set phonestopminutes 535{ 536// time setting 537 lcd.clear(); 538 lcd.setCursor(0,0); 539 lcd.print("Set stopminutes:"); 540 lcd.setCursor(0,1); 541 lcd.print(stopminupg,DEC); 542 if(digitalRead(P2)==HIGH) 543 { 544 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 545 if(stopminupg==59) 546 { 547 stopminupg=0; 548 } 549 else 550 { 551 stopminupg=stopminupg+1; 552 } 553 } 554 if(digitalRead(P3)==HIGH) 555 { 556 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 10 seconds 557 if(stopminupg==0) 558 { 559 stopminupg=59; 560 } 561 else 562 { 563 stopminupg=stopminupg-1; 564 } 565 } 566 delay(200); 567} 568 569void StoreSettings() //store the results depending on values in variables 570{ 571// Saving Time, Date, StopTime and StartTime only if they are changed 572// Disabled message to LCD to allow more accurate time setting 573 574 lcd.clear(); 575 lcd.setCursor(0,0); 576 if ((hourupg == 0) && (minupg == 0)) //do not set time if both hours and minutes are zero (cannot set clock to midnight time) 577 { 578 lcd.print("TIME not saved !"); 579 delay(2000); 580 } 581 else 582 { 583 lcd.print("Saving TIME"); 584 rtc.setTime(hourupg,minupg,00); // Set the time (24hr format) 585 hourupg = 0; 586 minupg = 0; 587 delay(2000); 588 } 589 lcd.clear(); 590 lcd.setCursor(0,0); 591 if ((dayupg == 0) || (monthupg == 0) || (yearupg == 0)) //do not set date if any of these values is zero 592 { 593 lcd.print("DATE not saved !"); 594 delay(2000); 595 } 596 else 597 { 598 lcd.print("Saving DATE"); 599 rtc.setDate(dayupg, monthupg, yearupg); // Day, Month, Year 600 dayupg = 0; 601 monthupg = 0; 602 delay(2000); 603 } 604 605 606//First Check if start and stoptime are not the same (should be at least one minute difference) 607 lcd.clear(); 608 lcd.setCursor(0,0); 609 if ((starthourupg == stophourupg) && (startminupg == stopminupg) && ((starthourupg + startminupg + stophourupg + stopminupg) != 0)) 610 { 611 lcd.print("START & STOPTIME"); 612 lcd.setCursor(0,1); 613 lcd.print("Cant be the same"); 614 delay(2000); 615 } 616 else 617 { 618 //check to see if starttime should be saved 619 if ((starthourupg == 0) && (startminupg == 0)) //don't set starttime if both hours and minutes are zero(cannot set starttime to midnight) 620 { 621 lcd.print("STARTTIME "); 622 lcd.setCursor(0,1); 623 lcd.print("not saved !"); 624 delay(2000); 625 } 626 else 627 { 628 lcd.print("Saving STARTTIME"); 629 PhoneOn.hour = starthourupg; 630 PhoneOn.min = startminupg; 631 PhoneOn.sec = 00; 632 StartPhoneSec = seconden(PhoneOn); //recalculate Phone starttime to number of seconds since midnight 633 EEPROM.write(1,PhoneOn.hour); //write activation time to EEPROM on UNO 634 EEPROM.write(2,PhoneOn.min); //write activation time to EEPROM on UNO 635 lcd.setCursor(0,1); 636 lcd.print(PhoneOn.hour); 637 lcd.print(":"); 638 if (PhoneOn.min < 10) 639 { 640 lcd.print("0"); 641 } 642 lcd.print(PhoneOn.min); 643 starthourupg = 0; 644 startminupg = 0; 645 delay(2000); 646 } 647 648 lcd.clear(); 649 lcd.setCursor(0,0); 650 //check to aee if stoptime should be changed 651 if ((stophourupg == 0) && (stopminupg == 0)) //don't set stoptime if both hours and minutes are zero(cannot set stoptime to midnight) 652 { 653 lcd.print("STOPTIME "); 654 lcd.setCursor(0,1); 655 lcd.print("not saved !"); 656 delay(2000); 657 } 658 else 659 { 660 lcd.print("Saving STOPTIME"); 661 PhoneOff.hour = stophourupg; 662 PhoneOff.min = stopminupg; 663 PhoneOff.sec = 00; 664 StopPhoneSec = seconden(PhoneOff); //recalculate Phone stoptime to number of seconds since midnight 665 EEPROM.write(3,PhoneOff.hour); //write de-activation time to EEPROM on UNO 666 EEPROM.write(4,PhoneOff.min); //write de-activation time to EEPROM on UNO 667 lcd.setCursor(0,1); 668 lcd.print(PhoneOff.hour); 669 lcd.print(":"); 670 if (PhoneOff.min < 10) 671 { 672 lcd.print("0"); 673 } 674 lcd.print(PhoneOff.min); 675 stophourupg = 0; 676 stopminupg = 0; 677 delay(2000); 678 } 679 } 680 //delay(200); 681} 682
TELEPHONECLOCK.ino
arduino
Sketch for the Arduino Uno (a few Dutch words used as variables, sorry)
1/* 2 * Telephone activation clock 3 * This is a clock to activate 4 a telephone for a programmed time. 5 * I use the DS3231 real time clock, this 6 is more accurate than the DS1302 version from Velleman (VMA 301). 7 * The 16/2 8 LCD screen is the one that came with the Genuino Starter kit. 9 * For the relais 10 I used the Velleman VMA406. For safety reasons i use the normally closed contacts 11 to have 12 * an active telephone een when power supply to the unit fails. 13 * 14 15 * Erik Luyten 16 * March 11th, 2019 17 */ 18 19 20#include <DS3231.h> 21 //for clock 22#include <LiquidCrystal.h> //for 23 LCD display 24#include <EEPROM.h> //for EEPROM on UNO to store 25 phone activation time settings during power failure 26DS3231 rtc(SDA, SCL); 27LiquidCrystal 28 lcd(12, 11, 5, 4, 3, 2); 29 30 31 32const int RelayPin = 7; //activation 33 relay for telephone (normally closed is used so phone is active if unit is without 34 power) 35const int LedTel = 6; //signal LED for phonerelay 36 37//************Buttons 38 for setclock*****************// 39int P1=8; // Button SELECT to cycle through 40 possible MENU settings (0 = display actual date/time TO 10 = store changed values) 41int 42 P2=9; // Button + 43int P3=10; // Button - 44 45//************Variables for 46 setclock **************// 47int hourupg; //upgrade setting for hours 48int 49 minupg; //upgrade setting for minutes 50int yearupg; //upgrade setting 51 for year (will be set to actual year in SETUP so we don't have to start from zero) 52int 53 monthupg; //upgrade setting for month(number) 54int dayupg; //upgrade 55 setting for day(number) 56int menu = 0; //cycles from 0 to 10 57int starthourupg 58 = 00; //starting hour for telephone 59int startminupg = 00; //starting minutes 60 for telephone 61int stophourupg = 00; //stop hour for telephone 62int stopminupg 63 = 00; //stop minutes for telephone 64unsigned long BreakSetClock; //breakout 65 from menu settings after 10 seconds (each buttonpress resets BreakSetClock time) 66 67//************Time 68 to set phone on and off **********// 69Time PhoneOn; // Time to activate 70 telephone 71Time PhoneOff; // Time to deactivate telephone 72Time ClockTijd; 73 // Actual DayTime from Real Time Clock 74 75int DST; //Daylight Saving 76 Time (read from EEPROM address 0: 0 = winter, 1 = Summer ) (not implemented yet) 77 78unsigned 79 long StartPhoneSec; // time to activate telephone in seconds from midnight 80unsigned 81 long StopPhoneSec; // time to deactivate telephone in seconds from midnight 82unsigned 83 long DagSeconden; // actual seconds since midnight 84 85Time t; //the 86 actual time/date info 87 88 89void setup() { 90 Serial.begin(9600); 91 pinMode(RelayPin, 92 OUTPUT); 93 pinMode(LedTel, OUTPUT); 94 95 pinMode(P1,INPUT); 96 pinMode(P2,INPUT); 97 98 pinMode(P3,INPUT); 99 100 rtc.begin(); 101 lcd.begin(16,2); 102 // *********** 103 Time and Date (no day of week) can now be set with setup buttons (P1= Menu, P2= 104 +, P3= -) 105 //rtc.setDOW(SATURDAY); // Set Day-of-Week to SUNDAY 106 //rtc.setTime(11,19,30); 107 // Set the time to 12:00:00 (24hr format) 108 //rtc.setDate(23, 2, 2019); 109 // Day, Month, Year 110 //******* 111 //Default activation time for telephone 112 (can be changed with setup buttons) 113 /* //not needed anymore since activation 114 can be set with buttons 115 PhoneOn.hour = 18; 116 PhoneOn.min = 00; 117 PhoneOn.sec 118 = 00; 119 120 //State deactivation time for telephone 121 PhoneOff.hour = 122 20; 123 PhoneOff.min = 00; 124 PhoneOff.sec = 00; 125 //store default activation 126 and de-activation time to EEPROM in UNO 127 EEPROM.write(1,PhoneOn.hour); 128 EEPROM.write(2,PhoneOn.min); 129 130 EEPROM.write(3,PhoneOff.hour); 131 EEPROM.write(4,PhoneOff.min); 132 */ 133 134//Read 135 activation and deactivation time from EEPROM at powerup 136 PhoneOn.hour = EEPROM.read(1); 137 138 PhoneOn.min = EEPROM.read(2); 139 PhoneOff.hour = EEPROM.read(3); 140 PhoneOff.min 141 = EEPROM.read(4); 142 143 StartPhoneSec = seconden(PhoneOn); //recalculate 144 Phone starttime to number of seconds since midnight 145 StopPhoneSec = seconden(PhoneOff); 146 //recalculate Phone stoptime to number of seconds since midnight 147 148 // 149 ??? no idea why we have to add 5 years but it works like this ???? 150 yearupg 151 = t.year + 5; //yearupg is set to actual year to allow easy setup (we do not 152 have to start from the beginning of (unix) time) 153 154} 155 156 157void loop() 158 { 159 160// check if you press the SET button and increase the menu index 161 162 if(digitalRead(P1)) 163 { 164 menu=menu+1; 165 BreakSetClock = DagSeconden; 166 //store keypress time to allow breakout after 10 seconds 167 } 168 if 169 (DagSeconden > (BreakSetClock + 10)) //display time and date after 10 seconds without 170 keypress 171 { 172 menu = 0; 173 } 174// in which subroutine should 175 we go? 176/* 177 * Since clock time settings should be as accurate as possible we 178 will perform them last in the cycle (just before storing the values). 179 * You 180 cannot set seconds. This is always 0. Set minutes 1 higher and press select when 181 the reference clock is at 59 seconds. 182 * This has to be done in the 10 seconds 183 timeout frame so you might have to press the + and - a few times to cancel the breakout 184 routine. 185 */ 186 switch (menu) { 187 case 0: 188 DisplayDateTime(); 189 // void DisplayDateTime 190 break; 191 case 1: 192 DisplaySetStartHour(); 193 // void DisplaySetStartHour (for telephone activation) 194 break; 195 case 196 2: 197 DisplaySetStartMin(); // void DisplaySetStartMin (for telephone activation) 198 199 break; 200 case 3: 201 DisplaySetStopHour(); // void DisplaySetStopHour 202 (for telephone De-activation) 203 break; 204 case 4: 205 DisplaySetStopMin(); 206 // void DisplaySetStopMin (for telephone De-activation) 207 break; 208 209 case 5: 210 DisplaySetYear(); // void DisplaySetYear 211 break; 212 213 case 6: 214 DisplaySetMonth(); // void DisplaySetMonth 215 break; 216 217 case 7: 218 DisplaySetDay(); // void DisplaySetDay 219 break; 220 221 case 8: 222 DisplaySetHour(); // void DisplaySetHour 223 break; 224 225 case 9: 226 DisplaySetMinute(); // void DisplaySetMinute 227 break; 228 229 case 10: 230 StoreSettings(); // void StoreSettings (only for the 231 settings that changed: Time - Date - PhoneOn - PhoneOff) 232 menu=0; 233 break; 234 235 } 236 237 238 239 t = rtc.getTime(); // Read current time and date. 240 DagSeconden 241 = seconden(t); //calculate seconds since midnight 242 243// print control info 244 to Serial Monitor (commented out for better LCD clock response) 245/* Serial.print("Uur: 246 "); 247 Serial.print(t.hour); 248 Serial.print(" Min: "); 249 Serial.print(t.min); 250 251 Serial.print(" Sec: "); 252 Serial.print(t.sec); 253 Serial.print(" Menupressed: 254 "); 255 Serial.println(menu); 256 257 Serial.print("Seconden sinds middernacht: 258 "); 259 Serial.print(DagSeconden); 260 Serial.print(" Start telephone: "); 261 262 Serial.print(StartPhoneSec); 263 Serial.print(" Stop telephone: "); 264 Serial.println(StopPhoneSec); 265 266 267 268 Serial.print("Starttijd: "); 269 Serial.print(PhoneOn.hour); 270 Serial.print(":"); 271 272 if (PhoneOn.min < 10) 273 { 274 Serial.print("0"); 275 } 276 Serial.print(PhoneOn.min); 277 278 Serial.print(" Stoptijd: "); 279 Serial.print(PhoneOff.hour); 280 Serial.print(":"); 281 282 if (PhoneOff.min < 10) 283 { 284 Serial.print("0"); 285 } 286 Serial.println(PhoneOff.min); 287*/ 288 289 290 if (StartPhoneSec < StopPhoneSec) //no activation possible at midnight 291 - stoptime is larger than starttime 292 { 293 if ((DagSeconden > StartPhoneSec) 294 && (DagSeconden < StopPhoneSec)) //ClockTime is between start and stop times - activate 295 phone 296 { 297 digitalWrite(RelayPin, LOW); 298 digitalWrite(LedTel, 299 HIGH); 300 } 301 else // Clocktime 302 is outside start and stoptimes - deactivate phone 303 { 304 digitalWrite(RelayPin, 305 HIGH); 306 digitalWrite(LedTel, LOW); 307 } 308 } 309 else if 310 (StartPhoneSec > StopPhoneSec) //we are running over midnight to the next day - 311 phone active at midnight 312 { 313 if (DagSeconden > StartPhoneSec) //activate 314 phone 315 { 316 digitalWrite(RelayPin, LOW); 317 digitalWrite(LedTel, 318 HIGH); 319 } 320 else if (DagSeconden > StopPhoneSec) //deactivate 321 phone 322 { 323 digitalWrite(RelayPin, HIGH); 324 digitalWrite(LedTel, 325 LOW); 326 } 327 } 328 329 330 delay(500); //wait 0.5 seconds (keypress 331 reaction time is 0.5 second) 332 333} 334 335 336unsigned long seconden(Time dezetijd){ 337 //calculate seconds sinds midnight for: ClockTime, PhoneStart and PhoneStop 338 339 unsigned long result; 340 unsigned long mijnuur = dezetijd.hour; 341 unsigned 342 long uurseconden = 3600; 343 result = (dezetijd.sec + dezetijd.min * 60 + mijnuur 344 * uurseconden); 345 return result; 346} 347 348void DisplayDateTime () //show 349 Time, Date, PhoneStartTime and PhoneStopTime on LCD screen 350{ 351 lcd.clear(); 352 353 lcd.setCursor(1,0); 354 lcd.print(rtc.getTimeStr()); 355 lcd.print(" "); 356 357 lcd.print(PhoneOn.hour); 358 lcd.print(":"); 359 if (PhoneOn.min < 10) 360 { 361 362 lcd.print("0"); 363 } 364 lcd.print(PhoneOn.min); 365 lcd.setCursor(0,1); 366 367 lcd.print(rtc.getDateStr()); 368 lcd.print(" "); 369 lcd.print(PhoneOff.hour); 370 371 lcd.print(":"); 372 if (PhoneOff.min < 10) 373 { 374 lcd.print("0"); 375 376 } 377 lcd.print(PhoneOff.min); 378} 379 380void DisplaySetHour() //set clock 381 hours 382{ 383// time setting 384 lcd.clear(); 385 if(digitalRead(P2)==HIGH) 386 387 { 388 BreakSetClock = DagSeconden; //store menupress time to allow breakout 389 after 10 seconds 390 if(hourupg==23) 391 { 392 hourupg=0; 393 } 394 395 else 396 { 397 hourupg=hourupg+1; 398 } 399 } 400 if(digitalRead(P3)==HIGH) 401 402 { 403 BreakSetClock = DagSeconden; //store menupress time to allow breakout 404 after 10 seconds 405 if(hourupg==0) 406 { 407 hourupg=23; 408 } 409 410 else 411 { 412 hourupg=hourupg-1; 413 } 414 } 415 lcd.setCursor(0,0); 416 417 lcd.print("Set hour:"); 418 lcd.setCursor(0,1); 419 lcd.print(hourupg,DEC); 420 421 delay(200); 422} 423 424void DisplaySetMinute() //set clock minutes 425{ 426// 427 Setting the minutes 428 lcd.clear(); 429 if(digitalRead(P2)==HIGH) 430 { 431 432 BreakSetClock = DagSeconden; //store menupress time to allow breakout after 433 10 seconds 434 if (minupg==59) 435 { 436 minupg=0; 437 } 438 else 439 440 { 441 minupg=minupg+1; 442 } 443 } 444 if(digitalRead(P3)==HIGH) 445 446 { 447 BreakSetClock = DagSeconden; //store menupress time to allow breakout 448 after 10 seconds 449 if (minupg==0) 450 { 451 minupg=59; 452 } 453 454 else 455 { 456 minupg=minupg-1; 457 } 458 } 459 lcd.setCursor(0,0); 460 461 lcd.print("Set Minutes:"); 462 lcd.setCursor(0,1); 463 lcd.print(minupg,DEC); 464 465 delay(200); 466} 467 468void DisplaySetYear() //set clock year (allready filled 469 with year in clockmemory) 470{ 471// setting the year 472 lcd.clear(); 473 if(digitalRead(P2)==HIGH) 474 475 { 476 BreakSetClock = DagSeconden; //store menupress time to allow breakout 477 after 10 seconds 478 yearupg=yearupg+1; 479 } 480 if(digitalRead(P3)==HIGH) 481 482 { 483 BreakSetClock = DagSeconden; //store menupress time to allow breakout 484 after 10 seconds 485 yearupg=yearupg-1; 486 } 487 lcd.setCursor(0,0); 488 lcd.print("Set 489 Year:"); 490 lcd.setCursor(0,1); 491 lcd.print(yearupg,DEC); 492 delay(200); 493} 494 495void 496 DisplaySetMonth() //set clock month 497{ 498// Setting the month 499 lcd.clear(); 500 501 if(digitalRead(P2)==HIGH) 502 { 503 BreakSetClock = DagSeconden; //store 504 menupress time to allow breakout after 10 seconds 505 if (monthupg==12) 506 { 507 508 monthupg=1; 509 } 510 else 511 { 512 monthupg=monthupg+1; 513 514 } 515 } 516 if(digitalRead(P3)==HIGH) 517 { 518 BreakSetClock = DagSeconden; 519 //store menupress time to allow breakout after 10 seconds 520 if (monthupg==1) 521 522 { 523 monthupg=12; 524 } 525 else 526 { 527 monthupg=monthupg-1; 528 529 } 530 } 531 lcd.setCursor(0,0); 532 lcd.print("Set Month:"); 533 lcd.setCursor(0,1); 534 535 lcd.print(monthupg,DEC); 536 delay(200); 537} 538 539void DisplaySetDay() //set 540 clock day 541{ 542// Setting the day 543 lcd.clear(); 544 if(digitalRead(P2)==HIGH) 545 546 { 547 BreakSetClock = DagSeconden; //store menupress time to allow breakout 548 after 10 seconds 549 if (dayupg==31) 550 { 551 dayupg=1; 552 } 553 554 else 555 { 556 dayupg=dayupg+1; 557 } 558 } 559 if(digitalRead(P3)==HIGH) 560 561 { 562 BreakSetClock = DagSeconden; //store menupress time to allow breakout 563 after 10 seconds 564 if (dayupg==1) 565 { 566 dayupg=31; 567 } 568 569 else 570 { 571 dayupg=dayupg-1; 572 } 573 } 574 lcd.setCursor(0,0); 575 576 lcd.print("Set Day:"); 577 lcd.setCursor(0,1); 578 lcd.print(dayupg,DEC); 579 580 delay(200); 581 582} 583 584void DisplaySetStartHour() //set phone starthour 585{ 586// 587 time setting 588 lcd.clear(); 589 lcd.setCursor(0,0); 590 lcd.print("Set starthour:"); 591 592 lcd.setCursor(0,1); 593 lcd.print(starthourupg,DEC); 594 if(digitalRead(P2)==HIGH) 595 596 { 597 BreakSetClock = DagSeconden; //store menupress time to allow breakout 598 after 10 seconds 599 if(starthourupg==23) 600 { 601 starthourupg=0; 602 603 } 604 else 605 { 606 starthourupg=starthourupg+1; 607 } 608 609 } 610 if(digitalRead(P3)==HIGH) 611 { 612 BreakSetClock = DagSeconden; 613 //store menupress time to allow breakout after 10 seconds 614 if(starthourupg==0) 615 616 { 617 starthourupg=23; 618 } 619 else 620 { 621 starthourupg=starthourupg-1; 622 623 } 624 } 625 delay(200); 626} 627 628void DisplaySetStartMin() //set phonestartminutes 629{ 630// 631 time setting 632 lcd.clear(); 633 lcd.setCursor(0,0); 634 lcd.print("Set startminutes:"); 635 636 lcd.setCursor(0,1); 637 lcd.print(startminupg,DEC); 638 if(digitalRead(P2)==HIGH) 639 640 { 641 BreakSetClock = DagSeconden; //store menupress time to allow breakout 642 after 10 seconds 643 if(startminupg==59) 644 { 645 startminupg=0; 646 647 } 648 else 649 { 650 startminupg=startminupg+1; 651 } 652 653 } 654 if(digitalRead(P3)==HIGH) 655 { 656 BreakSetClock = DagSeconden; 657 //store menupress time to allow breakout after 10 seconds 658 if(startminupg==0) 659 660 { 661 startminupg=59; 662 } 663 else 664 { 665 startminupg=startminupg-1; 666 667 } 668 } 669 delay(200); 670} 671 672void DisplaySetStopHour() //set 673 phonestophour 674{ 675// time setting 676 lcd.clear(); 677 lcd.setCursor(0,0); 678 679 lcd.print("Set stophour:"); 680 lcd.setCursor(0,1); 681 lcd.print(stophourupg,DEC); 682 683 if(digitalRead(P2)==HIGH) 684 { 685 BreakSetClock = DagSeconden; //store 686 menupress time to allow breakout after 10 seconds 687 if(stophourupg==23) 688 689 { 690 stophourupg=0; 691 } 692 else 693 { 694 stophourupg=stophourupg+1; 695 696 } 697 } 698 if(digitalRead(P3)==HIGH) 699 { 700 BreakSetClock 701 = DagSeconden; //store menupress time to allow breakout after 10 seconds 702 if(stophourupg==0) 703 704 { 705 stophourupg=23; 706 } 707 else 708 { 709 stophourupg=stophourupg-1; 710 711 } 712 } 713 delay(200); 714} 715 716void DisplaySetStopMin() //set phonestopminutes 717{ 718// 719 time setting 720 lcd.clear(); 721 lcd.setCursor(0,0); 722 lcd.print("Set stopminutes:"); 723 724 lcd.setCursor(0,1); 725 lcd.print(stopminupg,DEC); 726 if(digitalRead(P2)==HIGH) 727 728 { 729 BreakSetClock = DagSeconden; //store menupress time to allow breakout 730 after 10 seconds 731 if(stopminupg==59) 732 { 733 stopminupg=0; 734 735 } 736 else 737 { 738 stopminupg=stopminupg+1; 739 } 740 741 } 742 if(digitalRead(P3)==HIGH) 743 { 744 BreakSetClock = DagSeconden; 745 //store menupress time to allow breakout after 10 seconds 746 if(stopminupg==0) 747 748 { 749 stopminupg=59; 750 } 751 else 752 { 753 stopminupg=stopminupg-1; 754 755 } 756 } 757 delay(200); 758} 759 760void StoreSettings() //store the 761 results depending on values in variables 762{ 763// Saving Time, Date, StopTime 764 and StartTime only if they are changed 765// Disabled message to LCD to allow more 766 accurate time setting 767 768 lcd.clear(); 769 lcd.setCursor(0,0); 770 if ((hourupg 771 == 0) && (minupg == 0)) //do not set time if both hours and minutes are zero 772 (cannot set clock to midnight time) 773 { 774 lcd.print("TIME not saved 775 !"); 776 delay(2000); 777 } 778 else 779 { 780 lcd.print("Saving 781 TIME"); 782 rtc.setTime(hourupg,minupg,00); // Set the time (24hr format) 783 784 hourupg = 0; 785 minupg = 0; 786 delay(2000); 787 } 788 lcd.clear(); 789 790 lcd.setCursor(0,0); 791 if ((dayupg == 0) || (monthupg == 0) || (yearupg == 0)) 792 //do not set date if any of these values is zero 793 { 794 lcd.print("DATE 795 not saved !"); 796 delay(2000); 797 } 798 else 799 { 800 lcd.print("Saving 801 DATE"); 802 rtc.setDate(dayupg, monthupg, yearupg); // Day, Month, Year 803 804 dayupg = 0; 805 monthupg = 0; 806 delay(2000); 807 } 808 809 810 811//First Check if start and stoptime are not the same (should be at least 812 one minute difference) 813 lcd.clear(); 814 lcd.setCursor(0,0); 815 if ((starthourupg 816 == stophourupg) && (startminupg == stopminupg) && ((starthourupg + startminupg + 817 stophourupg + stopminupg) != 0)) 818 { 819 lcd.print("START & STOPTIME"); 820 821 lcd.setCursor(0,1); 822 lcd.print("Cant be the same"); 823 delay(2000); 824 825 } 826 else 827 { 828 //check to see if starttime should be saved 829 830 if ((starthourupg == 0) && (startminupg == 0)) //don't set starttime if both 831 hours and minutes are zero(cannot set starttime to midnight) 832 { 833 lcd.print("STARTTIME 834 "); 835 lcd.setCursor(0,1); 836 lcd.print("not saved !"); 837 838 delay(2000); 839 } 840 else 841 { 842 lcd.print("Saving 843 STARTTIME"); 844 PhoneOn.hour = starthourupg; 845 PhoneOn.min 846 = startminupg; 847 PhoneOn.sec = 00; 848 StartPhoneSec = 849 seconden(PhoneOn); //recalculate Phone starttime to number of seconds since midnight 850 851 EEPROM.write(1,PhoneOn.hour); //write activation time to EEPROM 852 on UNO 853 EEPROM.write(2,PhoneOn.min); //write activation time 854 to EEPROM on UNO 855 lcd.setCursor(0,1); 856 lcd.print(PhoneOn.hour); 857 858 lcd.print(":"); 859 if (PhoneOn.min < 10) 860 { 861 862 lcd.print("0"); 863 } 864 lcd.print(PhoneOn.min); 865 866 starthourupg = 0; 867 startminupg = 0; 868 delay(2000); 869 870 } 871 872 lcd.clear(); 873 lcd.setCursor(0,0); 874 875 //check to aee if stoptime should be changed 876 if ((stophourupg == 877 0) && (stopminupg == 0)) //don't set stoptime if both hours and minutes are zero(cannot 878 set stoptime to midnight) 879 { 880 lcd.print("STOPTIME "); 881 882 lcd.setCursor(0,1); 883 lcd.print("not saved !"); 884 delay(2000); 885 886 } 887 else 888 { 889 lcd.print("Saving STOPTIME"); 890 891 PhoneOff.hour = stophourupg; 892 PhoneOff.min = stopminupg; 893 894 PhoneOff.sec = 00; 895 StopPhoneSec = seconden(PhoneOff); //recalculate 896 Phone stoptime to number of seconds since midnight 897 EEPROM.write(3,PhoneOff.hour); 898 //write de-activation time to EEPROM on UNO 899 EEPROM.write(4,PhoneOff.min); 900 //write de-activation time to EEPROM on UNO 901 lcd.setCursor(0,1); 902 903 lcd.print(PhoneOff.hour); 904 lcd.print(":"); 905 if 906 (PhoneOff.min < 10) 907 { 908 lcd.print("0"); 909 } 910 911 lcd.print(PhoneOff.min); 912 stophourupg = 0; 913 stopminupg 914 = 0; 915 delay(2000); 916 } 917 } 918 //delay(200); 919} 920
Downloadable files
schematicTelephone activation clock
wiring diagram
schematicTelephone activation clock
schematicTelephone activation clock
wiring diagram
schematicTelephone activation clock
Documentation
PhoneClock enclosure
MDF 4mm lasercut box with additional MDF pieces for component parts
PhoneClock enclosure
Comments
Only logged in users can leave comments
eluyten
0 Followers
•0 Projects
Table of contents
Intro
3
0