Translate

Saturday 15 March 2014

Room Management System – Adding a photocell and a maintenance switch


Adding a photocell
We managed to switch on all the lights in the apartment and let them switch off again after we have left but we don't want the lights to switch on while the sun is shining right into the room. There for we will be adding a photocell preferably to be mounted in the room with least light conditions. However, if the light conditions vary to much or the rooms are on different building sites we might consider of putting in 2 photocells.
Lets start with one photocell:

First we go back to our breadboard and add a photocell. One end of the photocell connects to the 5V bus and the other end to the analogue pin 0 of our Arduino board. The pin of the photocell connecting to the Arduino analogue input, connects also to a 10 K potentiometer. The centre pin of the potentiometer connects to GND. The potentiometer we are using to adjust the sensitivity of our photocell.



OK so far. Now we need to put a few line of code in our sketch.

This time we start right at the top of our sketch and add a few variables. We start at

/////////Timer and sensitivity settings//////////////////////////////////////
and add

unsigned int photoCellCutOff = 280;              //var holding the value where the photocell cuts off

from here we go further down to where it says

//////////////////////////////////all the other variables////////////////////////////////
and add

int lightSensor = A0;                                                 //defining the input for the photocell
int sensorValue = 0;                                                  //holding the indicated sensor value of the photocell


and now we go further down to the point where it says

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

right above that line we add

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

//Serial.print("Sensor value: ");                                    //debug only
//Serial.println(sensorValue);                                       //debug only

and finally we need to implement the cut off in every room check statement where we have sufficient daylight to prevent the main lights from coming on during daylight.
There for we go below the line where it says

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

if(switchState[0] == 1 && lightStatus[14] == 1) {                             //checking if PIR in Room 1 was
                                                                                                        //activated (bed 1)
lightStatus[14] = 0;                                                                           //resetting master off
digitalWrite(doorMonitor, LOW);                                                     //resetting the door Monitor LED
}
//>>>>>>>>we add below here in the if statement <<<<<<<<<<<<<<<<<<<<<<<<<<

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

“ && sensorValue <= photoCellCutOff” we need to add in the same if statement of every room where we have big windows and we like to prevent the lights from coming on during daylight. In my case it will be all the bedrooms and the living area.
If you have to work with 2 photocells, you got to do the same circuit again and connect it to the second analogue input of the Arduino board and rename the variables to

unsigned int photoCellCutOff1 = 200;                  //var holding the value where the second
                                                                           //photocell cuts off
int lightSensor1 = A1;                                          //defining the input for the second photocell
int sensorValue1 = 0;                                           //holding the indicated sensor value of the
                                                                           //second photocell

and insert them as described for the first photocell. You may have noticed, that by now I am typing the Serial.print statements already commented out. To find the right cut off for the photocell please uncomment the read out statements and check in the serial monitor the reading to get a bit of an idea how to set the cut off point. You still might have to recheck after installing the ready system since we are measuring resistance and long, thin cables might increase the resistance so you have to adjust the cut off a little. You still can change the sensitivity with the in line potentiometer and if the range is to small you can also try to increase the potentiometer value from 10 K up to a 100 K.


Adding a maintenance switch
Last, I also like to implement a maintenance switch. This switch overrides all the priorities, photocells, PIR's and switches on everything to check light bulbs and AC functions even the doors are open etc.

Back to the breadboard and there, at the end of the row with the buttons, we add a toggle switch, connect the centre pin to the 5 V bus and and one switch side to the fourth input pin of the third input shift register (CD4024B).



To add the code for the maintenance switch we start again right at the top of our sketch. Where we declare the variables, we find again the part where it says

/////////////////////all the other variables////////////////////////////////////
and add

int maintenancePin = 0;                             //defining the var for the maintenance switch

from here we go down to the end of our input code just above the line

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

we find the end of the input code and add


if(switchVar3 & (1 << 2)) {                                          //checking S19
//Serial.println("Switch 19 was activated.");                   //debug only
switchState[18] = 1;
}
else {
switchState[18] = 0;
}

//>>>>>>>>>>>>>>>>>we start adding here<<<<<<<<<<<<<<<<<<<<<<

if(switchVar3 & (1 << 3)) {                                         //checking S20
//Serial.println("Switch 20 was activated.");                  //debug only
maintenancePin = 1;
}
else {
maintenancePin = 0;
}

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

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


Found the entry point again? OK, now we go down to where we start the output processing and find the line

///////////////////////output////////////////////////////

for(int i=0; i<17; i++) {
/*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];
}

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

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
}

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

If the maintenance switch is activated, we want everything on. We loop through all our circuits, set the timers and the light status and most important the output. The number 32767 we get by adding up all the numbers which go in the lightOutput[n] array if everything switches on. Remember all the statements:
if nothing else is set to stop the lights from coming on
lightOutput[0] = 1;
lightOutput[1] = 2;
lightOutput[2] = 4;

and so on

This are the numbers we have to add up.

For all the ones building and coding with me and are wondering why the master relay does not come on and something else instead, I have to apologise because I did a small mistake in adding binary numbers.
Please check all the statements where we are making sure that the master relay stays on:

lightOutput[14] = 16348; <<<<<<<<<<That's wrong!!!!!!!!!!!

Please change to

lightOutput[14] = 16384;

Again, please accept my apologies for making you work a little more.

In the next chapter I will have a look into how to connect the unit in the praxis and again the complete revised code with everything in it, what we have done so far.

No comments:

Post a Comment