Translate

Friday 14 March 2014

Room Management System – Coding the processing - part 5


Today we will look at the code for the master relay which is controlled by a read switch in the main door, a timer and all the PIR's in the apartment. Let's have a quick review how it is supposed to work.
If the main door was opened we want to check if still somebody is in the apartment. If no more human presence is sensed, we want to give it a bit a time so somebody hiding in a corner has the chance to move and activate one of the PIR's to tell the system not to switch off all the lights. If finally no more presence is sensed the master relay will switch off all the lights including selected circuits with manually maintained lights. Since I have a hotel room in mind with this design we also want to switch off the AC's if nobody is in the room.

Again, we start coding at the bottom of our main loop:

else if(switchState[17] == 0 && lightStatus[14] == 1){                            //if a door is opened and the 
                                                                                                               //master relay is on
currentTime = millis();                                                                              //setting time reference
endTime = currentTime - roomTimer[13];                                                //calculating the inactive time
if(endTime >= delayTime[13]){                                                               //comparing inactive time with
                                                                                                              //delay time
lightOutput[13] = 0;                                                                               //cancelling ability to switch on the
                                                                                                             //AC
lightStatus[13] = 0;                                                                                //resetting the light (AC) status
roomTimer[13] = 0;                                                                               //resetting the timer
}
}

//>>>>>>>>>>>>>>>>>Again we start coding here<<<<<<<<<<<<<<<<<<<<<<

/////////////Door switch control ////////////////////
if(switchState[18] != masterSwitchStateOld) {                                     //door switch check if the switch
                                                                                                           //state has changed
Serial.println("Door switch was activated");                                          //debug only
currentTime = millis();                                                                          //setting time reference
lightStatus[16] = 1;                                                                              //setting light status
digitalWrite(doorMonitor, HIGH);                                                        //setting the control LED
for(int i=0; i<17; i++){                                                                         //looping through the timers
roomTimer[i] = currentTime;                                                                //setting the timers
}
}
else if(switchState[18] == masterSwitchStateOld && lightStatus[16] == 1){ //if the switch state
                                                                                                          //has not changed and the lights are on
Serial.println("Checking off status");                                                    //debug only
currentTime = millis();                                                                         //setting the time reference
offTime = roomTimer[16] + delayTime[14];                                        //setting the allowed delay time
if(currentTime >= offTime) {                                                               //comparing the times
for(int c=0; c<17; c++) {                                                                   //looping through the circuits
if(roomTimer[c] != roomTimer[16]) {                                                 //comparing timers
mainOff = 1;                                                                                      //setting the switch off all command
lightStatus[16] = 0;                                                                            //switching off the master relay
}
else {
mainOff = 0;                                                                                     //if the timers match we set the
                                                                                                        //switch off all command to 0
break;                                                                                              //leaving the loop
}
}
}
if(mainOff == 0) {                                                                             //master off command is 0
Serial.println("switching off everything and reset all");                         //debug only
for(int i=0; i<17; i++) {                                                                     //looping through the circuits
lightStatus[i] = 0;                                                                              //resetting all light status
lightOutput[i] = 0;                                                                             //switching off all lights
priorityStatus[i] = 0;                                                                         //resetting all set priorities
roomTimer[i] = 0;                                                                            //resetting all room timers
}
digitalWrite(doorMonitor, LOW);                                                    //resetting the control LED
mainOff = 1;                                                                                   //resetting master off command
}
}
masterSwitchStateOld = switchState[18]; //setting the switchState to old
}

//>>>>>>>>>>>>>>>>>>>>end of main loop<<<<<<<<<<<<<<<<<<<<<<<

We got that far. But today we have a little more to do. With the code we have just put in, we only switch our master relay off. We did not switch it on anywhere yet. Thinking about a lot of possibilities how and from where to switch on the master relay I decided to switch it on with the first human presence being detected.
That means we have to add a couple of lines of code in every circuit we check. Do you remember where we started the processor coding?
We have to add the following two lines of code in to every room processing statement:
lightOutput[14] = 16348;
lightStatus[14] = 1;
Lets go back up and find:

//////////////////processing the input/////////////////////
if(switchState[1] == 0) {                                                           //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

//>>>>>>>>>Here we insert the 2 lines of code<<<<<<<<<<<<<<<<<<<
lightOutput[14] = 16348;                                                         //make sure the master relay
                                                                                               //stays on
lightStatus[14] = 1;                                                                  //setting the master relay status
//>>>>>>>>>End of insert<<<<<<<<<<<<<<<<<<<

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
}
}
}
else if(switchState[1] == 1 && lightStatus[0] == 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
}
else if(switchState[1] == 0 && lightStatus[0] == 0) {          //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
}

This two lines of code go in any light checking statements starting with switchState[0] to switchState[13]. Do not insert them in the 4 statements where we are checking the AC's, switchState[14] to switchState[17].
Now we told our system to switch off everything after a preprogrammed delay if the state of the read switch at the main door changes. We also have to put in a couple of lines of code in the checking statements to cancel the switch off command if a PIR was activated.
Again, we go up to the point where we start coding the processing part:

//////////////////processing the input/////////////////////

//>>>>>>>>>>>>>>>>>>we start adding the code here<<<<<<<<<<<<<<<<<<<

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
}
//>>>>>>>>>>>>>>end of insert on first room<<<<<<<<<<<<<<<<<<<<<<<<<<
if(switchState[1] == 0) {                                                                    //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] = 16348;                                                                  //make sure the master relay
                                                                                                        //stays on
lightStatus[14] = 1;                                                                           //setting the master relay 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
}
}
}
else if(switchState[1] == 1 && lightStatus[0] == 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
}
else if(switchState[1] == 0 && lightStatus[0] == 0) {                     //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
}

Now we go on to the next room and insert the same lines of code.

if(switchState[2] == 1 && lightStatus[16] == 1) {                          //checking if PIR in Room 2 was
                                                                                                     //activated (bed 2)
lightStatus[16] = 0;                                                                        //resetting master off
digitalWrite(doorMonitor, LOW);                                                  //resetting the door Monitor LED
}
PLEASE make sure you change the switchState[n] array to match the switch representing the next PIR we are addressing. In case of room 2 (bed 2) it would be if(switchState[2] == 1... and for room 3 it is if(switchState[4] == 1... and so on.
Did you find all the entry points? Good, if not, don't worry, at the end of this chapter I put again the complete updated code. But first we have to look at a couple of more things before we can start testing again. While typing the additions you might have noticed that we are working with a few more variables which need to be declared first. There for we go back right to the top of our code and find the point where it says “////////////////all the other variables////////////////”. Just below the array declaration “unsigned long delayTime[16] = {}” we add the following:

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 = 13;

after we have done this we move down a little further in the “setup loop” where it says:

“////////////defining pin modes//////////////”

and add:

pinMode(doorMonitor, OUTPUT);                                                      //setting the LED pin to output

The last thing for today, we need to go back to our breadboard and add the monitor LED. I like to use yellow or orange LED's for monitoring but I guess, that's up tu personal taste. OK, we connect a orange LED from the Arduino digital pin 13 to a 330 ohm resistor and to ground.



And as final, as promised again the complete updated code. Again, I have run it before posting it but some browsers or editors do format hyphens differently. If you just copy and paste and you end up with lots of “abandoned '/'” warnings please check the Serial.print statements the problem is mostly in here'”'.

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

///////////Timer and Sensitivity Settings to be changed to individual needs////////////////

long int dBed1 = 60000;                                                //delay time in milliseconds for bedroom 1
long int dBed2 = 60000;                                                //delay time in milliseconds for bedroom 2
long int dBed3 = 60000;                                                //delay time in milliseconds for bedroom 3
long int dLiving = 120000;                                             //delay time in milliseconds for living area
long int dBath1 = 180000;                                             //delay time in milliseconds for bathroom 1
long int dBath2 = 180000;                                             //delay time in milliseconds for bathroom 2
long int dBath3 = 180000;                                             //delay time in milliseconds for bathroom 3
long int dBath4 = 180000;                                             //delay time in milliseconds for bathroom 4
long int dKitchen = 120000;                                          //delay time in milliseconds for kitchen
long int dCorridor = 60000;                                           //delay time in milliseconds for corridor
long int dAC1 = 120000;                                               //delay time in milliseconds for AC 1 (bed1)
long int dAC2 = 120000;                                              //delay time in milliseconds for AC 2 (bed2)
long int dAC3 = 120000;                                              //delay time in milliseconds for AC 3 (bed3)
long int dAC4 = 120000;                                              //delay time in milliseconds for AC 4 (living)
long int dMaster = 240000;                                          //delay time in milliseconds for Master Off

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

//////////defining Arduino Pins/////////////////
const int latchPin = 2;                                                       //latch pin input connected to
                                                                                        //Arduino digital pin 2
const int clockPin = 3;                                                      //clock pin input connected to
                                                                                       //Arduino digital pin 3
const int dataPin = 4;                                                       //data pin input connected to
                                                                                       //Arduino digital pin 4
const int latchPinOut = 5;                                                 //latch pin output shift register
                                                                                       //74HC595 connected to Arduino
                                                                                       //digital pin 5
const int clockPinOut = 6;                                               //clock pin output shift register
                                                                                      //74HC595 connected to Arduino
                                                                                      //digital pin 6
const int dataPinOut = 7;                                                //data pin output shift register
                                                                                      //74HC595 connected to Arduino
                                                                                      //digital pin 7

//////////Variables to hold the data for each shift register//////////

byte switchVar1 = 0;                                                       //data for input shift register 1
byte switchVar2 = 0;                                                       //data for input shift register 2
byte switchVar3 = 0;                                                       //data for input shift register 3

////////////////all the other variables///////////////////////////
unsigned long delayTime[16] = {dBed1, dBed2, dBed3, dLiving, dBath1, dBath2, dBath3,
                                                dBath4, dKitchen, dCorridor, dAC1, dAC2, dAC3, dAC4,
                                               dMaster, 0};
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 = 13;                                                     //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


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

Serial.begin(9600);

//////////////////defining pin modes////////////////////
pinMode(doorMonitor, OUTPUT);                              //setting the LED pin to output

//input shift register
pinMode(latchPin, OUTPUT);                                     //setting the latch pin to output
pinMode(clockPin, OUTPUT);                                    //setting the clock pin to output
pinMode(dataPin, INPUT);                                         //setting the data pin to input

//output shift register
pinMode(latchPinOut, OUTPUT);
pinMode(clockPinOut, OUTPUT);
pinMode(dataPinOut, OUTPUT);
}

void loop() {
//////////////////////////////////////getting the input//////////////////////////////////////////////////
//pulse the latch pin, set to high to collect serial data
digitalWrite(latchPin, HIGH);
//give it chance to collect the data
delayMicroseconds(20);
//set latch pin to low to transmit data serially
digitalWrite(latchPin, LOW);

//while in serial mode, collect data into a byte
switchVar1 = shiftIn(dataPin, clockPin);
switchVar2 = shiftIn(dataPin, clockPin);
switchVar3 = shiftIn(dataPin, clockPin);
/////////////do something with the collected Data/////////////////////
//checks for debugging
Serial.println();                                                    //debug only
Serial.print("Switch variable 1: ");                        //debug only
Serial.println(switchVar1, BIN);                          //debug only
Serial.println("-------------------");                    //debug only
Serial.println();                                                   //debug only
Serial.print("Switch variable 2: ");                       //debug only
Serial.println(switchVar2, BIN);                         //debug only
Serial.println("-------------------");                   //debug only
Serial.println();                                                  //debug only
Serial.print("Switch variable 3: ");                      //debug only
Serial.println(switchVar3, BIN);                        //debug only
Serial.println("-------------------");                  //debug only

////////////loop through the 8 input pins to check their status////////////
for(int n=0; n<=7; n++){

//shift register 1
if(switchVar1 & (1)) { //checking S1
Serial.println("Switch 1 was activated."); //debug only
switchState[0] = 1;
}
else {
switchState[0] = 0;
}

if(switchVar1 & (1 << 1)) {                                                      //checking S2
Serial.println("Switch 2 was activated.");                                   //debug only
switchState[1] = 1;
}
else {
switchState[1] = 0;
}
if(switchVar1 & (1 << 2)) {                                                      //checking S3
Serial.println("Switch 3 was activated.");                                   //debug only
switchState[2] = 1;
}
else {
switchState[2] = 0;
}

if(switchVar1 & (1 << 3)) {                                                     //checking S4
Serial.println("Switch 4 was activated.");                                  //debug only
switchState[3] = 1;
}
else {
switchState[3] = 0;
}

if(switchVar1 & (1 << 4)) {                                                     //checking S8
Serial.println("Switch 8 was activated.");                                  //debug only
switchState[7] = 1;
}
else {
switchState[7] = 0;
}

if(switchVar1 & (1 << 5)) {                                                    //checking S7
Serial.println("Switch 7 was activated.");                                 //debug only
switchState[6] = 1;
}
else {
switchState[6] = 0;
}

if(switchVar1 & (1 << 6)) {                                                      //checking S6
Serial.println("Switch 6 was activated.");                                   //debug only
switchState[5] = 1;
}
else {
switchState[5] = 0;
}

if(switchVar1 & (1 << 7)) {                                                      //checking S5
Serial.println("Switch 5 was activated.");                                   //debug only
switchState[4] = 1;
}
else {
switchState[4] = 0;
}

//shift register 2

if(switchVar2 & (1)) {                                                               //checking S9
Serial.println("Switch 9 was activated.");                                    //debug only
switchState[8] = 1;
}
else {
switchState[8] = 0;
}

if(switchVar2 & (1 << 1)) {                                                      //checking S10
Serial.println("Switch 10 was activated.");                                 //debug only
switchState[9] = 1;
}
else {
switchState[9] = 0;
}

if(switchVar2 & (1 << 2)) {                                                     //checking S11
Serial.println("Switch 11 was activated.");                                //debug only
switchState[10] = 1;
}
else {
switchState[10] = 0;
}

if(switchVar2 & (1 << 3)) {                                                      //checking S12
Serial.println("Switch 12 was activated.");                                 //debug only
switchState[11] = 1;
}
else {
switchState[11] = 0;
}

if(switchVar2 & (1 << 4)) {                                                      //checking S16
Serial.println("Switch 16 was activated.");                                 //debug only
switchState[15] = 1;
}
else {
switchState[15] = 0;
}

if(switchVar2 & (1 << 5)) {                                                     //checking S15
Serial.println("Switch 15 was activated.");                                //debug only
switchState[14] = 1;
}
else {
switchState[14] = 0;
}

if(switchVar2 & (1 << 6)) {                                                    //checking S14
Serial.println("Switch 14 was activated.");                               //debug only
switchState[13] = 1;
}
else {
switchState[13] = 0;
}

if(switchVar2 & (1 << 7)) {                                                    //checking S13
Serial.println("Switch 13 was activated.");                               //debug only
switchState[12] = 1;
}
else {
switchState[12] = 0;
}

//shift register 3

if(switchVar3 & (1)) {                                                              //checking S17
Serial.println("Switch 17 was activated.");                                //debug only
switchState[16] = 1;
}
else {
switchState[16] = 0;
}

if(switchVar3 & (1 << 1)) {                                                     //checking S18
Serial.println("Switch 18 was activated.");                                //debug only
switchState[17] = 1;
}
else {
switchState[17] = 0;
}
if(switchVar3 & (1 << 2)) {                                                   //checking S19
Serial.println("Switch 19 was activated.");                              //debug only
switchState[18] = 1;
}
else {
switchState[18] = 0;
}
}
//////////////////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) {                                                      //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] = 16348;                                                   //make sure the master relay
                                                                                         //stays on
lightStatus[14] = 1;                                                            //setting the master relay 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
}
}
}
else if(switchState[1] == 1 && lightStatus[0] == 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
}
else if(switchState[1] == 0 && lightStatus[0] == 0) {      //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
}
if(switchState[2] == 1 && lightStatus[16] == 1) {          //checking if PIR in Room 2 was
                                                                                     //activated (bed 2)
lightStatus[16] = 0;                                                        //resetting master off
digitalWrite(doorMonitor, LOW);                                  //resetting the door Monitor LED
}
if(switchState[3] == 0){                                                //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] = 16348;                                              //make sure the master relay
                                                                                    //stays on
lightStatus[14] = 1;                                                       //setting the master relay 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) { //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) { //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
}
if(switchState[4] == 1 && lightStatus[16] == 1) {       //checking if PIR in Room 3 was
                                                                                   //activated (bed 3)
lightStatus[16] = 0;                                                      //resetting master off
digitalWrite(doorMonitor, LOW);                                //resetting the door Monitor LED
}
if(switchState[5] == 0){                                               //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] = 16348;                                             //make sure the master relay
                                                                                   //stays on
lightStatus[14] = 1;                                                      //setting the master relay 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) {                     //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) {                     //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
}
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){                                                                 //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] = 16348;                                                               //make sure the master relay
                                                                                                     //stays on
lightStatus[14] = 1;                                                                        //setting the master relay 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) {                       //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) {                       //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
}
if(switchState[8] == 1 && lightStatus[16] == 1) {                           //checking if PIR in Room 5 was
                                                                                                      //activated (bath 1)
lightStatus[16] = 0;                                                                         //resetting master off
digitalWrite(doorMonitor, LOW);                                                    //resetting the door Monitor LED
}
if(switchState[8] == 1) {                                                                  //checking S9 PIR of bathroom 1
                                                                                                       //(room 5)
Serial.println("We switch on the lights");                                            //Debug only
lightOutput[4] = 16;                                                                         //switching on the lights
lightStatus[4] = 1;                                                                            //setting the light status
lightOutput[14] = 16348;                                                                 //make sure the master relay
                                                                                                       //stays on
lightStatus[14] = 1;                                                                          //setting the master relay status
roomTimer[4] = millis();                                                                   //setting the room timer
}
else if(switchState[8] == 0 && lightStatus[4] == 1) {                       //if no PIR was activated and
                                                                                                       //the lights are on
Serial.println("We are checking the timer");                                       //Debug only
currentTime = millis();                                                                      //setting time reference
endTime = currentTime - roomTimer[4];                                          //calculating the inactive time
if(endTime >= delayTime[4]) {                                                        //comparing inactive time with
                                                                                                      //delay time
Serial.println("We are switching off the lights");                                //debug only
lightOutput[4] = 0;                                                                          //switching off the lights
lightStatus[4] = 0;                                                                           //resetting the light status
roomTimer[4] = 0;                                                                          //resetting the room timer
}
}
if(switchState[9] == 1 && lightStatus[16] == 1) {                           //checking if PIR in Room 6 was
                                                                                                       //activated (bath 2)
lightStatus[16] = 0;                                                                          //resetting master off
digitalWrite(doorMonitor, LOW);                                                    //resetting the door Monitor LED
}
if(switchState[9] == 1) {                                                                  //checking S10 PIR of bathroom 2
                                                                                                       //(room 6)
Serial.println("We switch on the lights");                                            //Debug only
lightOutput[5] = 32;                                                                         //switching on the lights
lightStatus[5] = 1;                                                                            //setting the light status
lightOutput[14] = 16348;                                                                 //make sure the master relay
                                                                                                       //stays on
lightStatus[14] = 1;                                                                          //setting the master relay status
roomTimer[5] = millis();                                                                   //setting the room timer
}
else if(switchState[9] == 0 && lightStatus[5] == 1) {                       //if no PIR was activated and
                                                                                                       //the lights are on
Serial.println("We are checking the timer");                                       //Debug only
currentTime = millis();                                                                      //setting time reference
endTime = currentTime - roomTimer[5];                                          //calculating the inactive time
if(endTime >= delayTime[5]) {                                                        //comparing inactive time with
                                                                                                      //delay time
Serial.println("We are switching off the lights");                                 //debug only
lightOutput[5] = 0;                                                                          //switching off the lights
lightStatus[5] = 0;                                                                           //resetting the light status
roomTimer[5] = 0;                                                                          //resetting the room timer
}
}
if(switchState[10] == 1 && lightStatus[16] == 1) {                        //checking if PIR in Room 7 was
                                                                                                     //activated (bath 3)
lightStatus[16] = 0;                                                                        //resetting master off
digitalWrite(doorMonitor, LOW);                                                  //resetting the door Monitor LED
}
if(switchState[10] == 1) {                                                              //checking S11 PIR of bathroom 3
                                                                                                     //(room 7)
Serial.println("We switch on the lights");                                          //Debug only
lightOutput[6] = 64;                                                                       //switching on the lights
lightStatus[6] = 1;                                                                          //setting the light status
lightOutput[14] = 16348;                                                               //make sure the master relay
                                                                                                    //stays on
lightStatus[14] = 1;                                                                       //setting the master relay status
roomTimer[6] = millis();                                                                //setting the room timer
}
else if(switchState[10] == 0 && lightStatus[6] == 1) {                 //if no PIR was activated and
                                                                                                   //the lights are on
Serial.println("We are checking the timer");                                   //Debug only
currentTime = millis();                                                                  //setting time reference
endTime = currentTime - roomTimer[6];                                      //calculating the inactive time
if(endTime >= delayTime[6]) {                                                    //comparing inactive time with
                                                                                                  //delay time
Serial.println("We are switching off the lights");                            //debug only
lightOutput[6] = 0;                                                                     //switching off the lights
lightStatus[6] = 0;                                                                      //resetting the light status
roomTimer[6] = 0;                                                                    //resetting the room timer
}
}

if(switchState[11] == 1 && lightStatus[16] == 1) {                     //checking if PIR in bath 4 was
                                                                                                  //activated (room 8)
lightStatus[16] = 0;                                                                     //resetting master off
digitalWrite(doorMonitor, LOW);                                               //resetting the door Monitor LED
}
if(switchState[11] == 1) {                                                           //checking S12 PIR of bathroom 4
                                                                                                  //(room 8)
Serial.println("We switch on the lights");                                      //Debug only
lightOutput[7] = 128;                                                                 //switching on the lights
lightStatus[7] = 1;                                                                       //setting the light status
lightOutput[14] = 16348;                                                            //make sure the master relay
                                                                                                  //stays on
lightStatus[14] = 1;                                                                     //setting the master relay status
roomTimer[7] = millis();                                                              //setting the room timer
}
else if(switchState[11] == 0 && lightStatus[7] == 1) {                //if no PIR was activated and
                                                                                                  //the lights are on
Serial.println("We are checking the timer");                                  //Debug only
currentTime = millis();                                                                 //setting time reference
endTime = currentTime - roomTimer[7];                                     //calculating the inactive time
if(endTime >= delayTime[7]) {                                                   //comparing inactive time with
                                                                                                 //delay time
Serial.println("We are switching off the lights");                            //debug only
lightOutput[7] = 0;                                                                     //switching off the lights
lightStatus[7] = 0;                                                                      //resetting the light status
roomTimer[7] = 0;                                                                    //resetting the room timer
}
}

if(switchState[12] == 1 && lightStatus[16] == 1) {                    //checking if PIR in kitchen was
                                                                                                 //activated (room 9)
lightStatus[16] = 0;                                                                    //resetting master off
digitalWrite(doorMonitor, LOW);                                              //resetting the door Monitor LED
}
if(switchState[12] == 1) {                                                          //checking S13 PIR of kitchen
                                                                                                 //(room 9)
Serial.println("We switch on the lights");                                      //Debug only
lightOutput[8] = 256;                                                                 //switching on the lights
lightStatus[8] = 1;                                                                      //setting the light status
lightOutput[14] = 16348;                                                          //make sure the master relay
                                                                                                //stays on
lightStatus[14] = 1;                                                                    //setting the master relay status
roomTimer[8] = millis();                                                             //setting the room timer
}
else if(switchState[12] == 0 && lightStatus[8] == 1) {               //if no PIR was activated and
                                                                                                //the lights are on
Serial.println("We are checking the timer");                                //Debug only
currentTime = millis();                                                               //setting time reference
endTime = currentTime - roomTimer[8];                                   //calculating the inactive time
if(endTime >= delayTime[8]) {                                                 //comparing inactive time with
                                                                                               //delay time
Serial.println("We are switching off the lights");                          //debug only
lightOutput[8] = 0;                                                                   //switching off the lights
lightStatus[8] = 0;                                                                    //resetting the light status
roomTimer[8] = 0;                                                                   //resetting the room timer
}
}
if(switchState[13] == 1 && lightStatus[16] == 1) {                  //checking if PIR in corridor was
                                                                                               //activated (room 10)
lightStatus[16] = 0;                                                                  //resetting master off
digitalWrite(doorMonitor, LOW);                                            //resetting the door Monitor LED
}
if(switchState[13] == 1) {                                                        //checking S14 PIR of Corridor
                                                                                               //(room 10)
Serial.println("We switch on the lights");                                    //Debug only
lightOutput[9] = 512;                                                               //switching on the lights
lightStatus[9] = 1;                                                                    //setting the light status
lightOutput[14] = 16348;                                                         //make sure the master relay
                                                                                               //stays on
lightStatus[14] = 1;                                                                  //setting the master relay status
roomTimer[9] = millis();                                                           //setting the room timer
}
else if(switchState[13] == 0 && lightStatus[9] == 1) {            //if no PIR was activated and
//the lights are on
Serial.println("We are checking the timer");                               //Debug only
currentTime = millis();                                                              //setting time reference
endTime = currentTime - roomTimer[9];                                  //calculating the inactive time
if(endTime >= delayTime[9]) {                                                //comparing inactive time with
                                                                                              //delay time
Serial.println("We are switching off the lights");                        //debug only
lightOutput[9] = 0;                                                                 //switching off the lights
lightStatus[9] = 0;                                                                  //resetting the light status
roomTimer[9] = 0;                                                                 //resetting the room timer
}
}

if(switchState[14] == 1 && lightStatus[14] == 1){                 //Checking if readswitches are activated
                                                                                              //and the master relay is on AC room 1  
                                                                                              //(bed1)
lightOutput[10] = 1024;                                                          //providing the ability to
                                                                                               //switch on the AC
lightStatus[10] = 1;                                                                  //setting the light (AC) status
roomTimer[10] = millis();                                                         //setting the timer
}
else if(switchState[14] == 0 && lightStatus[14] == 1){            //if a door is opened and the master
                                                                                                //relay is on
currentTime = millis();                                                               //setting time reference
endTime = currentTime - roomTimer[10];                                  //calculating the inactive time
if(endTime >= delayTime[10]){                                                 //comparing inactive time with
                                                                                                //delay time
lightOutput[10] = 0;                                                                  //cancelling ability to switch on the
                                                                                                //AC
lightStatus[10] = 0;                                                                   //resetting the light (AC) status
roomTimer[10] = 0;                                                                 //resetting the timer
}
}
if(switchState[15] == 1 && lightStatus[14] == 1){                   //Checking if readswitches are activated
                                                                                                //and the master relay is on AC room 2 
                                                                                                //(bed2)
lightOutput[11] = 2048;                                                            //providing the ability to
                                                                                                //switch on the AC
lightStatus[11] = 1;                                                                   //setting the light (AC) status
roomTimer[11] = millis();                                                          //setting the timer
}
else if(switchState[15] == 0 && lightStatus[14] == 1){             //if a door is opened and the master
                                                                                                //relay is on
currentTime = millis();                                                               //setting time reference
endTime = currentTime - roomTimer[11];                                  //calculating the inactive time
if(endTime >= delayTime[11]){                                                 //comparing inactive time with
                                                                                                //delay time
lightOutput[11] = 0;                                                                  //cancelling ability to switch on the
                                                                                                //AC
lightStatus[11] = 0;                                                                   //resetting the light (AC) status
roomTimer[11] = 0;                                                                  //resetting the timer
}
}

if(switchState[16] == 1 && lightStatus[14] == 1){                    //Checking if readswitches are activated and
                                                                                                 //the master relay is on AC room 3 (bed3)
lightOutput[12] = 4096;                                                             //providing the ability to
                                                                                                  //switch on the AC
lightStatus[12] = 1;                                                                     //setting the light (AC) status
roomTimer[12] = millis();                                                            //setting the timer
}
else if(switchState[16] == 0 && lightStatus[14] == 1){               //if a door is opened and the master
                                                                                                  //relay is on
currentTime = millis();                                                                  //setting time reference
endTime = currentTime - roomTimer[12];                                    //calculating the inactive time
if(endTime >= delayTime[12]){                                                   //comparing inactive time with
                                                                                                  //delay time
lightOutput[12] = 0;                                                                    //cancelling ability to switch on the
                                                                                                  //AC
lightStatus[12] = 0;                                                                     //resetting the light (AC) status
roomTimer[12] = 0;                                                                   //resetting the timer
}
}

if(switchState[17] == 1 && lightStatus[14] == 1){                       //Checking if readswitches are activated
                                                                                                   //and the master relay is on AC room 4
                                                                                                   // living
lightOutput[13] = 8192;                                                               //providing the ability to
                                                                                                   //switch on the AC
lightStatus[13] = 1;                                                                      //setting the light (AC) status
roomTimer[13] = millis();                                                             //setting the timer
}
else if(switchState[17] == 0 && lightStatus[14] == 1){                //if a door is opened and the master
                                                                                                    //relay is on
currentTime = millis();                                                                   //setting time reference
endTime = currentTime - roomTimer[13];                                     //calculating the inactive time
if(endTime >= delayTime[13]){                                                    //comparing inactive time with
                                                                                                   //delay time
lightOutput[13] = 0;                                                                     //cancelling ability to switch on the
                                                                                                   //AC
lightStatus[13] = 0;                                                                      //resetting the light (AC) status
roomTimer[13] = 0;                                                                    //resetting the timer
}
}
/////////////Door switch control ////////////////////
if(switchState[18] != masterSwitchStateOld) {                             //door switch check if the switch state
                                                                                                   //has changed
Serial.println("Door switch was activated");                                  //debug only
currentTime = millis();                                                                  //setting time reference
lightStatus[16] = 1;                                                                      //setting light status
digitalWrite(doorMonitor, HIGH);                                               //setting the control LED
for(int i=0; i<17; i++){                                                                //looping through the timers
roomTimer[i] = currentTime;                                                       //setting the timers
}
}
else if(switchState[18] == masterSwitchStateOld && lightStatus[16] == 1){           //if the switch state
                                                                                                 //has not changed and the lights are on
Serial.println("Checking off status");                                            //debug only
currentTime = millis();                                                                 //setting the time reference
offTime = roomTimer[16] + delayTime[14];                                //setting the allowed delay time
if(currentTime >= offTime) {                                                       //comparing the times
for(int c=0; c<17; c++) {                                                           //looping through the circuits
if(roomTimer[c] != roomTimer[16]) {                                         //comparing timers
mainOff = 1;                                                                              //setting the switch off all command
lightStatus[16] = 0;                                                                    //switching off the master relay
}
else {
mainOff = 0;                                                                               //if the timers match we set the
                                                                                                  //switch off all command to 0
break;                                                                                         //leaving the loop
}
}
}
if(mainOff == 0) {                                                                      //master off command is 0
Serial.println("switching off everything and reset all");                   //debug only
for(int i=0; i<12; i++) {                                                               //looping through the circuits
lightStatus[i] = 0;                                                                        //resetting all light status
lightOutput[i] = 0;                                                                      //switching off all lights
priorityStatus[i] = 0;                                                                  //resetting all set priorities
roomTimer[i] = 0;                                                                      //resetting all room timers
}
digitalWrite(doorMonitor, LOW);                                              //resetting the control LED
mainOff = 1;                                                                             //resetting master off command
}
}
masterSwitchStateOld = switchState[18];                                 //setting the switchState to old
}




///////////////////shiftIn function to get the input data////////////////////////////////////

byte shiftIn(int myDataPin, int myClockPin) {
//////////////////declaring some variables///////////////
int i;                                                                                  //counter
int temp = 0;                                                                    //var to hold the raw input data
int pinState;                                                                     //var holding the state of the pin
byte myDataIn = 0;                                                         //var holding the complete incoming data

/////////////////setting the pin modes///////////////////

pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, INPUT);

///////////////////// loop through the single input pins////////////
for(i=7; i>0; i--) {
digitalWrite(myClockPin, LOW);                                    //setting the clock pin low to trigger the output
delayMicroseconds(2);                                                   //give it chance to send the data
temp = digitalRead(myDataPin);                                     //check if the pin was set (switch was pressed)
if(temp) {                                                                      //if the pin was set
pinState = 1;                                                                 //we set the pin state to 1 accordingly
myDataIn = myDataIn | (1 << i);                                    //setting the state in our binary output
}
else {
pinState = 0;                                                                 //if the pin was not set, the pin state stays 0
}
Serial.print("PinState: ");                                                //debug only
Serial.print(pinState);                                                    //debug only
Serial.print(" ");                                                             //debug only
Serial.println(myDataIn, BIN);                                       //debug only

digitalWrite(myClockPin, HIGH);                                  //setting the clock pin high
}
Serial.println(); //debug only
Serial.println(myDataIn, BIN);                                       //debug only

return myDataIn;                                                           //after looping through all the input pins returning the
                                                                                    //collected input data to the main program
}

No comments:

Post a Comment