Translate

Friday 14 March 2014

Room Management System – Coding the output


About bits, bytes and binary

As previously mentioned, we are working with bit, bytes and binary to make our room management system work. A quick review, 1 byte holds 8 bits and 1 bit can store 1 single digit. The shift registers we are using have 8 in- or output pins. Every in- or output pin is allowed to use one bit of data.
Computer chips or microprocessor, also commonly known are pretty dumb. They only know what we are telling them in a very simple form – 0 and 1 of on and off. That's where our binary numbers come in. Binary code or numbers consisting only of 0's and 1's what we again use to read the data the input shift registers are giving us or to send the data to the output shift register.
The only trick is now to find the position which has to be set to 1 or 0 to switch the right LED or relay on or off. The normal state of our shift register pins will be 0 and we have 8 of them. A input shifter will send us 00000000 unless we give a input to one of the pins. Output shifter will be set off unless we are sending a 1 to the shift register. Now we want to control which pin is set to on. There for we have to know our binary numbers. Since I have explained this subject already with the input coding, we only have a little bit a closer look at controlling the output shifters.
Please recheck with the data sheet of the shift register you are using about any of the following pin names, position and numbering. On a 8 bit shift register the pins are usually numbered from 1 to 8 or 0 to 7. As usual, we have to pay a little more attention to numbering starting with 0. I'll be referring to pins at there position like first, second, third ….
If we want to address the first pin of our shift register we need to send the number 1 which reeds in a 8 bit binary 00000001.
Since we are working with 2 shift register, we double the output pins and also the the storage we have to address to working on 16 bits the first output pin of our daisy chained shift registers is also the pin 1 in the chain of shift registers 0000000000000001.
To address the second pin in our chain we need to send the number 2 controlling the second pin of the first shift register in the chain and converts in binary to 0000000000000010.
To cut a long story short, I made a small table with integer to binary conversion and which pin in our chain will be addressed.

1..........0000000000000001..........first output pin..........first shift register
2..........0000000000000010..........second output pin.....first shift register
4..........0000000000000100..........third output pin........ first shift register
8..........0000000000001000..........fourth output pin......first shift register
16........0000000000010000..........fifth output pin.........first shift register
32........0000000000100000..........sixth output pin........first shift register
64....... 0000000001000000..........seventh output pin....first shift register
128..... 0000000010000000..........eight output pin........first shift register
------------------------------------------------------------------------------------------
256......0000000100000000..........first output pin..........second shift register
512......0000001000000000..........second output pin.....second shift register
1024....0000010000000000..........third output pin.........second shift register
2048... 0000100000000000..........fourth output pin......second shift register
4096....0001000000000000..........fifth output pin.........second shift register
8192....0010000000000000..........sixth output pin........second shift register
16384..0100000000000000..........seventh output pin...second shift register
32768..1000000000000000..........eight output pin........second shift register

For everybody needing a small refresher how binary works. The next place in a binary represents always the double integer of the current place. Check back on the above table. The first place in a binary is a 1. The second place is a 2, double of 1. The third place is a 4, double of 2. The fourth place is a 8, double of 4 and so on.
If we want to address pin 4 of the first shift register and pin 2 of the second shift register we would need to send the following binary 0000001000001000 or take the equivalent integer of each pin which would be for pin 4 first shift register a 8 and for pin 2 second shift register a 512 and simply add them together like 8 add 512 equals 520 and converts in binary back to 0000001000001000.
That's what we will be using for the out put code. If you recheck the processing part and have a look at the lightOutput[n] = x; statement, you will notice that starting from room 1 the statement will read lightOutput[0] = 1; //switching on the lights or
lightOutput[0] = 0; //switching off the lights and
at room 2
lightOutput[1] = 2; //switching on the lights or
lightOutput[1] = 0; //switching off the lights and
for room 3
lightOutput[2] = 4; //switching on the lights or
lightOutput[2] = 0; //switching off the lights and
for room 4
lightOutput[3] = 8; //switching on the lights or
lightOutput[3] = 0; //switching off the lights
and so on.

Now we only need to add up the numbers held in the lightOutput array, convert the result to binary and send it to the output shift register chain.

Here we go. Again we find the end of our main loop:

if(mainOff == 0) {                                                           //master off command is 0
Serial.println("switching off everything and reset all");       //debug only
for(int i=0; i<12; 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

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

//////////////////////////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
}

//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

}
//>>>>>>>>>>>>>>end of main loop<<<<<<<<<<<<<<<<<<<<<<

All set, now we have to add a couple of variables again. All the way up to the part where we are declaring our variables and there we find again the part where it says

/////////////////time and sensitivity settings//////////////////////////////////////////

here we add:

unsigned int sensitivity = 500;                                       //should be between 200 and 1000 as
                                                                                   //lower the number as more responsive
                                                                                   //the system will be

now we move down to the line where it says

////////////////////////////all the other variables///////////////////////////////
unsigned long delayTime[16] = {dBed1, dBed2, dBed3, dLiving, dBath1, dBath2, dBath3,
dBath4, dKitchen, dCorridor, dAC1, dAC2, dAC3, dAC4,
dMaster, 0};

and here we add

unsigned long outputL = 0;                                         //variable to hold the output

We are nearly done for today. If you are testing your sketch now, you might notice a warning in the progress window of your Arduino IDE saying something like memory low and system might get unstable. If everything is working, start commenting out the Serial.print statements which are in for debugging only anyway and to follow the work flow of the sketch.

In the next chapter we stop lights from coming on while there is bright daylight in a room and we will also implement a maintenance switch to switch on everything any time to check bulbs, AC-functions etc.


No comments:

Post a Comment