Translate

Monday, 31 March 2014

Room Management System – Adding a Real Time Clock


Since I finally got my real time clock module and I had a bit of a play around with it, I want to add it to the controller. To begin with I thought of something simple to start with. We'll be adding some outside lighting to the controller. Could be the garden lights or balcony lights or just the lights in front of the main door. What I would like to do is to switch the lights on controlled by the photocell but I don't want them light up the whole night. At 23:30, when I go sleep, I want them to switch off for the rest of the night.

Let's start with putting the module (tiny RTC, i2C) on to the breadboard. Soon as you have a look at the image you may notice, that I am using the relay board to add the clock. That's only because the relay board is the only one with having a bit a space left to add something. If you remember the relay example we built, just bridge the 5 V bus with the one from one of the other two boards and do the same with the ground. You can connect the input of the relay circuit to any of the used shift register outputs. Important is that you connect straight after the shift register output pin and NOT after the LED or the resistor accompanying the LED. My real time clock module came without connectors and so I had solder quickly some pins to it to attach the jumpers. Careful, the soldering pints are very small. Next step is connecting the 5V pin to the 5V bus of the breadboard and the GND pin to ground on our breadboard. The SCL pin connects to pin 28 of the Atmega 328 and the SDA pin goes to the Atmega pin 27.





Before we can do anything with the clock, we need to download a library and set the time. First things first. We need to download DS1307RTC.zip, the Time library and TimeAlarms.zip from https://www.pjrc.com/teensy/td_libs_DS1307RTC.html. Unzip all three files into the library folder of your Arduino IDE restart the IDE and run the set Time example. To test the module, you can open and run the read time example. Take a bit a time to look over the two examples. Than the upcoming code additions in the room management system will look familiar.

Today we start right on top of our sketch. We go up to line no 1 and add on top of everything

///////////////////////////////includes/////////////////////////////////////
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>

//>>>>>>>>>>end of addition<<<<<<<<<<<<<<<<<

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

from here we move down just a little, where it says:

///////////Timer and Sensitivity Settings to be changed to individual needs////////////////
unsigned int sensitivity = 500;                      //should be between 200 and 1000 as
                                                                  //lower the number as more responsive
                                                                  //the system will be
unsigned int photoCellCutOff = 280;           //var holding the value where the photocell cuts off

//>>>>>>>>>>>>>>>>>>>\\next addition starts here<<<<<<<<<<<<<<<<<<<<<<<

unsigned int photoOutsideOn = 220;           //var holding the value which the photocell reading
                                                                 // has to reach for the lights to switch on
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

//>>>>>>>>>>>>>>>>>>>>addition ends here again<<<<<<<<<<<<<<<<<<<<


Now we move down to the part where it says

////////////////all the other variables//////////////////////////////////
and on the end of this declaration block we add:

unsigned int outsideOnTime = 0;                     //checking result if the time is within
                                                                     //on or off time limits

For the next addition we need to go down a bit more and find the part:

//////////////////checking the light status//////////////////////

sensorValue = analogRead(lightSensor);            //reading the photocell

Serial.print("Sensor value: ");                            //Debug only
Serial.println(sensorValue);                               //Debug only
//////////////////processing the input/////////////////////

//>>>>>>>>>>>>>>>next addition starts here<<<<<<<<<<<<<<<<<<<<<

////////Outside lights////////////////////
outsideOnTime = checkOnTime(17, 00, hourOutsideOff, minuteOutsideOff); //function call to check time
Serial.print("Timer: ");                                                             //debug only
Serial.println(outsideOnTime);                                                 //Debug only
if(sensorValue <= photoOutsideOn | sensorValue < (photoOutsideOn + 50)  //checking the light value
&& outsideOnTime == 1){
lightOutput[15] = 32768;                                                        //switching on the lights
}
else {
lightOutput[15] = 0;                                                               //switching off the lights
}
//////////////////room lights//////////////////////////////

Last thing for today, we go all the way down to the bottom of the sketch and write a little function to check the times.

///////simple function to check on times

byte checkOnTime(byte hourOn, byte minuteOn, byte hourOff, byte minuteOff){
tmElements_t tm;
byte onTime = 0;                                               //return variable as no or off command (1,0)
long timeNow = 0;                                            //current time converted to Unix time
long onTrigger = 0;                                            //On time setting converted to Unix time
long offTrigger = 0;                                           //Off time setting converted to Unix time
if(RTC.read(tm)){                                            //reading the clock
timeNow = tmConvert_t(tmYearToCalendar(tm.Year), tm.Month, 
                                       tm.Day, tm.Hour, tm.Minute, tm.Second);      
                                                                       //function call to convert current time
onTrigger = tmConvert_t(tmYearToCalendar(tm.Year), tm.Month, 
                                      tm.Day, hourOn, minuteOn, 0); 
                                                                       //function call to convert switch on time
if(hourOff < hourOn) {                                     //checking if the off time is past midnight
offTrigger = tmConvert_t(tmYearToCalendar(tm.Year), 
                                       tm.Month, tm.Day+1, hourOff, minuteOff, 0); 
                                                                       //function call to convert switch off time
}
else {
offTrigger = tmConvert_t(tmYearToCalendar(tm.Year), 
                                       tm.Month, tm.Day, hourOff, minuteOff, 0); 
                                                                       //function call to convert switch off time before
                                                                       //midnight
}
if(timeNow >= onTrigger && timeNow < offTrigger) {               //comparing times
onTime = 1;
}
else {
onTime = 0;
}
}
Serial.print("Time now: ");                    //debug only
Serial.println(timeNow);                      //debug only
Serial.print("On trigger: ");                   //debug only
Serial.println(onTrigger);                      //debug only
Serial.print("Off Trigger: ");                  //debug only
Serial.println(offTrigger);                      //debug only

return onTime;                                     //return value on or off command (1, 0)
}


////////Function to convert real time to Unix time//////////////////
time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss)
{
tmElements_t tmSet;                                         //initializing the var holding the time to convert
tmSet.Year = YYYY – 1970;                          //year to be converted - 1970
tmSet.Month = MM;                                       //month to be converted
tmSet.Day = DD;                                            //day to be converted
tmSet.Hour = hh;                                            //hour to be converted
tmSet.Minute = mm;                                       //minute to be converted
tmSet.Second = ss;                                        //seconds to be converted
return makeTime(tmSet);                               //convert to Unix time
}

To see the results of our additions we need to add another LED to the test circuit. There for we connect a LED to the last free output of the second 74HC595 shift register with a 330 ohm resistor to ground.



After having a long play around with the real time clock and a hard time converting real time to Unix time I finally found a solution. All the standard approaches I found where I could have a peak how it is done didn't help me much since setting a time to trigger an on event and setting another time to trigger an off event will work as long as nothing goes wrong. Here in Malta we have something between 4 and 7 power cuts a year and we are dealing with people in rooms. A over filled kettle spilling water while it starts to boil, flooding the kettle base will through the circuit breaker and there we are. The clock module does have its on battery and as long as the battery is still good, it doesn't forget the time but we do not have the trigger any more to switch the light on or off. Unless we want to go through running the controller as well of a backup battery or having it run of a UPS I needed to find a way of checking also the time in between the on and off trigger.

Lets have a quick look at the added functions:
  1. byte checkOnTime(byte hourOn, byte minuteOn, byte hourOff, byte minuteOff)
    We are going in with hour and minutes we want to switch something on and off
    timeNow = tmConvert_t(tmYearToCalendar(tm.Year), tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second);
    Here we call the function tmConvert_t to convert the given real time to Unix time. In this call we just pass the readout of the clock module to get the current time.
    tmConvert_t(tmYearToCalendar(tm.Year), tm.Month, tm.Day, hourOn, minuteOn, 0);
    The same function call only this time we pass Year, Month and Day as it comes from the clock readout but the time variables are the preset times when we want to switch on the lights. The same happens with the next two function calls. The difference is that in the next call we pass the variables for the time we want the lights to switch off. The only thing we need to pay attention to is when the switch off time is passed midnight being carried to the next day. A simple if-statement is checking if the switch off time value is smaller than the switch on time value. Now, if the switch off time is passed midnight, we just add 1 day to the tm.Day readout and convert the result to Unix time.
    In the following if-statement we just need to compare the times to determine if the lights are to be switched on or off.
    if(timeNow >= onTrigger && timeNow < offTrigger)
  2. time_t tmConvert_t(int YYYY, byte MM, byte DD, byte hh, byte mm, byte ss)
    This little function was keeping busy for a whole afternoon. Since I didn't think about to deduct 1970 from the current year. I found the same function with a few valuable hints on the Arduino blog and still, it took me another 10 minutes to figure out why my one wasn't working properly and this one was. As you can see we are coming in with the complete date and time variables we want to convert. With “tmElements_t tmSet;” we initialize the variable tmSet. With the tmSet.Year, tmSet.Month, tmSet.Day....., we set the real-time time-string as we need it to be processed. The command makeTime(tmSet) converts the in tmSet contained real-time time-string in to Unix time.
Now we are good to go. In a future article we need to implement a function to accommodate for day light saving time and a possibility to update the time while the controller is connected to a network or a computer.

Sunday, 30 March 2014

Room Management System – PIR discovery


When I went shopping for a real life test of the unit, I made another discovery about PIR's. What seems to be so simple and just using a PIR with relay output, became quite difficult. Unfortunately the common PIR's on the market having a Line in, Line out and a common neutral. If it's rated for 240 Volt (Europe) well, 240 volt go in and 240 volt come out which is for our purpose not a great advantage except we want to add a relay ourself. It's possible but would just put up the costs of the unit unnecessary. Since a couple of days I am experimenting with the motion sensor coming with some of the Arduino starter kits, the HC-SR501. Advantage of this sensor is, that it runs of our 5 V power supply and the output can go straight into the shift register input. I also have 3 PIR's running in parallel without blocking diodes and it works just fine.

First we need to make the connections to the last 5 Volt and Ground bus. Then pin 1 from our PIR connects to ground, pin 3 to the 5 Volt bus and the middle one (pin 2) connects to the pin of our S1 which goes to the input pin (PI 1) of the first CD4021B shift register. Since I couldn't find a fitting diode in my electronic box, I am running 3 PIR's parallel without any problems. But for active use as shown in the image it would definitely be good practice to put a diode like a 1N4148 in the PIR's output.



Again a small warning at this stage. Please do not attempt to connect all PIR's you are planning to use to the circuit as shown. The same goes for relays. Connect 1 for demonstration purposes and 2 to 3 PIR's and you are fine. If you like to connect more, please check carefully the power consumption not to blow your Arduino board.

Thursday, 27 March 2014

Room Management System – Taking the Atmega of the Arduino board


This article was meant to come before and somehow I've skipped it on my to do list. However, for everybody wanting to add the microprocessor direct on to the controller without the use of an Arduino board.
First we look at the option with a stabilized 5 Volt power supply so we don't need to worry about voltage regulators.
Let's put it on the breadboard first. We start with placing the Atmega chip on the board and connect the pins 7, 20 and 21 to the 5V bus and the pins 8 and 22 to ground. In the same process we bridge the 5 V bus from the left with the 5 V bus on the right of our breadboard and we do the same with the ground. I also added a power socket where the 5 V power supply plugs into.





Next we add a 10K resistor from the 5V bus to pin 1 of the Atmega to prevent it from resetting itself. A 16 MHz crystal goes between the Atmega pins 9 and 10 and 22 pF capacitor connects to ground from each of the crystal pins. Finally we place a reset button on top of the chip and connect one pin to the Atmega pin 1 and the other one to Ground.





You may add a LED at the digital pin 13 like on the original Arduino board but unless you want to carry on and add a USB to Serial converter and test new chips without a program on it there is no real need for it. However, I add the option anyway. Just add a LED with a 330 ohm resistor to ground to the Atmega pin 19 and you are done.





If you like to connect a stabilized 5 Volt power supply you can stop here. If you are planning to run the unit of anything between 7.5 and 12 Volt you need to add a voltage regulator. In this example I am using again the 1.5 A rated 7805 regulator. Please be advised, that the 7805 will not run the whole unit with relays, LED's and PIR's. For further reference please check my article “Adding a power supply”. The voltage regulator connects with the output pin to the 5V bus, the middle pin to ground and the input pin to any DC power supply giving between 7.5 and 12 Volt. Between the input pin and ground we connect a 100 uF electrolytic capacitor and between the output pin and ground a 10 uF electrolytic capacitor. Optional you can connect a LED with a 330 ohm resistor between the 5V bus and ground as power indicator.


To connect everything to our room management system we need to take of the input shift registers latch pin from Arduino's input D2 and add it to the Atmega's chip pin 4, the clock pin from Arduino's input D3 goes to Atmega pin 5 and the data pin of the input shift register changes from Arduino input D4 to Atmega pin 6. The next thing we change are the inputs from the output shift registers. The latch pin moves from Arduino D5 to Atmega pin 11. The clock pin goes from Arduino D6 to Atmega pin 12 and the data pin moves from Arduino D7 to Atmega pin 13. I have been already working on some further development and since I am in need of using D13 for a different purpose, the doors witch indicator LED had to move to D8. That means we have to put the indicator LED connected to Arduino D13 to Atmega pin 14. We also have to change the variable declaration in our sketch. We look below the line where it says “All the other Variables” and find the entry:

doorMonitor = 13;

and change it to

doorMonitor = 8;

The input of the photocell moves from Arduino's A0 to Atmega pin 23. Finally we just have to connect the ground to any common ground of the Atmega board and the 5V bus to the output pin of voltage regulator. Now connect any DC power supply between 7.5 and 12 Volt on common Ground and the input pin of the voltage regulator and the unit is up and running without the Arduino board.



If you go for the option with a stabilized 5 Volt DC power supply just forget about the voltage regulator and connect the 5V to the common ground and any 5V bus since we have them all inter connected.


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];