Translate

Saturday 3 May 2014

Room Management System – Another Bug Fix and displaying the Output


While checking everything again after all this changes, I found another little bug. In normal mode the outside lights wouldn't switch on any more at the set time and that's simple because the statement where we check the photocell is inside the “holiday switch” condition. This check has to come out before we separate the operation mode.

Let's jump down into the main loop where it says “ //////////////Holiday lighting/////////////////////////” and find the following bit of code:


 if(switchState[20] == 1) { //check if the holiday switch

//is activated

lightOutput[14] = 0; //make sure the master relay is off

//Serial.print("Current date: ");

//Serial.print(days[currentDay - 1]);

//Serial.print(", ");

//Serial.print(currentDoM);

//Serial.print("/");

//Serial.print(currentMonth);

//Serial.print("/");

//Serial.println(currentYear);

//Serial.print("Current Time: ");

//Serial.print(currentHour);

//Serial.print(" : ");

//Serial.println(currentMinute);

//Serial.print("Photo cell switch: ");

//Serial.println(photocellSwitch);

//>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//The part below the mark until the next mark has to be cut and

//moved outside the holiday mode.

if(photocellSwitch == 1 && currentHour >= 17 && currentHour <= 23) {

Serial.println("Entered first loop!");

for(int i=0; i<9; i++){

lightLevel[i] = 1;

}

lightLevel[15] = 1;

}

else if(photocellSwitch == 1 && currentHour >= 0 && currentHour <= 8){

//Serial.println("Entered second loop!");

for(int i=0; i<9; i++){

lightLevel[i] = 1;

}

lightLevel[15] = 1;

}

else if(photocellSwitch == 0 && currentHour >= 5 && currentHour <= 8){

//Serial.println("Entered third loop!");

for(int c=0; c<17; c++){

lightLevel[c] = 0;

}

}

//>>>>>>>>>>>>>>>end of cut<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

//Serial.print("Light level room 1 after: ");

We paste the bit of code we just cut right above the line where it says “ //////////////Holiday lighting/////////////////////////”

like that:


 lcd.print(currentHour); //print current time (hour)

lcd.print(":"); //print separator

if(currentMinute < 10) lcd.print("0"); //if the minute is less than

//10 print 0 to keep 2 digits

lcd.print(currentMinute); //print current time (minutes)

//Serial.print("Light level room 1: ");

//Serial.println(lightLevel[0]);

//Serial.print("Light level room 4: ");

//Serial.println(lightLevel[3]);

photocellSwitch = getSensorValue(sensorValue, photoCellCutOff,

photoCellCutOn, photocellSwitchOld);

photocellSwitchOld = photocellSwitch;

//>>>>>>>>>Inserting the cut code here<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

if(photocellSwitch == 1 && currentHour >= 17 && currentHour <= 23) {

//Serial.println("Entered first loop!");

for(int i=0; i<9; i++){

lightLevel[i] = 1;

}

lightLevel[15] = 1;

}

else if(photocellSwitch == 1 && currentHour >= 0 && currentHour <= 8){

//Serial.println("Entered second loop!");

for(int i=0; i<9; i++){

lightLevel[i] = 1;

}

lightLevel[15] = 1;

}

else if(photocellSwitch == 0 && currentHour >= 5 && currentHour <= 8){

//Serial.println("Entered third loop!");

for(int c=0; c<17; c++){

lightLevel[c] = 0;

}

}

//>>>>>>>>>>end of insert<<<<<<<<<<<<<<<<<<<

//////////////Holiday lighting/////////////////////////

As a final today, we add a little status report to our display, telling us, which relays should be activated (1) and which not (0). That will make trouble shooting at a later stage a little easier. If we jump around in front of a PIR and the state of that circuit does not change to 1 than it's most likely, that we have a problem with the connected PIR. If it does change to 1 but the lights don't come on and you are sure it is not caused by the photocell which is still telling the system that's bright daylight and we don't need the lights in that specific room, than the weak link on the output is the relay.
 We have 16 sift register outputs and nearly all of them controlling a relay to switch a circuit. There for we have a quick look at our output command at:
masterSwitchStateOld = switchState[18]; //setting the switchState to old

 }

///////////////////////////Output/////////////////////////////////////////////////

for(int i=0; i<17; i++) { //loop through the light output array

outputL += lightOutput[i]; //adding up the numbers

}

Remember, we are giving every room as output command the integer equivalent of a 16 bit binary number and store it in the lightOutput[] array.

1 ------- 0000000000000001 ---------- for room 1
2 ------- 0000000000000010 ---------- for room 2
4 ------- 0000000000000100 ---------- for room 3
8 ------- 0000000000001000 ---------- for room 4
16 ----- 0000000000010000 ---------- for room 5
32 ----- 0000000000100000 ---------- for room 6
64 ----- 0000000001000000 ---------- for room 7
128 --- 0000000010000000 ---------- for room 8
256 --- 0000000100000000 ---------- for room 9

and so on

to get our output, we add up the numbers where the light have to be switched on, like room1, room3 and room 6 would give as the sum of 1, 4 and 32 equals 37. Converted back to a binary, that would give us 0000000000100101. If you remember to read binaries from right to left, you can tell again which room is activated (1) and which not (0).

Since we are counting up integers, we must convert it into a binary. Simple task.

First we set the cursor of our LCD screen to the first row and first column since we are displaying date and time in the second row.
Lcd.setCursor(0, 0);

As next we display the summed up values in outputL as binary.

Lcd.print(outputL, BIN);

and we are done.

To insert our lcd.print statements, we go right to the end of the main loop.


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

lcd.setCursor(0,0);

lcd.print(outputL, BIN);

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

//Serial.print("Output value: ");

//Serial.print(outputL);

//Serial.print(" ");

//Serial.println(outputL, BIN);

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

}

No comments:

Post a Comment