Translate

Friday, 4 April 2014

Room Management System – Switching on and off some lights while on vacation


I know, it's quite a few days since my last post but I am still working on the possibility of automated adjustment for day light saving time without implementing a huge library and I got the idea of adding a LCD and the ability to change some settings without taking the chip off or connecting the controller to the computer. My idea is still to keep this system independent from any network. While I was building a LCD shield to account for the possibility to run the LCD of a shift register since I am running out of Arduino pins, I found that I am missing a couple of parts, - they are already ordered but I still need to wait until they arrive.
To cut a long weekend story short, I started with the feature to switch on and off some lights while on a caribic cruise.
One way of doing it would have been to let the controller randomly decide which lights to switch on and off which might look nice but it doesn't really make sense to me since everybody has his ow habits of moving around in the house. We for example have an open plan living area. I come home, and soon as it gets dark the lights go on in there and in the kitchen. At latest 19:00h in the kitchen the lights go off and the lights in the living area are usually switched off between 23:00h and 23:30h. Our daughter is usually in her room from 19:00h onwards and she still goes to sleep at around 20:30. Well, the lights may go on in bedroom a couple of times before for a few minutes but that can be taken care of. The only room which is used a little more often and more randomly is the bathroom. The only thing needing to be taken care off is to make sure, that the bathroom lights come on just before bed time. The outside lights stay on the usual timing since the switch on when it gets dark and switch off at a preset time. Since we don't want worry about settings for winter spring, summer and autumn, I will give the program a time frame when to switch on the lights but still run it through a photocell to make sure that in summer the lights are not coming on at six in the afternoon while it's still bright daylight. But again in winter our lights are sometimes on at 5 in the evening.
I know it's a bit of a setting up to do we don't want just switch on and off some lights, we want to give somebody watching the impression that the house is used with all the usual habits while we are away.

First thing to do, we need another switch. That's the least of our worries since we still have 4 free input channels on our CD4021B shift register no 3. If you have a careful look at the following image, I added another switch on top of the long row of switches and buttons. The centre pin is connected to the 5 V supply and the left pin is going to PI5 (pin 13) of the just mentioned shift register.






Now we start with declaring all the variables and there for we go to the top of our sketch at the end of the part where it says “Time and sensitivity settings to be changed individually”.

There we add:

///////////Vacancy Timers/////////////////////
////Room 1 (Bed 1) ///////////
unsigned int room1On1[2] = {19, 35};              //Time to switch on the lights
unsigned int room1Off1[2] = {20, 15};              //Time to switch off the lights

unsigned int room1On2[2] = {21, 5};                //Time to switch on the ;lights
unsigned int room1Off2[2] = {21, 15};              //Time to switch off the lights

////////////Room 2 (Bed 2) ///////////
unsigned int room2On1[2] = {19, 30};               //Time to switch on the lights
unsigned int room2Off1[2] = {20, 10};               //Time to switch off the lights

////////////Room 3 (Bed 3) ///////////
unsigned int room3On1[2] = {18, 10};               //time to switch on the lights
unsigned int room3Off1[2] = {18, 25};              //time to switch off the lights

unsigned int room3On2[2] = {19, 15};              //Time to switch on the lights
unsigned int room3Off2[2] = {19, 40};              //Time to switch off the lights

unsigned int room3On3[2] = {23, 20};             //Time to switch on the lights
unsigned int room3Off3[2] = {23, 35};             //Time to switch off the lights

///////////Room 4 {Living)
unsigned int room4On[2] = {17, 30};               //Time to switch on the lights
unsigned int room4Off[2] = {23, 30};               //Time to switch off the lights


From here we go down to “void setup()” and just above at the end of our variable declaration we add:

byte room1Lights = 0;                                      //light on – off command for room 1
byte room2Lights = 0;                                      //light on – off command for room 2
byte room3Lights = 0;                                      //light on – off command for room 3
byte room4Lights = 0;                                      //light on – off command for room 4

byte currentHour = 0;                                       //var holding the current time (hour)
byte currentMinute = 0;                                    //var holding the current time (minute)


Now we go way down into the main loop and just below “///////processing the input/////////” we add:

//////////////Holiday lighting/////////////////////////
if(switchState[20] == 1) {                              //check if the holiday switch
                                                                     //is activated
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
}
///////Room 1 (Bed 1) //////////////
if(currentHour >= room1On1[0]){                //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(room1Lights == 1){                                  //if with in the on time
lightOutput[0] =1;                                        //switch on the lights
}
else {
lightOutput[0] = 0;                                       //other keep them off
}
if(currentHour >= room1On2[0]){                //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){                                  //if with in the on time
lightOutput[0] =1;                                        //switch on the lights
}
else {
lightOutput[0] = 0;                                       //other keep them off
}
////////Room 2 (Bed 2)//////////////
if(currentHour >= room2On1[0]){
room2Lights = checkOnTime(room2On1[0], room2On1[1], room2Off1[0], room2Off1[1]);
}
if(room2Lights == 1){
lightOutput[1] = 2;
}
else {
lightOutput[1] = 0;
}
////////Room 3 (Bed 3) ////////////
if(currentHour >= room3On1[0]){
room3Lights = checkOnTime(room3On1[0], room3On1[1], room3Off1[0], room3Off1[1]);
}
if(room3Lights == 1){
lightOutput[2] = 4;
}
else {
lightOutput[2] = 0;
}
if(currentHour >= room3On2[0]){
room3Lights = checkOnTime(room3On2[0], room3On2[1], room3Off2[0], room3Off2[1]);
}
if(room3Lights == 1){
lightOutput[2] = 4;
}
else {
lightOutput[2] = 0;
}
////////Room 4 (living)/////////////////////
if(currentHour >= room4On[0]){
room4Lights = checkOnTime(room4On[0], room4On[1], room4Off[0], room4Off[1]);
}
if(room4Lights == 1){
lightOutput[3] =8;
}
else {
lightOutput[3] = 0;
}
Now we need to make a copy of the part below where we control the outside lights and paste it direct under the part we just added.

////////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
}

Here ends the holiday switch processing. Below HERE WE ADD!

else {
Please don't forget other wise it will only interfere with sensor switching. Another IMPORTANT step is to close our curly bracket. We go down until we find the line

masterSwitchStateOld = switchState[18]; //setting the switchState to old
///////////////////////////Output/////////////////////////////////////////////////

Right after the statement “masterSwitchStateOld = switchState[18]; “ we close the curly bracket so it looks like this:

masterSwitchStateOld = switchState[18];
}
///////////////////////////Output/////////////////////////////////////////////////

In the next article we will add the kitchen ant a couple of bathrooms.

No comments:

Post a Comment