Translate

Tuesday 25 March 2014

Room Management System – Updating the code for the priority switches


I discovered a small problem in the room having the priority button to stop the lights from coming on while asleep. If somebody was pressing the button for a extended period of time the lights where going on and off every time the processor was cycling through the loop. To stop this from happening we have to add a few lines of code to the rooms with priority switch.

We start again on the top of our code where we declare the variables and find the part:

////////////////all the other variables///////////////////////////
unsigned long delayTime[16] = {dBed1, dBed2, dBed3, dLiving, dBath1, dBath2, dBath3,
                                                 dBath4, dKitchen, dCorridor, dAC1, dAC2, dAC3, dAC4,
                                                 dMaster, 0};
int lightSensor = A0;                                            //defining the input for the photocell
int sensorValue = 0;                                             //holding the indicated sensor value of the photocell
unsigned long outputL = 0;                                   //variable holding the output data
unsigned int mainOff = 1;                                    //variable for master relay control
unsigned long offTime = 0;                                  //var needed to calculate delay for master off
unsigned int masterSwitchStateOld = 0;              //var holding the previous door switch state
int doorMonitor = 8;                                          //Arduino pin for a monitor LED
unsigned int 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
unsigned int lightStatus[17] = {0};                     //array holding the switch status of each room on/off
unsigned long roomTimer[17] = {0};                 //array holding the time when the PIR was last activated
unsigned int priorityStatus[17] = {0};                //array holding the priority status of each room on/off
unsigned long currentTime = 0;                          //var to hold a reference time to calculate the up time
                                                                        //against the preprogrammed delay time
unsigned long endTime = 0;                              //var to hold a temp result to calculate the up time
                                                                       //against the preprogrammed delay time
int maintenancePin = 0;                                    //defining the var for the maintenance switch
int maintenanceActive = 0;                               //holding the switch state

//>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>addition Starts here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

unsigned int switchState1Old = 0;                    //var to check if the switch state has changed
unsigned int switchState3Old = 0;                    //var to check if the switch state has changed
unsigned int switchState5Old = 0;                    //var to check if the switch state has changed
unsigned int switchState7Old = 0;                    //var to check if the switch state has changed


//>>>>>>>>>>>>>>>>>>>addition Ends here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

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

Serial.begin(9600);
//////////////////defining pin modes////////////////////

I am adding four switchStateOld statements with the number of the corresponding switch in it. There we just check if the switch state has changed and if it has we carry on with what we are supposed to do if not, somebody is keeping the switch pressed and the system ignores it until the state changes.
Now we have to go down in our code to the point where it says “Processing the Input”. Please look out for this lines
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Below there we need to add...

//////////////////processing the input/////////////////////
if(switchState[0] == 1 && lightStatus[16] == 1) {                       //checking if PIR in Room 1 was
                                                                                                  //activated (bed 1)
lightStatus[16] = 0;                                                                     //resetting master off
digitalWrite(doorMonitor, LOW);                                               //resetting the door Monitor LED
}
if(switchState[1] == 0 && sensorValue <= photoCellCutOff) { //checking if S2 priority off was
                                                                                                //set bed 1
if(switchState[0] == 1 && priorityStatus[0] == 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[0] = 1;                                                                  //switching on the lights – binary
                                                                                              //000000000000000000000001
lightStatus[0] = 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[0] = millis();                                                          //setting the timer
}
else if(switchState[0] == 0 && lightStatus[0] == 1) {              //the PIR not activated but the
                                                                                              //lights are on
Serial.println("We are checking the timer");                              //Debug only
currentTime = millis();                                                             //setting time reference
endTime = currentTime - roomTimer[0];                                  //calculating the inactive time
if(endTime >= delayTime[0]) {                                               //comparing inactive time with
                                                                                             //allowed delay time
Serial.println("Time is up switching off the lights");                   //Debug only
lightOutput[0] = 0;                                                                 //switching off the lights
lightStatus[0] = 0; //resetting the light status
roomTimer[0] = 0; //resetting the room timer
}
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//in to the if statement we need to add “&& switchState1Old != 1”<<<<<<<<<<<<

else if(switchState[1] == 1 && lightStatus[0] == 1
&& switchState1Old != 1) {                                                //if priority is activated and the
                                                                                            //lights are on
//Serial.println("Priority switch activated switching off the lights"); //Debug only
lightOutput[0] = 0;                                                               //switching off the lights
lightStatus[0] = 0;                                                                //resetting the light status
roomTimer[0] = 0;                                                               //resetting the room timer
priorityStatus[0] = 1;                                                            //setting the priority status bed 1
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//in to the if statement we need to add “&& switchState1Old != 1”<<<<<<<<<<<<<
else if(switchState[1] == 1 && lightStatus[0] == 0
&& switchState1Old != 1) {                                               //if priority was activated and the
                                                                                          //lights are off
//Serial.println("Priority switch deactivated switching on the lights");   //Debug only
lightOutput[0] =1;                                                               //switching on the lights
lightStatus[0] = 1;                                                               //setting the light status
roomTimer[0] = millis();                                                      //setting the room timer
priorityStatus[0] = 0;                                                          //setting the priority for bed 1 back //to 0
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Just add the line below<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
switchState1Old = switchState[1];                                     //passing on the switch state


This changes we have to make also for the 3 remaining rooms with priority switch.



if(switchState[2] == 1 && lightStatus[16] == 1) {             //checking if PIR in Room 2 was
                                                                                        //activated (bed 2)
lightStatus[14] = 0;                                                           //resetting master off
digitalWrite(doorMonitor, LOW);                                     //resetting the door Monitor LED
}
if(switchState[3] == 0 && sensorValue <= photoCellCutOff){ //checking if S4 priority off was
                                                                                        //set bed 2
if(switchState[2] == 1 && priorityStatus[1] == 0){           //check if the PIR in bed 2 was
                                                                                        //activated (S3)
//Serial.println("We switch on the lights");                           //debug only
lightOutput[1] = 2;                                                            //switch on the lights
                                                                                        //Binary 0000000000000010
lightStatus[1] = 1;                                                             //setting the light status
lightOutput[14] = 16384;                                                 //make sure the master relay
                                                                                       //stays on
lightStatus[14] = 1;                                                          //setting the master yelay status
roomTimer[1] = millis();                                                   //setting the timer
}
else if(switchState[2] == 0 && lightStatus[1] == 1) {     //the PIR not activated but the
                                                                                      //the lights are on
//Serial.println("We are checking the timer");                    //debug only
currentTime = millis();                                                     //setting time reference
endTime = currentTime - roomTimer[1];                         //calculating the inactive time
if(endTime >= delayTime[1]) {                                       //comparing inactive time with
//Serial.println("Time is up we switch the lights off");        //debug only
lightOutput[1] = 0;                                                        //switching off the lights
lightStatus[1] = 0;                                                         //resetting the light status
roomTimer[1] = 0;                                                       //resetting the room timer
}
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
else if(switchState[3] == 1 && lightStatus[1] == 1
&& switchState3Old != 1) {                                             //if priority is activated and the
                                                                                        //lights are on
//Serial.println("Priority switch activated, switching off the lights"); //debug only
lightOutput[1] = 0;                                                           //switching off the lights
lightStatus[1] = 0;                                                            //resetting the light status
roomTimer[1] = 0;                                                          //resetting the room timer
priorityStatus[1] = 1;                                                      //setting the priority status for
                                                                                      //bed 2
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
else if(switchState[3] == 1 && lightStatus[1] == 0
&& switchState3Old != 1) {                                          //if priority is activated and the
                                                                                      //lights are off
//Serial.println("Priority switch off, switching the light back to normal"); //debug only
lightOutput[1] = 2;                                                          //switching ion the lights
lightStatus[1] = 1;                                                           //setting the light status
roomTimer[1] = millis();                                                  //setting the room timer
priorityStatus[1] = 0;                                                       //resetting the priority status
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
switchState3Old = switchState[3];


if(switchState[4] == 1 && lightStatus[16] == 1) {        //checking if PIR in Room 3 was
                                                                                    //activated (bed 3)
lightStatus[14] = 0;                                                       //resetting master off
digitalWrite(doorMonitor, LOW);                                 //resetting the door Monitor LED
}
if(switchState[5] == 0 && sensorValue <= photoCellCutOff){ //checking if S6 priority off was
                                                                                     //set bed 3
if(switchState[4] == 1 && priorityStatus[2] == 0){        //check if the PIR in bed 3 was
                                                                                     //activated (S5)
//Serial.println("We switch on the lights");                        //debug only
lightOutput[2] = 4;                                                         //switch on the lights
                                                                                     //Binary 0000000000000100
lightStatus[2] = 1;                                                          //setting the light status
lightOutput[14] = 16384;                                              //make sure the master relay
                                                                                    //stays on
lightStatus[14] = 1;                                                      //setting the master yelay status
roomTimer[2] = millis();                                               //setting the timer
}
else if(switchState[4] == 0 && lightStatus[2] == 1) { //the PIR not activated but the
                                                                                  //the lights are on
//Serial.println("We are checking the timer");                 //debug only
currentTime = millis();                                                  //setting time reference
endTime = currentTime - roomTimer[2];                      //calculating the inactive time
if(endTime >= delayTime[2]) {                                    //comparing inactive time with
//Serial.println("Time is up we switch the lights off");     //debug only
lightOutput[2] = 0;                                                      //switching off the lights
lightStatus[2] = 0;                                                       //resetting the light status
roomTimer[2] = 0;                                                      //resetting the room timer
}
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
else if(switchState[5] == 1 && lightStatus[2] == 1
&& switchState5Old != 1) {                                      //if priority is activated and the
                                                                                  //lights are on
//Serial.println("Priority switch activated, switching off the lights"); //debug only
lightOutput[2] = 0;                                                     //switching off the lights
lightStatus[2] = 0;                                                       //resetting the light status
roomTimer[2] = 0;                                                     //resetting the room timer
priorityStatus[2] = 1;                                                  //setting the priority status for
                                                                                  //bed 3
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
else if(switchState[5] == 1 && lightStatus[2] == 0
&& switchState5Old != 1) {                                     //if priority is activated and the
                                                                                //lights are off
//Serial.println("Priority switch off, switching the light back to normal"); //debug only
lightOutput[2] = 4;                                                   //switching ion the lights
lightStatus[2] = 1;                                                    //setting the light status
roomTimer[2] = millis();                                           //setting the room timer
priorityStatus[2] = 0;                                               //resetting the priority status
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
switchState5Old = switchState[5];

if(switchState[6] == 1 && lightStatus[16] == 1) { //checking if PIR in Room 4 was
                                                                            //activated (living)
lightStatus[16] = 0;                                               //resetting master off
digitalWrite(doorMonitor, LOW);                         //resetting the door Monitor LED
}
if(switchState[7] == 0 && sensorValue <= photoCellCutOff){ //checking if S8 priority off was
                                                                          //set living
if(switchState[6] == 1 && priorityStatus[3] == 0){ //check if the PIR in living was
                                                                         //activated (S7)
//Serial.println("We switch on the lights");            //debug only
lightOutput[3] = 8;                                             //switch on the lights
                                                                         //Binary 0000000000001000
lightStatus[3] = 1;                                              //setting the light status
lightOutput[14] = 16384;                                  //make sure the master relay
                                                                        //stays on
lightStatus[14] = 1;                                           //setting the master yelay status
roomTimer[3] = millis();                                    //setting the timer
}
else if(switchState[6] == 0 && lightStatus[3] == 1) { //the PIR not activated but the
                                                                       //the lights are on
//Serial.println("We are checking the timer");     //debug only
currentTime = millis();                                      //setting time reference
endTime = currentTime - roomTimer[3];           //calculating the inactive time
if(endTime >= delayTime[3]) {                         //comparing inactive time with
                                                                       //delay time
//Serial.println("Time is up we switch the lights off"); //debug only
lightOutput[3] = 0;                                           //switching off the lights
lightStatus[3] = 0;                                            //resetting the light status
roomTimer[3] = 0;                                          //resetting the room timer
}
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
else if(switchState[7] == 1 && lightStatus[3] == 1
&& switchState7Old != 1) {                          //if priority is activated and the
                                                                     //lights are on
//Serial.println("Priority switch activated, switching off the lights"); //debug only
lightOutput[3] = 0;                                         //switching off the lights
lightStatus[3] = 0;                                          //resetting the light status
roomTimer[3] = 0;                                         //resetting the room timer
priorityStatus[3] = 1;                                     //setting the priority status for
                                                                     //living
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
else if(switchState[7] == 1 && lightStatus[3] == 0
&& switchState7Old != 1) {                         //if priority is activated and the
                                                                    //lights are off
//Serial.println("Priority switch off, switching the light back to normal"); //debug only
lightOutput[3] = 8;                                        //switching on the lights
lightStatus[3] = 1;                                         //setting the light status
roomTimer[3] = millis();                                //setting the room timer
priorityStatus[3] = 0;                                    //resetting the priority status
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
switchState7Old = switchState[7];

No comments:

Post a Comment