Translate

Tuesday 11 March 2014

Room Management System - Coding the processing - part 2


Did you try it? Did the LED come on after pressing your button number 1 and did the LED go off after just over 1 minute?
Did the LED stay on while you kept activating button 1?
Did you also try to press button number 2 and the LED went off no matter what you did with button 1?
And finally did the LED come back on after you pressed button 2 again?
If you tested all the above and you can answer all of this with yes, we are ready to type the code for the next rooms having a priority button.

We go back down to the end of our main loop and start where we finished in the last chapter:

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
}

//>>>>>>>>>>>>>>>>>>WE carry on from here<<<<<<<<<<<<<<<<<<

if(switchState[3] == 0){ //checking if S2 priority off was
//set bed 2
if(switchState[2] == 1 && priorityStatus[1] == 0){ //check if the PIR in bed 2 was
//activated
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
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[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
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 2
}
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[7] == 0){ //checking if S8 priority off was
//set living
if(switchState[6] == 1 && priorityStatus[3] == 0){ //check if the PIR in bed 3 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
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
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
//bed 2
}
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 ion the lights
lightStatus[3] = 1; //setting the light status
roomTimer[3] = millis(); //setting the room timer
priorityStatus[3] = 0; //resetting the priority status
}

}

////>>>>>>>>>>>>>>>End of main loop<<<<<<<<<<<<<<<<<<<<<

The to days addition let us control the lights in the 3 bedrooms and the living area. The code is the same as the one we put in in the last chapter. The only thing changing is in which place in our arrays holding the variables we are pointing.
I know, looking at the numbers might be a little confusing in the beginning because we start counting the rooms and switches with 1. The first place in an array is 0. The light status for room 1 (bed1) is held in the array lightStatus[0] and the light status for room 2 (bed2) is held in the array lightStatus[1] and so on.
A little more attention we have to pay to our switches since we have two switches in some rooms.
The array switchState[0] is assigned to S1 representing the PIR's from room 1 lightStatus[0].
The array switchState[1] is assigned to S2 representing the priority switch from room 1 lightStatus[0], priorityStatus[0].
The array switchState[2] is assigned to S3 representing the PIR's from room 2 lightStatus[1].
The array switchState[3] is assigned to S4 representing the priority switch from room 2 lightStatus[1], priorityStatus[1].
And so on......

In the next chapter we will take care of the room without a priority switch.

No comments:

Post a Comment