Translate

Sunday 18 May 2014

Room Management System – Some more improvements and another bug fix


Again a suggestion from Rene to change the way the input is handled. First we have to go to the top of the declaration part and add a couple of definitions:

/////////////////////Includes/////////////////////////////

#include <DS1307RTC.h>

#include <Time.h>

#include <Wire.h>

#include <ShiftLCD.h>

#include <avr/pgmspace.h>

//>>>>>>>>>>>>>Addition starts here<<<<<<<<<<<<<

#define ON 1 // (RRKM-01) The switch is in ON state

#define OFF 0 // (RRKM-01) The switch is in OFF state

//>>>>>>>>>>>>>Addition ends here<<<<<<<<<<<<<

From here we move down to the main loop in to the “////////////loop through the 8 input pins to check their status////////////” section and there we replace the whole section.

////////////loop through the 8 input pins to check their status////////////

//>>>>>>>>>>>>>Replacement starts here<<<<<<<<<<<<<

//shift register 1

if(switchVar1 & (1 << 0)) { //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;

}

if(switchVar3 & (1 << 4)) { //checking S21

//Serial.println("Switch 20 was activated."); //debug only

switchState[20] = 1;

}

else {

switchState[20] = 0;

}

if(switchVar3 & (1 << 5)) { //checking S22

//Serial.println("Switch 21 was activated."); //debug only

switchState[21] = 1;

}

else {

switchState[21] = 0;

}

if(switchVar3 & (1 << 6)) { //checking S23

//Serial.println("Switch 22 was activated."); //debug only

switchState[22] = 1;

}

else {

switchState[22] = 0;

}

//////////////Debug Statements//////////////////////////////////

#ifdef DA_DEBUG

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

Serial.print("Switch state: ");

Serial.print(c);

Serial.print(" / ");

Serial.println(switchState[c]);

delay(500);

}

#endif

//>>>>>>>>>>>>>>Replacement ends here<<<<<<<<<<<<<

with the code below:

// (RRKM-01) shift register 1

switchState[0] = IIFi((switchVar1 & (1)), ON, OFF); //checking S1

switchState[1] = IIFi((switchVar1 & (1 << 1)), ON, OFF); //checking S2

switchState[2] = IIFi((switchVar1 & (1 << 2)), ON, OFF); //checking S3

switchState[3] = IIFi((switchVar1 & (1 << 3)), ON, OFF); //checking S4

switchState[7] = IIFi((switchVar1 & (1 << 4)), ON, OFF); //checking S5

switchState[6] = IIFi((switchVar1 & (1 << 5)), ON, OFF); //checking S6

switchState[5] = IIFi((switchVar1 & (1 << 6)), ON, OFF); //checking S7

switchState[4] = IIFi((switchVar1 & (1 << 7)), ON, OFF); //checking S8

// (RRKM-01) shift register 2

switchState[8] = IIFi((switchVar2 & (1)), ON, OFF); //checking S9

switchState[9] = IIFi((switchVar2 & (1 << 1)), ON, OFF); //checking S10

switchState[10] = IIFi((switchVar2 & (1 << 2)), ON, OFF); //checking S11

switchState[11] = IIFi((switchVar2 & (1 << 3)), ON, OFF); //checking S12

switchState[15] = IIFi((switchVar2 & (1 << 4)), ON, OFF); //checking S13

switchState[14] = IIFi((switchVar2 & (1 << 5)), ON, OFF); //checking S14

switchState[13] = IIFi((switchVar2 & (1 << 6)), ON, OFF); //checking S15

switchState[12] = IIFi((switchVar2 & (1 << 7)), ON, OFF); //checking S16

// (RRKM-01) shift register 3

switchState[16] = IIFi((switchVar3 & (1)), ON, OFF); //checking S17

switchState[17] = IIFi((switchVar3 & (1 << 1)), ON, OFF); //checking S18

switchState[18] = IIFi((switchVar3 & (1 << 2)), ON, OFF); //checking S19

switchState[19] = IIFi((switchVar3 & (1 << 3)), ON, OFF); //checking S20

switchState[20] = IIFi((switchVar3 & (1 << 4)), ON, OFF); //checking S21

switchState[21] = IIFi((switchVar3 & (1 << 5)), ON, OFF); //checking S22

switchState[22] = IIFi((switchVar3 & (1 << 6)), ON, OFF); //checking S23

//////////////Debug Statements//////////////////////////////////

#ifdef DA_DEBUG_in // (RRKM-01) Only when debugging

for(int x=0; x<23; x++) {

Serial.println( "Status of Switch S" + String(x+1) + ": " +

IIFs((switchState[x] == ON), "ON", "OFF" )

);

}

#endif

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

To make it work we also have to add to functions IIFs() and IIFi(). But since we also have a few other things to change, we do this last not to jump up and down in the code. You may have noticed, that I removed the maintenancePin variable and replaced it with switchState[19]. That means, we do not need the variable any more and can delete it in the declaration part. We also need to update the part where it is used. There fore we go down towards end of the main loop and find the part and change

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

}

if(maintenancePin == 1) { //<<<<<<<<<REPLACE with switchState[19]<<<<<<<<

for(int i=1; i>17; i++){ //loop through all circuits

lightStatus[i] = 1; //setting the light status of everything

roomTimer[i] = millis()/1000; //setting all the room timers

}

outputL = 32767; //setting the output

//binary 0111111111111111

}

lcd.setCursor(0,0);

lcd.print(outputL, BIN);

so it reads after

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

}

if(switchState[19] == 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()/1000; //setting all the room timers

}

outputL = 32767; //setting the output

//binary 0111111111111111

}

lcd.setCursor(0,0);

lcd.print(outputL, BIN);

Why we are here, I have noticed the other day that the outside lights do not come on with the maintenance switch and this is simple due to the fact that I forgot to add the integer value of bit 16 to the output value. Please add 32768 to the outputL value where we check switchState[19] so it reads after

outputL = 65535; //which reads as binary 1111111111111111

Now we have to go back up a little to the beginning of the “holiday timer” section. Remember, in the menu post 8 we added the corridor (room 10) in to the possible schedule. When we check the photocell to see if the light level is OK to switch on the lights, we are looping only through 9 rooms.

Let's correct it and find the following part:


photocellSwitch = getSensorValue(sensorValue, photoCellCutOff,

photoCellCutOn, photocellSwitchOld);

photocellSwitchOld = photocellSwitch;

//allowing the lights to switch on between 17:00 and 23:00 h

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

for(int i=0; i<9; i++){ //<<<<<<<<<<<<<Replace 9 with 10<<<<<<<<<<<<<

lightLevel[i] = 1;

}

lightLevel[15] = 1;

}

//allowing the lights to switch on between 00:00 and 08:00 h

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

for(int i=0; i<9; i++){ //<<<<<<<<<<<<<Replace 9 with 10<<<<<<<<<<<<<

lightLevel[i] = 1;

}

lightLevel[15] = 1;

}

//make sure the lights switch off on sunrise during morning hours

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

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

lightLevel[c] = 0;

}

}

#ifdef DA_DEBUG_photo

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

Serial.println(photocellSwitch);

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

Serial.print("Light level ");

Serial.print(c);

Serial.print(" :");

Serial.println(lightLevel[c]);

}

#endif

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

so this part reads after correction:


photocellSwitch = getSensorValue(sensorValue, photoCellCutOff,

photoCellCutOn, photocellSwitchOld);

photocellSwitchOld = photocellSwitch;

//allowing the lights to switch on between 17:00 and 23:00 h

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

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

lightLevel[i] = 1;

}

lightLevel[15] = 1;

}

//allowing the lights to switch on between 00:00 and 08:00 h

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

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

lightLevel[i] = 1;

}

lightLevel[15] = 1;

}

//make sure the lights switch off on sunrise during morning hours

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

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

lightLevel[c] = 0;

}

}

#ifdef DA_DEBUG_photo

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

Serial.println(photocellSwitch);

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

Serial.print("Light level ");

Serial.print(c);

Serial.print(" :");

Serial.println(lightLevel[c]);

}

#endif

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

As to days final the two functions needed for the new code to work.


///////////////////some easy inline logical functions////////////////////////////////////

int IIFi( boolean iLogicalValue, int iWhenTrue, int iWhenFalse ) {

if (iLogicalValue) return iWhenTrue;

else return iWhenFalse;

}

String IIFs( boolean iLogicalValue, String iWhenTrue, String iWhenFalse ) {

if (iLogicalValue) return iWhenTrue;

else return iWhenFalse;

}


2 comments:

  1. Hey Dieter,

    In IIFi and IIFs => if (iLogicalValue)...
    shoud read => if (iLogicalValue == true)...

    We discussed this last weekend
    Cheers, Rene

    ReplyDelete
  2. Hi Rene,
    sorry for the late reply, but I was pretty busy this week and I didn't look that much into my comments.
    Your actual code was ok, it worked. The problem was again in the debug statement:

    #ifdef DA_DEBUG_in // (RRKM-01) Only when debugging

    for(int x=0; x<23; x++) {

    Serial.println( "Status of Switch S" + String(x+1) + ": " +

    IIFs((switchState[x] == ON), "ON", "OFF" ) //<<<<<<<< IIFs((switchState[x] = ON), ....

    );

    }

    #endif

    Instead of checking If the switch state is set to on (==) we where setting it to on (=) and that's why the debug information came all out wrong. And being everything set to on after debug nothing was working as it should have...

    ReplyDelete