As promised the updated and debugged code for the room management system.
/////////////////////Declaring the Variables/////////////////
/////////////////////Declaring the Variables/////////////////
///////////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
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 input 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};
int maintenancePin = 0;
//defining the var for the maintenance switch
int lightSensor = A0;
//defining the input for the photocell
int sensorValue = 0;
//holding the indicated sensor value
//of the photocell
//of the photocell
unsigned long outputL = 0; //variable to hold the output
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 CD4021B
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 74HC595
pinMode(latchPinOut, OUTPUT);
pinMode(clockPinOut, OUTPUT);
pinMode(dataPinOut, OUTPUT);
//output shift register 74HC595
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;
}
}
if(switchVar3 & (1 <<
3)) { //checking S20
//Serial.println("Switch
20 was activated."); //debug only
maintenancePin = 1;
}
else {
maintenancePin = 0;
}
////////////////getting the photocell readings//////////////////////
sensorValue = analogRead(lightSensor);
//reading the photocell
//Serial.print("Sensor value:
"); //debug only
//Serial.println(sensorValue); //debug only
//////////////////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 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 &&
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 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 &&
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 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 &&
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 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] = 16384;
//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] = 16384;
//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] = 16384;
//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] = 16384; //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] = 16384;
//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] = 16384;
//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<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
//////////////////////////Output
Stage//////////////////////////////////////////////////////////
for(int i=0; i<17; i++) {
//loop through the light output array
//Serial.print("Light Output
"); //debug only
//Serial.print(i);
//debug only
//Serial.print(": ");
//debug only
//Serial.println(lightOutput[i]);
//debug only
//Serial.print("Light status:
"); //debug only
//Serial.println(lightStatus[i]);
//debug only
//Serial.print("Room Timer:
"); //debug only
//Serial.println(roomTimer[i]);
//debug only
outputL += lightOutput[i];
//adding up the numbers
}
if(maintenancePin == 1) {
//if maintenance switch is active
for(int i=1; i>17; i++){
//loop through all circuits
lightStatus[i] = 1;
//setting the light status of everything
roomTimer[i] = millis();
//setting all the room timers
}
outputL = 32767;
//setting the output
//binary 0111111111111111
}
//Serial.print("Output value:
"); //debug only
//Serial.print(outputL); //debug only
//Serial.print(" "); //debug only
//Serial.println(outputL, BIN); //debug only
digitalWrite(latchPinOut, LOW);
//setting the latch pin to low to
//be able to send the data
shiftOut(dataPinOut, clockPinOut,
MSBFIRST, (outputL >> 8)); //sending the date for the
//second shift register
shiftOut(dataPinOut, clockPinOut,
MSBFIRST, outputL); //sending the data for the
//first shift register
digitalWrite(latchPinOut, HIGH);
//setting the latch pin back to
//high to finish the data transmission
outputL = 0;
//setting the var holding the output
//number back to 0
delay(sensitivity);
//delay to adjust how responsive the
//system will react
///////////////////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