Translate

Saturday 5 April 2014

Room Management System – Part 2 of Switching some lights while on vacation


In the last post we took care of the bedrooms and the living area. Our first task today is to add a few more statements, taking care of the bathrooms and the kitchen. There fore, we start at the declaration part of our sketch and where we have been defining the times when to switch on and off a light in a particular room, we add:

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

//>>>>>>>>>>>>>Addition starts here<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//////////Room 5 (bath 1)//////////
unsigned int room5On[2] = {19, 55};
unsigned int room5Off[2] = {20, 10};

//////////Room 6 (bath 2)//////////
unsigned int room6On[2] = {19, 50};
unsigned int room6Off[2] = {20, 05};

//////////Room 7 (bath 3)//////////
unsigned int room7On[2] = {22, 50};
unsigned int room7Off[2] = {23, 15};

//////////Room 8 (bath 4)//////////
unsigned int room8On[2] = {22, 5};
unsigned int room8Off[2] = {22, 20};

//////////Room 9(Kitchen)//////////
unsigned int room9On[2] = {17, 45};
unsigned int room9Off[2] = {18, 30};

//>>>>>>>>>>>>>Addition ends here<<<<<<<<<<<<<<<<<<<<<<<<<

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

//////////defining Arduino Pins/////////////////
const int latchPin = 2;                  //5latch pin input connected to
                                                   //Arduino digital pin 2
const int clockPin = 3;                 //6clock pin input connected to

I think it's pretty self explaining but still a few words what's going on.
The room(n)On[2] array holds to 2 numbers. For example {17, 45} would mean the lights would switch at 17:45 h. The first value in the array is always the hour in 24 hour time format from 0 to 23. The second value indicates the minutes from 0 to 59.
room9On[2] = {17, 45}; Setting the time to switch on the lights of room 9 to 17:45 h
room9Off[2] = {18, 30}; Setting the time to switch off the lights of room 9 to 18: 30h

From here we go further down, again to the end of our declaration part:


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

//>>>>>>addition starts here<<<<<<<<<<<<<<<<<<<<<<<<<<<
byte room5Lights = 0;        //light on – off command for room5
byte room6Lights = 0;       //light on – off command for room 6
byte room7Lights = 0;       //light on – off command for room 7
byte room8Lights = 0;       //light on – off command for room 8
byte room9Lights = 0;       //light on – off command for room 9
//>>>>>>>>addition ends here<<<<<<<<<<<<<<<<<<<<<<


byte currentHour = 0;
byte currentMinute = 0;
byte currentDay = 0;

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

Serial.begin(9600);

The variable room(n)Lights is holding only 0 for the lights have to stay off or 1 for “Yes we are with in the time limits”, the lights can be switched on.

From here we make our way down again in to the main loop in the part where it says “Holiday lighting”. There we carry on after “/////////room 4////////”.

////////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;
}
//>>>>>>>>>>>>>addition starts here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
////////Room 5 (Bath 1)/////////////////////
if(currentHour >= room5On[0]){
room5Lights = checkOnTime(room5On[0], room5On[1], room5Off[0], room5Off[1]);
}
if(room5Lights == 1){
lightOutput[4] = 16;
}
else {
lightOutput[4] = 0;
}
////////Room 6 (Bath 2)/////////////////////
if(currentHour >= room6On[0]){
room6Lights = checkOnTime(room6On[0], room6On[1], room6Off[0], room6Off[1]);
}
if(room6Lights == 1){
lightOutput[5] = 32;
}
else {
lightOutput[5] = 0;
}
////////Room 7 (Bath 3)/////////////////////
if(currentHour >= room7On[0]){
room7Lights = checkOnTime(room7On[0], room7On[1], room7Off[0], room7Off[1]);
}
if(room7Lights == 1){
lightOutput[6] = 64;
}
else {
lightOutput[6] = 0;
}
////////Room 8 (Bath 4)/////////////////////
if(currentHour >= room8On[0]){
room8Lights = checkOnTime(room8On[0], room8On[1], room8Off[0], room8Off[1]);
}
if(room8Lights == 1){
lightOutput[7] = 128;
}
else {
lightOutput[7] = 0;
}
////////Room 9 (kitchen)/////////////////////
if(currentHour >= room9On[0]){
room8Lights = checkOnTime(room9On[0], room9On[1], room9Off[0], room9Off[1]);
}
if(room9Lights == 1){
lightOutput[8] = 256;
}
else {
lightOutput[8] = 0;
}
//>>>>>>>>>>>>>>>>>>>addition ends here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
////////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
}
}
else {


For now we have been only looking into light switching in the evening. For everybody getting usually up early in the morning, there might be a need for doing a few lights also in the morning.

Lets start again with the bedrooms. Children have to catch the school bus in the morning and they leave usually quite early. We go back to the time declarations and add under Room 1:

//////////////////////holiday timer settings//////////////////////

////////////Room 1 (Bed 1) ///////////
//>>>>>>>>>addition starts here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
unsigned int room1OnM[2] = {5, 35};                //Time to switch on the lights
unsigned int room1OffM[2] = {6, 15};                //Time to switch off the lights
//>>>>>>>>>addition ends here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
unsigned int room1On1[2] = {19, 35};               //Time to switch on the lights
unsigned int room1Off1[2] = {20, 15};               //Time to switch off the lights

The bathroom to bedroom 1 is room 5. Let's switch on the lights in the bathroom as well for a few minutes. There fore we find the time declarations for room 5 and add:

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

//////////Room 5 (bath 1)//////////
//>>>>>>>>>>>>>>>>>>>addition starts here<<<<<<<<<<<<<<<<<<<<<<<<
unsigned int room5OnM[2] = {5, 40};
unsigned int room5OffM[2] = {5, 45};
//>>>>>>>>>>>>>>>>>>addition ends here<<<<<<<<<<<<<<<<<<<<<<<<<
unsigned int room5On[2] = {19, 55};
unsigned int room5Off[2] = {20, 10};

//////////Room 6 (bath 2)//////////

While the lights go off in the bathroom, some action in the kitchen starts. So we add to room 9:

//////////Room 8 (bath 4)//////////
unsigned int room8On[2] = {22, 5};
unsigned int room8Off[2] = {22, 20};

//////////Room 9 (Kitchen)//////////
//>>>>>>>>>>>>>>>>>>>>>addition starts here<<<<<<<<<<<<<<<<<<<<<<<
unsigned int room9OnM[2] = {5, 50};
unsigned int room9OffM[2] = {6, 45};
//>>>>>>>>>>>>>>>>>>>>>addition ends here<<<<<<<<<<<<<<<<<<<<<<<
unsigned int room9On[2] = {17, 45};
unsigned int room9Off[2] = {18, 30};
////////////////////////////DO NOT MODIVY BELOW HERE///////////////////////////////////////

From here we move down again to the main loop in the part where we process the holiday lighting. We start off with room 1:

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

//>>>>>>>>>>>>>>>>>>>>>addition starts here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if(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(room1Lights == 1){                                      //if with in the on time
lightOutput[0] =1;                                            //switch on the lights
}
else {
lightOutput[0] = 0;                                            //other keep them off
}
//>>>>>>>>>>>>>>>>>>>addition ends here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if(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(room1Lights == 1){                             //if with in the on time
lightOutput[0] =1;                                   //switch on the lights
}
else {
lightOutput[0] = 0;                                 //other keep them off
}


You may have noticed a little change in the processing code. Up until now we where only checking if we passed the hour where the light have to be switched on to do the further processing which was OK as long as we where only switching the lights in the evening. Since we switch on lights in the morning as well, we restrict the time for further processing with in the switch on and off time. That means, we have to change the if-statement “if(currentHour >= room1OnM[0] ) {“. We just add “&& currentHour <= (room1OffM[0] + 1)”. We have to add 1 hour to the switch off hour since we have to account for anything from 0 to 59 minutes past the indicated switch on hour. Now the if-statement looks like “if(currentHour >= room1OnM[0] && currentHour <= (room1OffM[0] +1))”.
We do this change with all this statements in the holiday switch processing part. Please take care to get the numbers right. If the on hour is room1On1[0] the off hour has to be room1Off1[0].

OK, done?

Than we move down to room 5 and add:

////////Room 5 (Bath 1)/////////////////////
//>>>>>>>>>>>>>>addition starts here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if(currentHour >= room5OnM[0] && currentHour <= (room5OffM[0] + 1)){
room5Lights = checkOnTime(room5OnM[0], room5OnM[1], room5OffM[0], room5OffM[1]);
}
if(room5Lights == 1){
lightOutput[4] = 16;
}
else {
lightOutput[4] = 0;
}
//>>>>>>>>>>>>>>>>addition ends here<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if(currentHour >= room5On[0] && currentHour <= (room5Off[0] + 1)){
room5Lights = checkOnTime(room5On[0], room5On[1], room5Off[0], room5Off[1]);

from here we move down to the kitchen (room 9) and to the same:

////////Room 9 (kitchen)/////////////////////
//>>>>>>>>>>>>>>>>>addition starts here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if(currentHour >= room9OnM[0] && currentHour <= room9OffM[0] + 1){
room9Lights = checkOnTime(room9On[0], room9On[1], room9Off[0], room9Off[1]);
}
//>>>>>>>>>>>>>>>>>>addition ends here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if(currentHour >= room9On[0] && currentHour <= (room9Off[0] + 1)){
room9Lights = checkOnTime(room9On[0], room9On[1], room9Off[0], room9Off[1]);
}
if(room9Lights == 1){
lightOutput[8] = 256;
}
else {
lightOutput[8] = 0;
}

It is pretty simple and I think it is not a big deal to carry on and individually put in the code for the other rooms as needed.

One more thing I have just noticed. Every time I check a timer if the lights have to be on or off, I also pass it on to the actual output command. If we have in 1 room for example the lights switching 5 times, we process the output command also 5 times. We need to do this only once after all the timers have been checked.
I am talking about this part:

if(room9Lights == 1){
lightOutput[8] = 256;
}
else {
lightOutput[8] = 0;
}

Please change your statements as on the following example of room 1.

///////Room 1 (Bed 1) /////////////
if(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]);
}
////>>>>>>>>>>>>>Delete from here<<<<<<<<<<<<<<<<<<<<<<<<
if(room1Lights == 1){                                   //if with in the on time
lightOutput[0] =1;                                         //switch on the lights
}
else {
lightOutput[0] = 0;                                        //other keep them off
}
/////>>>>>>>>>>>>...to here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if(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]);
}
//>>>>>>>>>>>>>>>>>Delete from Here<<<<<<<<<<<<<<<<<<<<<<<
if(room1Lights == 1){                               //if with in the on time
lightOutput[0] =1;                                      //switch on the lights
}
else {
lightOutput[0] = 0;                                   //other keep them off
}
//>>>>>>>>>>>>>>>>>to here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
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]);
}
//>>>>>>IMPORTANT – DO NOT DELETE THE LAST ONE<<<<<<<<<<<
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) /////////////

Since I have a very busy weekend, I have to delay my plans a little to post a complete updated sketch again. But hopefully some time next week I will manage, since we made quite a few changes and additions since the last full code.
Plans for the next post are a easy way to enable or disable rooms for holiday switching and checking the photocell since there is no need for lights to switch on or off while there is still bright day light, - not even while we are away for a cruise.

No comments:

Post a Comment