Translate

Wednesday 21 May 2014

Room Management System – Improvements part 3


Cause we are still at it, I do have some more improvements to optimize and minimize the sketch. Today we start with adding a couple of loops to the “holiday lighting” section. Let's jump straight down to the beginning of the “holiday lighting” section in the main loop. There we have a closer look at the first room:

 //////////////Holiday lighting/////////////////////////

if(switchState[20] == 1) { //check if the holiday switch

//is activated

lightOutput[14] = 0; //make sure the master relay is off

///////Room 1 (Bed 1) /////////////

if(timer_active[0][0] == 1 && currentHour >= room_timers[0][0][0] &&

currentHour <= (room_timers[0][0][2] + 1)){ //checking if we came passed

//the hour where the lights

//to be switched on

//checking the times

room1Lights = checkOnTime(room_timers[0][0][0], room_timers[0][0][1],

room_timers[0][0][2], room_timers[0][0][3]);

}

if(timer_active[0][1] == 1 && currentHour >= room_timers[0][1][0] &&

currentHour <= (room_timers[0][1][2] + 1)){ //checking if we came passed

//the hour where the lights

//to be switched on

//checking the times

room1Lights = checkOnTime(room_timers[0][1][0], room_timers[0][1][1],

room_timers[0][1][2], room_timers[0][1][3]);

}

if(timer_active[0][2] == 1 && currentHour >= room_timers[0][2][0] &&

currentHour <= (room_timers[0][2][2] + 1)){ //checking if we came passed

//the hour where the lights

//to be switched on

//checking the times

room1Lights = checkOnTime(room_timers[0][2][0], room_timers[0][2][1],

room_timers[0][2][2], room_timers[0][2][3]);

}

if(timer_active[0][3] == 1 && currentHour >= room_timers[0][3][0] &&

currentHour <= (room_timers[0][3][2] + 1)){ //checking if we came passed

//the hour where the lights

//to be switched on

//checking the times

room1Lights = checkOnTime(room_timers[0][3][0], room_timers[0][3][1],

room_timers[0][3][2], room_timers[0][3][3]);

}

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;

}

We are going through 4 timers for every room, the same bit of code 4 times. That's again calling for some improvement. Lets shorten it a bit and run it through a for loop. If we are looking at room_timers[x][y][z] and timer_active[x][y], the place holder x is corresponding with the room number which we use a little later for further improvement. For now we look at the place holder y which is in correspondents with the timers. Having 4 timers for each room makes it again easy to do the change. We take the first part of the code

//and add

for(int i=0; i<4; i++){ //<<<<<<<<<<<<<ADD<<<<<<<<<<<<

//now we replace the second place holder with “i”

if(timer_active[0][i] == 1 && currentHour >= room_timers[0][i][0] &&

currentHour <= (room_timers[0][i][2] + 1)){ //checking if we came passed

//the hour where the lights

//to be switched on

//checking the times

room1Lights = checkOnTime(room_timers[0][i][0], room_timers[0][i][1],

room_timers[0][i][2], room_timers[0][i][3]);

}

//and we have to end the loop with a }

}

And now what the revised code for room 1 looks like:

///////Room 1 (Bed 1) /////////////

for(int i=0; i<4; i++){

if(timer_active[0][i] == 1 && currentHour >= room_timers[0][i][0] &&

currentHour <= (room_timers[0][i][2] + 1)){ //checking if we came passed

//the hour where the lights

//to be switched on

//checking the times

room1Lights = checkOnTime(room_timers[0][i][0], room_timers[0][i][1],

room_timers[0][i][2], room_timers[0][i][3]);

}

}

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;

}

OK so far? If you expected the code coming up for the rest of the room, I got to disappoint you. We go further and add a couple of variables to the declaration part in the “/all the other variables/” where it says “////Sensor and timer 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 temperatur1 = 0; //holding temperature for room 1

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)

//>>>>>>>>>>>>>ADD the two lines below<<<<<<<<<<<<<

byte roomLight[10] = {0}; //array holding the switch on command in holiday lighting

int outputValues[10] = {1,2,4,8,16,32,64,128,256,564};

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

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 

We go down a little further to the “//RTC and Holiday switch timers/” section and delete the following block of variables:

tmElements_t tm; //initializing RTC

//>>>>>>>>>>>>>DELETE from here<<<<<<<<<<<<

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 room10Lights = 0;

//>>>>>>>>>>>>>DELETE ends here<<<<<<<<<<<<<

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 - Sat, 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";

Now we go back down to the “Holiday lighting” section to room 1 where we just left off:

for(int x=0; x<10; x++){ //<<<<<<<<<<<<<ADD another for loop

//replace all the first place holders in the arrays with “x”

for(int i=0; i<4; i++){

if(timer_active[x][i] == 1 && currentHour >= room_timers[x][i][0] &&

currentHour <= (room_timers[x][i][2] + 1)){ //checking if we came passed

//the hour where the lights

//to be switched on

//checking the times

//replace room1Lights with roomLight[x]

roomLight[x] = checkOnTime(room_timers[x][i][0], room_timers[x][i][1],

room_timers[x][i][2], room_timers[x][i][3]);

}

}

//again replace room1Lights with roomLight[x]

if(roomLight[x] == 1 && lightLevel[x] == 1){ //if with in the on time

//replace 1 with outputValues[x]

lightOutput[x] = outputValues[x]; //switch on the lights

}

else {

lightOutput[x] = 0; //other keep them off

lightLevel[x] = 0;

}

}//<<<<<<<<<<<<<ADD closing braces to end the loop

Now we loop through the 10 rooms and the 4 timers at the same time. Having done this, we can delete the following statements for the next 9 rooms and the new “holiday lighting section read like this:

//////////////Holiday lighting/////////////////////////

if(switchState[20] == 1) { //check if the holiday switch

//is activated

lightOutput[14] = 0; //make sure the master relay is off

///////Room 1 (Bed 1) /////////////

for(int x=0; x<10; x++){

for(int i=0; i<4; i++){

if(timer_active[x][i] == 1 && currentHour >= room_timers[x][i][0] &&

currentHour <= (room_timers[x][i][2] + 1)){ //checking if we came passed

//the hour where the lights

//to be switched on

//checking the times

roomLight[x] = checkOnTime(room_timers[x][i][0], room_timers[x][i][1],

room_timers[x][i][2], room_timers[x][i][3]);

}

}

if(roomLight[x] == 1 && lightLevel[x] == 1){ //if with in the on time

lightOutput[x] = outputValues[x]; //switch on the lights

}

else {

lightOutput[x] = 0; //other keep them off

lightLevel[x] = 0;

}

}

////////Outside lights////////////////////

outsideOnTime = checkOnTime(17, 02, hourOutsideOff,

minuteOutsideOff); //function call to check time

if(outsideOnTime == 1 && lightLevel[15] == 1){

lightOutput[15] = 32768;

}

else {

lightOutput[15] = 0;

lightLevel[15] = 0;

}

#ifdef DA_DEBUG_holtimers

for(int x=0; x<10; x++){

for(int y=0; y<4; y++){

Serial.print("Room ");

Serial.print(x);

Serial.print(": ");

Serial.println(timer_active[x][y]);

}

}

Serial.print("Room 1 Lights: ");

Serial.println(room1Lights);

Serial.print("Room 2 Lights: ");

Serial.println(room2Lights);

Serial.print("Room 3 Lights: ");

Serial.println(room3Lights);

Serial.print("Room 4 Lights: ");

Serial.println(room4Lights);

Serial.print("Room 5 Lights: ");

Serial.println(room5Lights);

Serial.print("Room 6 Lights: ");

Serial.println(room6Lights);

Serial.print("Room 7 Lights: ");

Serial.println(room7Lights);

Serial.print("Room 8 Lights: ");

Serial.println(room8Lights);

Serial.print("Room 9 Lights: ");

Serial.println(room9Lights);

Serial.print("Room 10 Lights: ");

Serial.println(room10Lights);

#endif

}

else {

////////Outside lights////////////////////

In preparation of the planned expansion of the AC functionality we d a little more optimization. First we pack the existing code where we check the read switches into a function. This is one of the operations which are needed in daily operation. So we put the function right behind the ones where we check the room light operations unsigned long check_light_N(). Right after this one we add:

unsigned long ac_read(byte readSw, byte room, unsigned long light){

if(switchState[readSw] == 1 && lightStatus[14] == 1){ //Checking if readswitches are activated

//and the master relay is on AC

lightOutput[room] = light; //providing the ability to

//switch on the AC

lightStatus[room] = 1; //setting the light (AC) status

roomTimer[room] = millis()/1000; //setting the timer

}

else if(switchState[readSw] == 0 && lightStatus[14] == 1){ //if a door is opened and the master

//relay is on

currentTime = millis()/1000; //setting time reference

endTime = currentTime - roomTimer[room]; //calculating the inactive time

if(endTime >= delayTime[room]){ //comparing inactive time with

//delay time

lightOutput[room] = 0; //cancelling ability to switch on the

//AC

lightStatus[room] = 0; //resetting the light (AC) status

roomTimer[room] = 0; //resetting the timer

}

}

return lightOutput[room];

}

It looks pretty familiar. I have done pretty much the same as with the lights, use the existing code, put it into a function and pass the variables for switch number, room number and the light output value. Now going back up into the main loop into the “////////AC read switches///////” section. There we replace the hole section with:


/////////////////////Ac Read Switches////////////////////////

lightOutput[10] = ac_read(14, 10, 1024);

lightOutput[11] = ac_read(15, 11, 2048);

lightOutput[12] = ac_read(16, 12, 4096);

lightOutput[13] = ac_read(17, 13, 8192);

/////////////Door switch control ////////////////////

In the next post, we start with extending the AC control a little.

No comments:

Post a Comment