Translate

Sunday 4 May 2014

The full sketch of room management system including LCD

As promised, the full sketch before we finally start with building the menu. Again, you may use the sketch as is or modify it to your needs. What ever you do with it, you do it at your own risk.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
/////////////////////Includes/////////////////////////////
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <ShiftLCD.h>
#include <avr/pgmspace.h>
//#include <LiquidCrystal.h>
//#include <SPI.h>

/////////////////////Declaring the Variables/////////////////

///////////Timer and Sensitivity Settings to be changed to individual needs////////////////
unsigned int sensitivity = 400;    //should be between 200 and 1000 as
                                   //lower the number as more responsive 
                                   //the system will be
unsigned int photoCellCutOn = 320;  //var holding the switching limit for the photocell
unsigned int photoCellCutOff = 280; //var holding the value where the photocell cuts off
unsigned int photoOutsideOn = 220; //var holding the value which the photocell reading 
unsigned int photoOutsideOff = 260;
unsigned int hourOutsideOff = 23;  //var holding the time (full hours) in which the lights have 
                                   //to switch off
unsigned int minuteOutsideOff = 30;//var holding the time (minutes) in which the lights 
                                   //have to switch off

int dBed1 = 60;            //delay time in milliseconds for bedroom 1
int dBed2 = 60;            //delay time in milliseconds for bedroom 2
int dBed3 = 60;            //delay time in milliseconds for bedroom 3
int dLiving = 300;         //delay time in milliseconds for living area
int dBath1 = 180;          //delay time in milliseconds for bathroom 1
int dBath2 = 180;          //delay time in milliseconds for bathroom 2
int dBath3 = 180;          //delay time in milliseconds for bathroom 3
int dBath4 = 180;          //delay time in milliseconds for bathroom 4
int dKitchen = 120;        //delay time in milliseconds for kitchen
int dCorridor = 60;        //delay time in milliseconds for corridor
int dAC1 = 120;            //delay time in milliseconds for AC 1 (bed1)
int dAC2 = 120;            //delay time in milliseconds for AC 2 (bed2)
int dAC3 = 120;            //delay time in milliseconds for AC 3 (bed3)
int dAC4 = 120;            //delay time in milliseconds for AC 4 (living)
int dMaster = 240;         //delay time in milliseconds for Master Off

byte hourAc1On = 18;
byte minuteAc1On = 0;
byte hourAc1Off = 24;
byte minuteAc1Off = 0;

//////////////////////holliday timer settings//////////////////////

////////////Room 1 (Bed 1) ///////////
byte room1MActive = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room1OnM[2] = {5, 35};        //Time to switch on the lights 5
byte room1OffM[2] = {6, 5};       //Time to switch off the lights 6

byte room1O1Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room1On1[2] = {19, 35};        //Time to switch on the lights
byte room1Off1[2] = {20, 15};       //Time to switch off the lights

byte room102Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room1On2[2] = {21, 5};        //Time to switch on the ;ights
byte room1Off2[2] = {21, 15};       //Time to switch off the lights

////////////Room 2 (Bed 2) ///////////
byte room2MActive = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room2OnM[2] = {6, 30};         //Time to switch on the lights
byte room2OffM[2] = {6, 50};        //Time to switch off the lights

byte room201Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room2On1[2] = {19, 30};        //Time to switch on the lights
byte room2Off1[2] = {20, 10};       //Time to switch off the lights

////////////Room 3 (Bed 3) ///////////
byte room3MActive = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room3OnM[2] = {5, 50};        //time to switch on the lights
byte room3OffM[2] = {6, 20};       //time to switch off the lights

byte room301Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room3On1[2] = {18, 10};        //time to switch on the lights
byte room3Off1[2] = {18, 25};       //time to switch off the lights

byte room302Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room3On2[2] = {19, 15};        //Time to switch on the lights
byte room3Off2[2] = {19, 40};       //Time to switch off the lights

byte room303Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room3On3[2] = {23, 20};        //Time to switch on the lights
byte room3Off3[2] = {23, 35};       //Time to switch off the lights

///////////Room 4 {Living)
byte room401Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room4On[2] = {17, 30};         //Time to switch on the lights
byte room4Off[2] = {23, 30};        //Time to switch off the lights

//////////Room 5 (bath 1)//////////
byte room5MActive = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room5OnM[2] = {5, 40};         //Time to switch on the lights
byte room5OffM[2] = {5, 45};        //Time to switch off the lights

byte room501Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room5On[2] = {19, 55};         //Time to switch on the lights
byte room5Off[2] = {20, 10};        //Time to switch off the lights

//////////Room 6 (bath 2)//////////
byte room6MActive = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room6OnM[2] = {6, 35};         //Time to switch on the lights
byte room6OffM[2] = {6, 45};        //Time to switch off the lights

byte room601Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room6On[2] = {19, 50};         //Time to switch on the lights
byte room6Off[2] = {20, 05};        //Time to switch off the lights

//////////Room 7 (bath 3)//////////
byte room7MActive = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room7OnM[2] = {6, 5};         //Time to switch on the lights
byte room7OffM[2] = {6, 25};        //Time to switch off the lights

byte room701Active = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room7On[2] = {22, 50};         //Time to switch on the lights
byte room7Off[2] = {23, 15};        //Time to switch off the lights

//////////Room 8 (bath 4)//////////
byte room8MActive = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room8On[2] = {22, 5};         //Time to switch on the lights
byte room8Off[2] = {22, 20};       //Time to switch off the lights

//////////Room 9 (Kitchen)//////////
byte room9MActive = 1;                     //Set to 1 if you want to process
                                           //Timer will be ignored when set to 0
byte room9OnM[2] = {5, 50};         //Time to switch on the lights
byte room9OffM[2] = {6, 45};        //Time to switch off the lights

byte room901Active = 1;                     //Set to 1 if you want to process
                                            //Timer will be ignored when set to 0
byte room9On[2] = {17, 45};         //Time to switch on the lights
byte room9Off[2] = {18, 30};        //Time to switch off the lights

///////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////DO NOT MODIVY BELOW HERE///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
////////////////////////////defining Arduino Pins/////////////////////////////
//////////////////////////////////////////////////////////////////////////////
ShiftLCD lcd(9, 11, 10);       //initializing the LCD adaptor pins

const byte latchPin = 2;        //5latch pin input connected to
                               //Arduino digital pin 2
const byte clockPin = 3;        //6clock pin input connected to
                               //Arduino digital pin 3
const byte dataPin = 4;         //7data pin input connected to 
                               //Arduino digital pin 4
                               
const byte latchPinOut = 5;     //2latch pin output shift register
                               //74HC595 connected to Arduino
                               //digital pin 5
const byte clockPinOut = 6;     //3clock pin output shift register
                               //74HC595 connected to Arduino
                               //digital pin 6
const byte dataPinOut = 7;      //4data pin output shift register
                               //74HC595 connected to Arduino
                               //digital pin 7

const int lightSensor = A0;          //defining the input for the photocell

const byte doorMonitor = 8;           //Arduino pin for a monitor LED
const byte setupMode = 12;            //Arduino pin for switching to setup mode

/////////////////////////////////////////////////////////////////////////////
///////Variables to hold the data for each input shift register//////////////
/////////////////////////////////////////////////////////////////////////////

byte switchVar1 = 0;           //data for input shift register 1
byte switchVar2 = 0;           //data for input shift register 2
byte switchVar3 = 0;           //data for input shift register 3

///////////////////////////////////////////////////////////////////////////////
///////////////////////////all the other variables/////////////////////////////
///////////////////////////////////////////////////////////////////////////////

////Sensor and timer variables
int delayTime[16] = {dBed1, dBed2, dBed3, dLiving, dBath1, dBath2, dBath3,
                     dBath4, dKitchen, dCorridor, dAC1, dAC2, dAC3, dAC4,
                     dMaster, 0};
                               
int sensorValue = 0;                          //holding the indicated sensor value of the photocell
byte photocellSwitch = 0;                    //holding the switch command after 
                                             //checking sensor readings (0, 1)
byte photocellSwitchOld = 0;                 //switch command from the previous pass
byte lightLevel[17] ={0};                    //array holding the switch state
                                             //checking timer and photocell (0, 1)


unsigned int roomTimer[17] = {0};            //array holding the time when the PIR was last activated
unsigned int currentTime = 0;                //var to hold a reference time to calculate the up time 
                                              //against the preprogrammed delay time
unsigned int endTime = 0;                    //var to hold a temp result to calculate the up time 
                                              //against the preprogrammed delay time 
unsigned int outsideOnTime = 0;              //checking result if the time is within 
                                             //on or off time limits  
                                             
////////////////////////////////////////////////////////////////////////////////////////
//////////////////////PIR and Room switch related Var's/////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////                                            

byte mainOff = 1;                             //variable for master relay control
unsigned int offTime = 0;                    //var needed to calculate delay for master off
byte masterSwitchStateOld = 0;                //var holding the previous door switch state
byte switchState[25] = {0};                   //array holding the state of each switch 
unsigned long lightOutput[17] = {0};         //array holding a integer which converted to binary 
                                              //will trigger the relay to switch in our output code
byte lightStatus[17] = {0};                   //array holding the switch status of each room on/off
byte priorityStatus[17] = {0};                 //array holding the priority status of each room on/off
byte switchState1Old = 0;                     //var to check if the priority switch state has changed
byte switchState3Old = 0;                     //var to check if the priority switch state has changed
byte switchState5Old = 0;                     //var to check if the priority switch state has changed
byte switchState7Old = 0;                     //var to check if the priority switch state has changed

//////////////////////////////////////////////////////////////////////////////
///////////////////////////Output/////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

unsigned long outputL = 0;                    //variable holding the output data

//////////////////////////////////////////////////////////////////////////////////////
///////////////////////////Service Switches///////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

byte maintenancePin = 0;                       //defining the var for the maintenance switch
byte maintenanceActive = 0;                    //holding the switch state

/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////RTC and Holiday switch timers///////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

byte room1Lights = 0;                 //var to hold the on command for room light
byte room2Lights = 0;                 //var to hold the on command for room light
byte room3Lights = 0;                 //var to hold the on command for room light
byte room4Lights = 0;                 //var to hold the on command for room light
byte room5Lights = 0;                 //var to hold the on command for room light
byte room6Lights = 0;                 //var to hold the on command for room light
byte room7Lights = 0;                 //var to hold the on command for room light
byte room8Lights = 0;                 //var to hold the on command for room light
byte room9Lights = 0;                 //var to hold the on command for room light

byte currentHour = 0;                 //var holding the time (hour 0-23)
byte currentMinute = 0;               //var holding the time (minute 0-59)
byte currentDay = 0;                  //var holding the date (day 1-31)
byte currentDoM = 0;                  //var holding the weekday (Sun - Sa, 1-7)
byte currentMonth = 0;                //var holding the date (month 1-12)
int currentYear = 0;                  //var holding the year (based on unix time)

//Array holding the day names to replace the weekday index

prog_char weekday_0[] PROGMEM = "Sun";
prog_char weekday_1[] PROGMEM = "Mon";
prog_char weekday_2[] PROGMEM = "Tue";
prog_char weekday_3[] PROGMEM = "Wed";
prog_char weekday_4[] PROGMEM = "Thu";
prog_char weekday_5[] PROGMEM = "Fri";
prog_char weekday_6[] PROGMEM = "Sat";

PROGMEM const char *weekday_table[] = {
  weekday_0,
  weekday_1,
  weekday_2,
  weekday_3,
  weekday_4,
  weekday_5,
  weekday_6
};

char buffer[20];
///////////////////////////////////////////////////////////////////////////
///////////////////Menu and user interface/////////////////////////////////
///////////////////////////////////////////////////////////////////////////

const byte btnMenu = 1;          //defining the menu button – moves through the menu
const byte btnSearch = 2;        //defining the search button – moves through values 
const byte btnSelect = 3;        //defining the select button – selects a menu or a value
const byte btnNone = 0;          //defining the non button pressed var
int act_key_in = 0;              //var holding the key related sensor reading

char buffer_M[20];

//Storing some menu messages in the program memory
prog_char msg_0[] PROGMEM = "Not Used";      
prog_char msg_1[] PROGMEM = "Saving....";
prog_char msg_2[] PROGMEM = "Setup mode";
prog_char msg_3[] PROGMEM = "Starting....";
prog_char msg_4[] PROGMEM = "RMU 1.2.5";
//Creating the table for the stored menu messages
PROGMEM const char *msg_table[] = {
  msg_0,
  msg_1,
  msg_2,
  msg_3,
  msg_4
};



void setup() {
   //////////////Start Serial for Debugging/////////////////////

   //Serial.begin(9600);
   
   //printing initialisation message
   lcd.begin(16, 2);
   lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(msg_table[4]))));
   delay(1000);
   lcd.setCursor(0, 1);
   lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(msg_table[3]))));
   delay(2000);
     
     //////////////////defining pin modes////////////////////
   
   pinMode(doorMonitor, OUTPUT);        //setting the LED pin to output
   
   pinMode(setupMode, INPUT);           //setup switch to activate menu
   
   pinMode(latchPin, OUTPUT);  //setting the latch pin to output
   pinMode(clockPin, OUTPUT);  //setting the clock pin to output
   pinMode(dataPin, INPUT);  //setting the data pin to input
   
   pinMode(latchPinOut, OUTPUT);
   pinMode(clockPinOut, OUTPUT);
   pinMode(dataPinOut, OUTPUT);
   
}  

void loop() {
 
  ///////////////////checking the setup switch/////////////////////////
  while(digitalRead(setupMode) != 0){        //stay in here if activated
    lcd.clear();                             //clear display
    lcd.print(strcpy_P(buffer_M, (char*)pgm_read_word(&(msg_table[2]))));                 //print Setup mode
    delay(500);                              //small delay
    
    button_loop();                           //function call to monitore menu buttons
  }
   //////////////////////////////////////getting the input//////////////////////////////////////////////////
   
  
   //Serial.print("switchVar1 first: ");
   //Serial.println(switchVar1, BIN);
   //Serial.print("switchVar2 first: ");
   //Serial.println(switchVar2, BIN);
   //Serial.print("switchVar3 first: ");
   //Serial.println(switchVar3, BIN);
   
   //pulse the latch pin, set to high to collect serial data
   digitalWrite(latchPin, HIGH);
   //give it chance to collect the data
   delayMicroseconds(25);
   //set latch pin to low to transmit data serially
   digitalWrite(latchPin, LOW);

   //while in serial mode, collect data into a byte
   switchVar1 = shiftIn(dataPin, clockPin);
   switchVar2 = shiftIn(dataPin, clockPin);
   switchVar3 = shiftIn(dataPin, clockPin);
   
   /////////////do something with the collected Data/////////////////////
   //checks for debugging
   //Serial.println();                       //debug only
   //Serial.print("Switch variable 1: ");    //debug only
   //Serial.println(switchVar1, BIN);        //debug only
   //Serial.println("-------------------");  //debug only
   //Serial.println();                       //debug only
   //Serial.print("Switch variable 2: ");    //debug only
   //Serial.println(switchVar2, BIN);        //debug only
   //Serial.println("-------------------");  //debug only
   //Serial.println();                       //debug only
   //Serial.print("Switch variable 3: ");    //debug only
   //Serial.println(switchVar3, BIN);        //debug only
   //Serial.println("-------------------");  //debug only
   
   ////////////loop through the 8 input pins to check their status////////////
   for(int n=0; n<=7; n++){

      //shift register 1
      if(switchVar1 & (1 << 0)) {                     //checking S1
         //Serial.println("Switch 1 was activated.");   //debug only
         switchState[0] = 1;
      }
      else {
         switchState[0] = 0;
      }

      if(switchVar1 & (1 << 1)) {                     //checking S2
         //Serial.println("Switch 2 was activated.");   //debug only
         switchState[1] = 1;
      }
      else {
         switchState[1] = 0;
      }
   
      if(switchVar1 & (1 << 2)) {                     //checking S3
         //Serial.println("Switch 3 was activated.");   //debug only
         switchState[2] = 1;
      }
      else {
         switchState[2] = 0;
      }

      if(switchVar1 & (1 << 3)) {                     //checking S4
         //Serial.println("Switch 4 was activated.");   //debug only
         switchState[3] = 1;
      }
      else {
         switchState[3] = 0;
      }

      if(switchVar1 & (1 << 4)) {                     //checking S8
         //Serial.println("Switch 8 was activated.");   //debug only
         switchState[7] = 1;
      }
      else {
         switchState[7] = 0;
      }

      if(switchVar1 & (1 << 5)) {                     //checking S7
         //Serial.println("Switch 7 was activated.");   //debug only
         switchState[6] = 1;
      }
      else {
         switchState[6] = 0;
      }

      if(switchVar1 & (1 << 6)) {                     //checking S6
         //Serial.println("Switch 6 was activated.");   //debug only
         switchState[5] = 1;
      }
      else {
         switchState[5] = 0;
      }

      if(switchVar1 & (1 << 7)) {                     //checking S5
         //Serial.println("Switch 5 was activated.");   //debug only
         switchState[4] = 1;
      }
      else {
         switchState[4] = 0;
      }

      //shift register 2

      if(switchVar2 & (1)) {                          //checking S9
         //Serial.println("Switch 9 was activated.");   //debug only
         switchState[8] = 1;
      }
      else {
         switchState[8] = 0;
      }

      if(switchVar2 & (1 << 1)) {                      //checking S10
         //Serial.println("Switch 10 was activated.");   //debug only
         switchState[9] = 1;
      }
      else {
         switchState[9] = 0;
      }

      if(switchVar2 & (1 << 2)) {                    //checking S11
         //Serial.println("Switch 11 was activated.");   //debug only
         switchState[10] = 1;
      }
      else {
         switchState[10] = 0;
      }

      if(switchVar2 & (1 << 3)) {                   //checking S12
         //Serial.println("Switch 12 was activated.");   //debug only
         switchState[11] = 1;
      }
      else {
         switchState[11] = 0;
      }

      if(switchVar2 & (1 << 4)) {                      //checking S16
         //Serial.println("Switch 16 was activated.");   //debug only
         switchState[15] = 1;
      }
      else {
         switchState[15] = 0;
      }

      if(switchVar2 & (1 << 5)) {                      //checking S15
         //Serial.println("Switch 15 was activated.");   //debug only
         switchState[14] = 1;
      }
      else {
         switchState[14] = 0;
      }

      if(switchVar2 & (1 << 6)) {                      //checking S14
         //Serial.println("Switch 14 was activated.");   //debug only
         switchState[13] = 1;
      }
      else {
         switchState[13] = 0;
      }

      if(switchVar2 & (1 << 7)) {                      //checking S13
         //Serial.println("Switch 13 was activated.");   //debug only
         switchState[12] = 1;
      }
      else {
         switchState[12] = 0;
      }

      //shift register 3

       if(switchVar3 & (1)) {                          //checking S17
         //Serial.println("Switch 17 was activated.");   //debug only
         switchState[16] = 1;
      }
      else {
         switchState[16] = 0;
      }

       if(switchVar3 & (1 << 1)) {                     //checking S18
         //Serial.println("Switch 18 was activated.");   //debug only
         switchState[17] = 1;
      }
      else {
         switchState[17] = 0;
      }
      if(switchVar3 & (1 << 2)) {                     //checking S19
         //Serial.println("Switch 19 was activated.");   //debug only
         switchState[18] = 1;
      }
      else {
         switchState[18] = 0;
      }
      
      if(switchVar3 & (1 << 3)) {                     //checking S20
         //Serial.println("Switch 20 was activated.");   //debug only
         maintenancePin = 1;
      }
      else {
         maintenancePin = 0;
      }
      
      if(switchVar3 & (1 << 4)) {                     //checking S21
         //Serial.println("Switch 20 was activated.");   //debug only
         switchState[20] = 1;
      }
      else {
         switchState[20] = 0;
      }
      
      if(switchVar3 & (1 << 5)) {                     //checking S22
         //Serial.println("Switch 21 was activated.");   //debug only
         switchState[21] = 1;
      }
      else {
         switchState[21] = 0;
      }
      
      if(switchVar3 & (1 << 6)) {                     //checking S23
         //Serial.println("Switch 22 was activated.");   //debug only
         switchState[22] = 1;
      }
      else {
         switchState[22] = 0;
      }
   }
   
  if(switchState[21] == 1){
    //Serial.println("Setup");
  } 
  //////////////Debug Statements//////////////////////////////////
  /*for(int c=0; c<21; c++){
   Serial.print("Switch state: ");
   Serial.print(c);
   Serial.print(" / ");
   Serial.println(switchState[c]);
   delay(100);
  }*/
   //////////////////checking the light status//////////////////////
  
    sensorValue = 0; 
    int reading = 0;                                  //the readings
     for(int i=0; i<15; i++){                         //take 15 readings
      reading += analogRead(lightSensor);
    }
    //average the readings
    sensorValue = reading / 15;
   //Serial.print("Sensor value: ");
   //Serial.println(sensorValue);
  
   //////////////////processing the input/////////////////////
  tmElements_t tm;               //initializing RTC
  if(RTC.read(tm)) {             //Reading the clock
    currentHour = tm.Hour;       //passing the time into a var
    currentMinute = tm.Minute;   //passing the time into a var
    currentDay = tm.Wday - 1;        //passing Weekday 
                                 //(Mon - Sun eg 1-7) into var
    currentDoM = tm.Day;         //passing day in to var (1-31)
    currentMonth = tm.Month;     //passing month into var (1-12)
    currentYear = tmYearToCalendar(tm.Year); //passing year to var
  }
  
  lcd.setCursor(0, 0);             //set the corsor to line 1 pos 1
  lcd.print("               ");    //print 15 blanks to delete all
                                   //prior statements
  lcd.setCursor(0, 1);             //set cursor to row 2 pos 1
  lcd.print(strcpy_P(buffer, (char*)pgm_read_word(&(weekday_table[currentDay]))));
  lcd.print(" ");
  if(currentDoM < 10) lcd.print("0");
  lcd.print(currentDoM);
  lcd.print(".");
  if(currentMonth < 10) lcd.print("0");
  lcd.print(currentMonth);
  lcd.print(" ");
  if(currentHour < 10) lcd.print("0"); //if the hour is less than 10
                                   //we print a 0 to keep 2 digits
  lcd.print(currentHour);          //print current time (hour)
  lcd.print(":");                  //print seperator
  if(currentMinute < 10) lcd.print("0");  //if the minute is less than
                                   //10 print 0 to keep 2 digits
  lcd.print(currentMinute);        //print current time (minutes)
  //Serial.print("Light level room 1: ");
  //Serial.println(lightLevel[0]);
  //Serial.print("Light level room 4: ");
  //Serial.println(lightLevel[3]); 
  photocellSwitch = getSensorValue(sensorValue, photoCellCutOff, 
                                   photoCellCutOn, photocellSwitchOld);
  photocellSwitchOld = photocellSwitch;
  if(photocellSwitch == 1 && currentHour >= 17 && currentHour <= 23) {
    //Serial.println("Entered first loop!");
    for(int i=0; i<9; i++){
      lightLevel[i] = 1;
    }
    lightLevel[15] = 1;
  }
  else if(photocellSwitch == 1 && currentHour >= 0 && currentHour <= 8){
    //Serial.println("Entered second loop!");
    for(int i=0; i<9; i++){
      lightLevel[i] = 1;
    }
    lightLevel[15] = 1;
  }
  else if(photocellSwitch == 0 && currentHour >= 5 && currentHour <= 8){
    //Serial.println("Entered third loop!");
    for(int c=0; c<17; c++){
      lightLevel[c] = 0;
    }
  }
  //////////////Holliday lighting/////////////////////////
    
  if(switchState[20] == 1) {       //check if the holliday switch
                                   //is activated
    lightOutput[14] = 0;           //make sure the master  relay is off
    
    //Serial.print("Current date: ");
    //Serial.print(days[currentDay - 1]);
    //Serial.print(", ");
    //Serial.print(currentDoM);
    //Serial.print("/");
    //Serial.print(currentMonth);
    //Serial.print("/");
    //Serial.println(currentYear);
    //Serial.print("Current Time: ");
    //Serial.print(currentHour);
    //Serial.print(" : ");
    //Serial.println(currentMinute);
    //Serial.print("Photo cell switch: ");
    //Serial.println(photocellSwitch);  
    //Serial.print("Light level room 1 after: ");
    //Serial.println(lightLevel[0]);
    //Serial.print("photocell switch: ");
    //Serial.println(photocellSwitch);
    //Serial.print("Light level room 3 after: ");
    //Serial.println(lightLevel[2]);
    //Serial.print("Light level room 4 after: ");
    //Serial.println(lightLevel[3]);
    ///////Room 1 (Bed 1) ///////////// 
    if(room1MActive == 1 && currentHour >= room1OnM[0] && 
       currentHour <= (room1OffM[0] + 1)){ //checking if we came passed
                                           //the hour where the lights
                                           //to be switched on
                                    
      //checking the times                              
      room1Lights = checkOnTime(room1OnM[0], room1OnM[1], 
                                room1OffM[0], room1OffM[1]);
    }
    
    
    if(room1O1Active == 1 && currentHour >= room1On1[0] && 
       currentHour <= (room1Off1[0] +1)){ //checking if we came passed
                                          //the hour where the lights
                                          //to be switched on
                                    
      //checking the times                              
      room1Lights = checkOnTime(room1On1[0], room1On1[1], 
                                room1Off1[0], room1Off1[1]);
    }
        
 
    if(room102Active == 1 && currentHour >= room1On2[0] &&
       currentHour <= (room1Off2[0] + 1)){ //checking if we came passed
                                           //the hour where the lights
                                           //to be switched on
    //checking the times                                 
      room1Lights = checkOnTime(room1On2[0], room1On2[1], 
                                room1Off2[0], room1Off2[1]);
    }
    
    if(room1Lights == 1 && lightLevel[0] == 1){   //if with in the on time
      lightOutput[0] =1;            //switch on the lights
    }
    else {
      lightOutput[0] = 0;           //other keep them off
      lightLevel[0] = 0;
    }
    
    ////////Room 2 (Bed 2)//////////////
    if(room2MActive ==1 && currentHour >= room2OnM[0] 
       && currentHour <= (room2OffM[0] + 1)){
      room2Lights = checkOnTime(room2OnM[0], room2OnM[1], 
                    room2OffM[0], room2OffM[1]);
    }
    
    if(room201Active == 1 && currentHour >= room2On1[0] && 
       currentHour <= (room2Off1[0] + 1)){
      room2Lights = checkOnTime(room2On1[0], room2On1[1], 
                    room2Off1[0], room2Off1[1]);
    }
    
    if(room2Lights == 1 && lightLevel[1] == 1){
      lightOutput[1] = 2;
    }
    else {
      lightOutput[1] = 0;
      lightLevel[1] =0;
    }
    
    ////////Room 3 (Bed 3) ////////////
    if(room3MActive == 1 && currentHour >= room3OnM[0] && 
    currentHour <= (room3OffM[0] + 1)){
      room3Lights = checkOnTime(room3OnM[0], room3OnM[1], 
      room3OffM[0], room3OffM[1]);
    }
    
    if(room301Active == 1 && currentHour >= room3On1[0] && 
       currentHour <= (room3Off1[0] + 1)){
      room3Lights = checkOnTime(room3On1[0], room3On1[1], 
      room3Off1[0], room3Off1[1]);
    }
    
    if(room302Active == 1 && currentHour >= room3On2[0] && 
    currentHour <= (room3Off2[0] + 1)){
      room3Lights = checkOnTime(room3On2[0], room3On2[1], 
      room3Off2[0], room3Off2[1]);
    }
    
    if(room3Lights == 1 && lightLevel[2] == 1){
      lightOutput[2] = 4;
    }
    else {
      lightOutput[2] = 0;
      lightLevel[2] = 0;
    }
    
    ////////Room 4 (living)/////////////////////
    if(room401Active == 1 && currentHour >= room4On[0] && 
       currentHour <= (room4Off[0] + 1)){
      room4Lights = checkOnTime(room4On[0], room4On[1], 
                    room4Off[0], room4Off[1]);
    }
    
    if(room4Lights == 1 && lightLevel[3] == 1){
      lightOutput[3] = 8;
    }
    else {
      lightOutput[3] = 0;
      lightLevel[3] = 0;
    }
    
    ////////Room 5 (Bath 1)/////////////////////
    if(room5MActive == 1 && currentHour >= room5OnM[0] && 
       currentHour <= (room5OffM[0] + 1)){
      room5Lights = checkOnTime(room5OnM[0], room5OnM[1], room5OffM[0], room5OffM[1]);
    }
        
    if(room501Active == 1 && currentHour >= room5On[0] && 
       currentHour <= (room5Off[0] + 1)){
      room5Lights = checkOnTime(room5On[0], room5On[1], room5Off[0], room5Off[1]);
    }
    
    if(room5Lights == 1 && lightLevel[4] == 1){
      lightOutput[4] = 16;
    }
    else {
      lightOutput[4] = 0;
      lightLevel[4] =0;
    }
    
    ////////Room 6 (Bath 2)/////////////////////
    if(room6MActive ==1 && currentHour >= room6OnM[0] && 
       currentHour <= (room6OffM[0] + 1)){
      room6Lights = checkOnTime(room6OnM[0], room6OnM[1], 
                                room6OffM[0], room6OffM[1]);
    }
    
    if(room601Active == 1 && currentHour >= room6On[0] && 
       currentHour <= (room6Off[0] + 1)){
      room6Lights = checkOnTime(room6On[0], room6On[1], 
                                room6Off[0], room6Off[1]);
    }
    
    if(room6Lights == 1 && lightLevel[5] == 1){
      lightOutput[5] = 32;
    }
    else {
      lightOutput[5] = 0;
      lightLevel[5] = 0;
    }
    
    ////////Room 7 (Bath 3)/////////////////////
    if(room7MActive == 1 && currentHour >= room7OnM[0] && 
       currentHour <= (room7OffM[0])){
      room7Lights = checkOnTime(room7OnM[0], room7OnM[1], 
                                room7OffM[0], room7OffM[1]);
    }
    
    if(room701Active == 1 && currentHour >= room7On[0] && 
       currentHour <= (room7Off[0])){
      room7Lights = checkOnTime(room7On[0], room7On[1], 
                                room7Off[0], room7Off[1]);
    }
    
    if(room7Lights == 1 && lightLevel[6] == 1){
      lightOutput[6] = 64;
    }
    else {
      lightOutput[6] = 0;
      lightLevel[6] = 0;
    }
    
    ////////Room 8 (Bath 4)/////////////////////
    if(room8MActive == 1 && currentHour >= room8On[0] && 
       currentHour <= (room8Off[0] + 1)){
      room8Lights = checkOnTime(room8On[0], room8On[1], 
                                room8Off[0], room8Off[1]);
    }
    
    if(room8Lights == 1 && lightLevel[7] == 1){
      lightOutput[7] = 128;
    }
    else {
      lightOutput[7] = 0;
      lightLevel[7] = 0;
    }
    
    ////////Room 9 (kitchen)/////////////////////
    if(room9MActive == 1 && currentHour >= room9OnM[0] && 
       currentHour <= room9OffM[0] + 1){
      room9Lights = checkOnTime(room9OnM[0], room9OnM[1], 
                                room9OffM[0], room9OffM[1]);
    }
    if(room901Active == 1 && currentHour >= room9On[0] && currentHour <= (room9Off[0] + 1)){
      room9Lights = checkOnTime(room9On[0], room9On[1], 
                                room9Off[0], room9Off[1]);
    }
    
    if(room9Lights == 1 && lightLevel[8] == 1){
      lightOutput[8] = 256;
    }
    else {
      lightOutput[8] = 0;
      lightLevel[8] = 0;
    }
      
      ////////Outside lights////////////////////
     outsideOnTime = checkOnTime(17, 02, hourOutsideOff, 
                                  minuteOutsideOff);    //function call to check time
     
     //Serial.print("Timer: ");                          //debug only
     //Serial.println(outsideOnTime);                    //debug only
     /*if(sensorValue <= photoOutsideOn | 
        sensorValue < (photoOutsideOn + 50)            //checking if light is
                                                       //within photocell readings
        && outsideOnTime == 1){
       lightOutput[15] = 32768;                        //switching on the lights
     }
     else {
       lightOutput[15] = 0;                            //no matches, they switch off
     }*/
     
     if(outsideOnTime == 1 && lightLevel[15] == 1){
       lightOutput[15] = 32768;
     }
     else {
       lightOutput[15] = 0;
       lightLevel[15] = 0;
     }
 
  }  
  else {
      
     ////////Outside lights////////////////////
     outsideOnTime = checkOnTime(17, 02, hourOutsideOff, 
                                 minuteOutsideOff);    //function call to check time
     //Serial.print("Timer: ");                          //debug only
     //Serial.println(outsideOnTime);                    //debug only
     /*if(sensorValue <= photoOutsideOn | 
        sensorValue < (photoOutsideOn + 50)            //checking if light is
                                                       //within photocell readings
        && outsideOnTime == 1){
       lightOutput[15] = 32768;                        //switching on the lights
     }
     else {
       lightOutput[15] = 0;                            //no matches, they switch off
     }*/
     if(outsideOnTime == 1 && lightLevel[15] == 1){
       lightOutput[15] = 32768;
     }
     else {
       lightOutput[15] = 0;
       lightLevel[15] = 0;
     }  
     //////////////////room lights//////////////////////////////
         
      lightStatus[16] = check_master(0);                 //check if doorswitch was activated room 1 (bed1)
     
      lightOutput[0] = check_light_P(0, 1, 0, 1);        //check status room 1 (bed 1)
      delay(5);
      lightStatus[16] = check_master(2);                 //check if doorswitch was activated room 2 (bed 2)
     
      lightOutput[1] = check_light_P(2, 3, 1, 2);       //check status room 2 (bed 2)
      delay(5);
      lightStatus[16] = check_master(4);                 //check if doorswitch was activated room 3 (bed 3)
      
      lightOutput[2] = check_light_P(4, 5, 2, 4);       //check status room 3 (bed 3)
      delay(5);
      lightStatus[16] = check_master(6);                 //check if doorswitch was activated room 4 (living)
     
      lightOutput[3] = check_light_P(6, 7, 3, 8);       //check status room 4 (living)
      delay(5);
      lightStatus[16] = check_master(8);                 //check if doorswitch was activated room 5 (bath 1)
      
      lightOutput[4] = check_light_N(8, 4, 16);         //check status room 5 (bath 1)
      delay(5);
      lightStatus[16] = check_master(9);                 //check if door switch was activated room 6 (bath 2)
      
      lightOutput[5] = check_light_N(9, 5, 32);
      delay(5);
      lightStatus[16] = check_master(10);
    
      lightOutput[6] = check_light_N(10, 6, 64);
      delay(5);
      lightStatus[16] = check_master(11);  
      
      lightOutput[7] = check_light_N(11, 7, 128);
      delay(5);
      lightStatus[16] = check_master(12); 
      
      lightOutput[8] = check_light_N(12, 8, 256);
      delay(5);
      lightStatus[16] = check_master(13);
    
      lightOutput[9] = check_light_N(13, 9, 512);
  
  /////////////////////Ac Read Switches////////////////////////
  
    if(switchState[14] == 1 && lightStatus[14] == 1){              //Checking if readswitches are activated
                                                                 //and the master relay is on AC room 1 (bed1)
      lightOutput[10] = 1024;                                    //providing the ability to
                                                                 //switch on the AC
      lightStatus[10] = 1;                                       //setting the light (AC) status
      roomTimer[10] = millis()/1000;                                  //setting the timer
    }
    else if(switchState[14] == 0 && lightStatus[14] == 1){       //if a door is opened and the master
                                                                 //relay is on
      currentTime = millis()/1000;                                    //setting time reference
      endTime = currentTime - roomTimer[10];                     //calculating the inactive time
      if(endTime >= delayTime[10]){                              //comparing inactive time with
                                                                 //delay time
        lightOutput[10] = 0;                                     //canceling ability to switch on the 
                                                                 //AC
        lightStatus[10] = 0;                                     //resetting the light (AC) status
        roomTimer[10] = 0;                                       //resetting the timer
      }
    }
    
  if(switchState[15] == 1 && lightStatus[14] == 1){              //Checking if readswitches are activated
                                                                 //and the master relay is on AC room 2 (bed2)
      lightOutput[11] = 2048;                                    //providing the ability to
                                                                 //switch on the AC
      lightStatus[11] = 1;                                       //setting the light (AC) status
      roomTimer[11] = millis()/1000;                                  //setting the timer
    }
    else if(switchState[15] == 0 && lightStatus[14] == 1){       //if a door is opened and the master
                                                                 //relay is on
      currentTime = millis()/1000;                                    //setting time reference
      endTime = currentTime - roomTimer[11];                     //calculating the inactive time
      if(endTime >= delayTime[11]){                              //comparing inactive time with
                                                                 //delay time
        lightOutput[11] = 0;                                     //canceling ability to switch on the 
                                                                 //AC
        lightStatus[11] = 0;                                     //resetting the light (AC) status
        roomTimer[11] = 0;                                       //resetting the timer
      }
    }  
  
  if(switchState[16] == 1 && lightStatus[14] == 1){              //Checking if readswitches are activated
                                                                 //and the master relay is on AC room 3 (bed3)
      lightOutput[12] = 4096;                                    //providing the ability to
                                                                 //switch on the AC
      lightStatus[12] = 1;                                       //setting the light (AC) status
      roomTimer[12] = millis()/1000;                                  //setting the timer
    }
    else if(switchState[16] == 0 && lightStatus[14] == 1){       //if a door is opened and the master
                                                                 //relay is on
      currentTime = millis()/1000;                                    //setting time reference
      endTime = currentTime - roomTimer[12];                     //calculating the inactive time
      if(endTime >= delayTime[12]){                              //comparing inactive time with
                                                                 //delay time
        lightOutput[12] = 0;                                     //canceling ability to switch on the 
                                                                 //AC
        lightStatus[12] = 0;                                     //resetting the light (AC) status
        roomTimer[12] = 0;                                       //resetting the timer
      }
    }  
  
  if(switchState[17] == 1 && lightStatus[14] == 1){              //Checking if readswitches are activated
                                                                 //and the master relay is on AC room 4 living
      lightOutput[13] = 8192;                                    //providing the ability to
                                                                 //switch on the AC
      lightStatus[13] = 1;                                       //setting the light (AC) status
      roomTimer[13] = millis()/1000;                                  //setting the timer
    }
    else if(switchState[17] == 0 && lightStatus[14] == 1){       //if a door is opened and the master
                                                                 //relay is on
      currentTime = millis()/1000;                                    //setting time reference
      endTime = currentTime - roomTimer[13];                     //calculating the inactive time
      if(endTime >= delayTime[13]){                              //comparing inactive time with
                                                                 //delay time
        lightOutput[13] = 0;                                     //canceling ability to switch on the 
                                                                 //AC
        lightStatus[13] = 0;                                     //resetting the light (AC) status
        roomTimer[13] = 0;                                       //resetting the timer
      }
    }
    
      /////////////Door switch control ////////////////////
    //Serial.print("switchState 18 :");
    //Serial.println(switchState[18]);
    //Serial.print("Switch state old: ");
    //Serial.println(masterSwitchStateOld);
    //Serial.print("Light status 16: ");
    //Serial.println(lightStatus[16]);
      
    if(switchState[18] != masterSwitchStateOld) {               //door switch check if the switch state
                                                                //has changed
      //Serial.println("Door switch was activated");              //debug only
      currentTime = millis()/1000;                                   //setting time reference
      lightStatus[16] = 1;                                      //setting light status
      digitalWrite(doorMonitor, HIGH);                          //setting the control LED
      for(int i=0; i<17; i++){                                  //looping through the timers
        roomTimer[i] = currentTime;                             //setting the timers
      }
    }
    else if(switchState[18] == masterSwitchStateOld && lightStatus[16] == 1){ //if the switch state
                                                                //has not changed and the lights are on
      //Serial.println("Checking off status");                    //debug only
      currentTime = millis()/1000;                                   //setting the time reference
      offTime = roomTimer[16] + delayTime[14];                  //setting the allowed delay time
      //Serial.print("off Time: ");
      //Serial.println(offTime);
      //Serial.print("current Time: ");
      //Serial.println(currentTime);
      if(currentTime >= offTime) {                              //comparing the times
        for(int c=0; c<17; c++) {                               //looping through the circuits
          if(roomTimer[c] != roomTimer[16]) {                   //comparing timers
            mainOff = 1;                                        //setting the switch off all command
            lightStatus[16] = 0;                                //switching off the master relay
          }
          else {
            mainOff = 0;                                        //if the timers match we set the 
                                                                //switch off all command to 0
            break;                                              //leaving the loop
          }
        }
      }
      //Serial.print("Main off: ");
      //Serial.println(mainOff);
      if(mainOff == 0) {                                        //master off command is 0
        //Serial.println("switching off everything and reset all"); //debug only
        for(int i=0; i<17; i++) {                               //looping through the circuits
          lightStatus[i] = 0;                                   //resetting all light status
          lightOutput[i] = 0;                                   //switching off all lights
          priorityStatus[i] = 0;                                //resetting all set priorities
          roomTimer[i] = 0;                                     //resetting all room timers
        }
        digitalWrite(doorMonitor, LOW);                         //resetting the control LED
        mainOff = 1;                                            //resetting master off command
      }
    }
    masterSwitchStateOld = switchState[18];                     //setting the switchState to old
  }  
  ///////////////////////////Output/////////////////////////////////////////////////
  
  for(int i=0; i<17; i++) {                                //loop through the light output array
    /*Serial.print("Light Output ");                        //debug only
    Serial.print(i);                                      //debug only
    Serial.print(": ");                                   //debug only
    Serial.println(lightOutput[i]);                       //debug only
    Serial.print("Light status: ");                       //debug only
    Serial.println(lightStatus[i]);                       //debug only
    Serial.print("Room Timer: ");                         //debug only
    Serial.println(roomTimer[i]);                         //debug only
    delay(500);*/
    outputL += lightOutput[i];                            //adding up the numbers
  }
  
  if(maintenancePin == 1) {              //if maintenance switch is active
    for(int i=1; i>17; i++){             //loop through all circuits
      lightStatus[i] = 1;                //setting the light status of everything
      roomTimer[i] = millis()/1000;           //setting all the room timers
    }
    outputL = 32767;                     //setting the output 
                                         //binary 0111111111111111
  }  
  
  lcd.setCursor(0,0);
  lcd.print(outputL, BIN);
  //Serial.print("Output value: ");
  //Serial.print(outputL);
  //Serial.print("   ");
  //Serial.println(outputL, BIN);
  digitalWrite(latchPinOut, LOW);                   //setting the latch pin to low to
                                                    //be able to send the data
  shiftOut(dataPinOut, clockPinOut, MSBFIRST, (outputL >> 8)); //sending the date for the 
                                                               //second shift register
  shiftOut(dataPinOut, clockPinOut, MSBFIRST, outputL);        //sending the data for the
                                                               //first shift register
  digitalWrite(latchPinOut, HIGH);                  //setting the latch pin back to
                                                    //high to finish the data transmission
  
  outputL = 0;                                      //setting the var holding the output 
                                                    //number back to 0 
  delay(sensitivity);                               //delay to adjust how responsive the
                                                    //system will react
  
}



////////////////Shift In Function for Input processing ////////////

byte shiftIn(int myDataPin, int myClockPin) {
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;
  
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
  
  for(i=7; i>=0; i--) {
    digitalWrite(myClockPin, LOW);
    delayMicroseconds(2);
    temp = digitalRead(myDataPin);
    if(temp) {
      pinState = 1;
      myDataIn = myDataIn | (1 << i);
    }
    else {
      pinState = 0;
    }
    //Serial.print("PinState: ");    //debug only
    //Serial.print(pinState);        //debug only
    //Serial.print("        ");      //debug only
    //Serial.println(myDataIn, BIN); //debug only
    
    digitalWrite(myClockPin, HIGH);
  }
  //Serial.println();                //debug only
  //Serial.println(myDataIn, BIN);   //debug only
  return myDataIn;
}


////////////function to check timer/////////
byte checkOnTime(byte hourOn, byte minuteOn, byte hourOff, byte minuteOff){
  tmElements_t tm;
  byte onTime = 0;
  long timeNow = 0;
  long onTrigger = 0;
  long offTrigger = 0;
  
  if(RTC.read(tm)){ 
    
    timeNow = tmConvert_t(tmYearToCalendar(tm.Year), tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second);
    
    onTrigger = tmConvert_t(tmYearToCalendar(tm.Year), tm.Month, tm.Day, hourOn, minuteOn, 0);
    
    if(hourOff < hourOn) {
      offTrigger = tmConvert_t(tmYearToCalendar(tm.Year), tm.Month, tm.Day+1, hourOff, minuteOff, 0);
    }
    else {
      offTrigger = tmConvert_t(tmYearToCalendar(tm.Year), tm.Month, tm.Day, hourOff, minuteOff, 0);
    }
    if(timeNow >= onTrigger && timeNow < offTrigger) {
      onTime = 1;
    }
    else {
      onTime = 0;
    }
  }
  //Serial.print("   Time now: ");
  //Serial.println(timeNow);
  //Serial.print(" On trigger: ");
  //Serial.println(onTrigger);
  //Serial.print("Off Trigger: ");
  //Serial.println(offTrigger);
  return onTime;
}

/////function to convert real time to unix timne////////
time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss){
  tmElements_t tmSet;
  tmSet.Year = YYYY - 1970;
  tmSet.Month = MM;
  tmSet.Day = DD;
  tmSet.Hour = hh;
  tmSet.Minute = mm;
  tmSet.Second = ss;
  return makeTime(tmSet);         //convert to time_t
}

byte getSensorValue(int sensorReading, int switchValue, 
                    int switchLimit, byte switchStatus){
  byte onStatus = 0;
  
  if(switchStatus == 0){
    if(sensorReading <= switchValue){
      onStatus = 1;
    }
    else if(sensorReading > switchLimit){
      onStatus = 0;
    }
    else if(sensorReading > switchValue &&
            sensorReading <= switchLimit){
      onStatus = 0;
    }
  }
  else if(switchStatus == 1){
    if(sensorReading <= switchValue){
      onStatus = 1;
    }
    else if(sensorReading > switchValue &&
            sensorReading <= switchLimit){
      onStatus = 1;
    }
    else if(sensorReading > switchLimit){
      onStatus = 0;
    }
  }
  return onStatus;
} 

byte check_master(byte swNo){
  if(switchState[swNo] == 1 && lightStatus[16] == 1) {            //checking if PIR with switch Number  was
                                                                   //activated (bed 1)
     lightStatus[16] = 0;                                      //resetting master off
     digitalWrite(doorMonitor, LOW);                           //resetting the door Monitor LED
  }
  //else {
    //lightStatus[16] = 1;
 // }
  return lightStatus[16];
}

unsigned long check_light_P(byte pir, byte prio, byte room, unsigned long light){
  if(switchState[prio] == 0 && sensorValue <= photoCellCutOff) {  //checking if S2 priority off was
                                                                   //set bed 1
      if(switchState[pir] == 1 && priorityStatus[room] == 0) {        //check if the PIR in bed 1 was
                                                                 //activated and no priority was set
         //Serial.println("We switch in the lights in bedroom 1");  //Debug only
         lightOutput[room] = light;                                     //switching on the lights – binary
                                                                 //000000000000000000000001
         lightStatus[room] = 1;                                     //setting the light status for bed 1
         lightOutput[14] = 16384;                                //make sure the master relay
                                                                 //stays on
         lightStatus[14] = 1;                                    //setting the master yelay status
         roomTimer[room] = millis()/1000;                                //setting the timer
      }
      else if(switchState[pir]  == 0 && lightStatus[room] == 1) {     //the PIR not activated but the
                                                                 //lights are on
         //Serial.println("We are checking the timer");            //Debug only
         currentTime = millis()/1000;                                 //setting time reference
         endTime = currentTime - roomTimer[room];                   //calculating the inactive time
         if(endTime >= delayTime[room]) {                           //comparing inactive time with
                                                                 //allowed delay time
            //Serial.println("Time is up switching off the lights");  //Debug only
            lightOutput[room] = 0;                                  //switching off the lights
            lightStatus[room] = 0;                                  //resetting the light status
            roomTimer[room] = 0;                                    //resetting the room timer
         }
      }
   }
   else if(switchState[prio] == 1 && lightStatus[room] == 1
           && switchState1Old != 1) {                            //if priority is activated and the
                                                                 //lights are on
      //Serial.println("Priority switch activated switching off the lights");  //Debug only
      lightOutput[room] = 0;                                        //switching off the lights
      lightStatus[room] = 0;                                        //resetting the light status
      roomTimer[room] = 0;                                          //resetting the room timer
      priorityStatus[room] = 1;                                     //setting the priority status bed 1
   }
   else if(switchState[prio] == 1 && lightStatus[room] == 0
           && switchState1Old != 1) {                               //if priority was activated and the 
                                                                 //lights are off
      //Serial.println("Priority switch deactivated switching on the lights");    //Debug only
      lightOutput[room] = light;                                         //switching on the lights
      lightStatus[room] = 1;                                        //setting the light status
      roomTimer[room] = millis()/1000;                                   //setting the room timer
      priorityStatus[room] = 0;                                     //setting the priority for bed 1 back                                                                  //to 0
   }
   switchState1Old = switchState[prio];        //passing on the switch state
   return lightOutput[room];
}

unsigned long check_light_N(byte pir, byte room, unsigned long light){
  if(switchState[pir] == 1) {                                     //checking S9 PIR of bathroom 1 
                                                                 //(room 5)
    //Serial.println("We switch on the lights");                 //Debug only
    lightOutput[room] = light;                                       //switching on the lights
    lightStatus[room] = 1;                                    //setting the light status
    lightOutput[14] = 16384;                                   //make sure the master relay
                                                               //stays on
    lightStatus[14] = 1;                                       //setting the master yelay status
    roomTimer[room] = millis()/1000;                                   //setting the room timer
  }
  else if(switchState[pir] == 0 && lightStatus[room] == 1) {       //if no PIR was activated and 
                                                              //the lights are on
    //Serial.println("We are checking the timer");              //Debug only
    currentTime = millis()/1000;                                   //setting time reference
    endTime = currentTime - roomTimer[room];                     //calculating the inactive time
    if(endTime >= delayTime[room]) {                             //comparing inactive time with 
                                                              //delay time
      //Serial.println("We are switching off the lights");      //debug only 
      lightOutput[room] = 0;                                     //switching off the lights
      lightStatus[room] = 0;                                    //resetting the light status
      roomTimer[room] = 0;                                       //resetting the room timer
    }
  }
  return lightOutput[room];
}

int read_act_buttons(){
  act_key_in = analogRead(1);      //reading the sensor
  //Readings centered at 170 for menu
  //203 for search and 252 for select
  //Serial.print("Reading: ");
  //Serial.println(act_key_in);
  if(act_key_in < 160) return btnNone; //most likely result first
  //adding a range of +/- 10 to compensate for bouncing
  //readings
  if(act_key_in > 160 && act_key_in < 180) return btnMenu;  
  if(act_key_in > 193 && act_key_in < 213) return btnSearch;
  if(act_key_in > 242 && act_key_in < 262) return btnSelect;
  return btnNone;
}

//Checking for a pressed menu button
void button_loop(){
  //Serial.println("in button loop");
  byte button = read_act_buttons();   //function call to read the buttons
  if(button == btnMenu){              //if button menu was pressed
    lcd.setCursor(0, 1);              //temp to check functionality
    lcd.print("Menu button");         //temp to check functionality
    delay(500);                       //delay for readability
  }
}

2 comments:

  1. Hi Dieter,

    I am messing around with your code a bit, but it does not compile, I think it is missing a few includes:

    room_code_final:18: error: 'time_t' does not name a type
    room_code_final:159: error: 'ShiftLCD' does not name a type
    room_code_final.ino: In function 'void setup()':
    room_code_final:323: error: 'lcd' was not declared in this scope
    room_code_final.ino: In function 'void loop()':
    room_code_final:350: error: 'lcd' was not declared in this scope
    room_code_final:609: error: 'tmElements_t' was not declared in this scope
    room_code_final:609: error: expected `;' before 'tm'
    room_code_final:610: error: 'RTC' was not declared in this scope
    room_code_final:610: error: 'tm' was not declared in this scope
    room_code_final:617: error: 'tmYearToCalendar' was not declared in this scope
    room_code_final:620: error: 'lcd' was not declared in this scope
    room_code_final.ino: In function 'byte checkOnTime(byte, byte, byte, byte)':
    room_code_final:1205: error: 'tmElements_t' was not declared in this scope
    room_code_final:1205: error: expected `;' before 'tm'
    room_code_final:1211: error: 'RTC' was not declared in this scope
    room_code_final:1211: error: 'tm' was not declared in this scope
    room_code_final:1213: error: 'tmYearToCalendar' was not declared in this scope
    room_code_final:1213: error: 'tmConvert_t' was not declared in this scope
    room_code_final.ino: At global scope:
    room_code_final:1240: error: 'time_t' does not name a type
    room_code_final.ino: In function 'void button_loop()':
    room_code_final:1391: error: 'lcd' was not declared in this scope

    ReplyDelete
  2. Hi Rene,
    that looks like a problem with the ShiftLCD and the time library. The ShiftLCD library I am using, I downloaded from here:

    http://cjparish.blogspot.com/2010/01/controlling-lcd-display-with-shift.html

    the libraries for the real time clock are from here:

    https://www.pjrc.com/teensy/td_libs_DS1307RTC.html

    Please check if you are using the same libraries and if they are present in the library folder of your IDE.

    If you have already libraries with the same name but from a different source, please back up your existing ones before adding the new ones.

    Please let me know if it solved the problem.

    ReplyDelete