Translate

Tuesday 11 March 2014

Room Management System - Coding the processing - part 1


Before we put in the first lines of code we need to do a little more planing. Let's have a look at what our room management system is supposed to do.
Generally speaking, the system is supposed to control two light circuits. One circuit which is switched on through PIR's independently for every single room and switched off by the RMS after a preprogrammed time delay. The second circuit is controlling all the other little lights which are still switched manually like bed side lights, the lights behind the TV, the lights in the wardrobe etc.. This part of the RMS activates only the circuit and provides the ability to switch this lights on and switches off the circuit if the main door was opened and no more presence is sensed in the apartment.
The 3. part is controlling the AC-units. It will activate the circuit soon as human presence is sensed and provides the ability to switch on the units. Individual units will switch off soon as a window or balcony door is opened for more than a preprogrammed time. All units will switch off with the master relay when the door was opened and no more human presence is sensed in the apartment.
For bedrooms we nee to take in account that we do not want the lights to switch on in the night while asleep. Therefore we need to install a switch (push button) to tell the system not to switch the lights on until it is pressed again to cancel the priority. The same I would like in the living area. I personally like to watch a movie with just a little background light behind the TV without all the bright main light switched on.
Another thing we need to consider is that we might not want the main light to switch on during bright daylight.

Lets take it step by step:

We start with finding the part in the main loop of our input code where is says “///////Loop through the 8 input pins to check there status///”. From there we go down a little more the first status check “if(switchVar1 & (1)) //checking S1”.
Found it? - Great, cause now we start assigning the switches to the rooms, sensors and push buttons.
S1 will be for our PIR' in Bedroom 1 and we write this behind the commented part //checking S1 PIR bed 1
S2 will be for push button as priority switch for bedroom 1 and we add to the commented part //checking S2 priority bed 1
This goes for all the statements where we are checking the switch state. Here the additions for the rest of the switches:
S3 //checking S3 PIR bed 2
S4 //checking S4 priority switch bed 2
S5 //checking S5 PIR bed 3
S6 //checking S6 priority switch bed 3
S7 //checking S7 PIR living
S8 //checking S8 priority switch living
S9 //checking S9 bathroom 1
S10 //checking S10 bathroom 2
S11 //checking S11 bathroom 3
S12 //checking S12 bathroom 4
S13 //checking S13 kitchen
S14 //checking S14 corridor
S15 //checking S15 read switch (NC) AC bed 1
S16 //checking S16 read switch (NC) AC bed 2
S17 //checking S17 read switch (NC) AC bed 3
S18 //checking S18 read switch (NC) AC bed 4
S19 //checking S19 read switch (NC) main door

Ooops, we are missing one switch. Quickly looking through my notes, when I was doing the first planing, I did not account for the priority switch in the living area. No problem. We have a quick look at our breadboards. We need to add another switch to the top of our row of switches and connect one pin wit the 5V supply bus and the other pin with the parallel input 3 (pin 5) of our input shift register 3 (CD4021B).

S 19 IMG

Now we have to find the end of main loop:

if(switchVar3 & (1 << 1)) { //checking S18 read switch (NC)
AC bed 4
Serial.println(“Switch 18 was activated”); //debug only
switchState[17] = 1;
}
else {
switchState[17] = 0;
}
}
}

and add another switch statement:

if(switchVar3 & (1 << 2)) { //checking S19 read switch (NC)
main door
Serial.println(“Switch 19 was activated”); //debug only
switchState[18] = 1;
}
else {
switchState[18] = 0;
}

The end of our main loop looks like that after the addition:

if(switchVar3 & (1 << 1)) { //checking S18 read switch (NC)
//AC bed 4
Serial.println(“Switch 18 was activated”); //debug only
switchState[17] = 1;
}
else {
switchState[17] = 0;
}

if(switchVar3 & (1 << 2)) { //checking S19 read switch (NC)
//main door
Serial.println(“Switch 19 was activated”); //debug only
switchState[18] = 1;
}
else {
switchState[18] = 0;
}
}
}

Everything set? Lets switch on the lights in the first bedroom if the PIR is activated.
We start where we just entered the additional part of the input code:

if(switchVar3 & (1 << 2)) { //checking S19 read switch (NC)
//main door
Serial.println(“Switch 19 was activated”); //debug only
switchState[18] = 1;
}
else {
switchState[18] = 0;
}
}
//>>>>>>That's our entry point. From here we carry on
//////////////////////////////////Processing the Input////////////////////////////////////////

if(switchState[1] == 0) { //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
roomTimer[0] = millis(); //setting the timer
}
else if(switchState[0] == 0 && lightStatus[0] == 1) { //the PIR not activated but the
//lights are on
Serial.println('We are checking the timer”); //Debug only
currentTime = millis(); //setting time reference
endTime = currentTime – roomTimer[0]; //calculating the inactive time
if(endTime >= delayTime[0]) { //comparing inactive time with
//allowed delay time
Serial.println(“Time is up switching off the lights”); //Debug only
lightOutput[0] = 0; //switching off the lights
lightStatus[0] = 0; //resetting the light status
roomTimer[0] = 0; //resetting the room timer
}
}
}
else if(switchState[1] == 1 && lightStatus[0] == 1) { //if priority is activated and the
//lights are on
Serial.println(“Priority switch activated switching off the lights”); //Debug only
lightOutput[0] = 0; //switching off the lights
lightStatus[0] = 0; //resetting the light status
roomTimer[0] = 0; //resetting the room timer
priorityStatus[0] = 1; //setting the priority status bed 1
}
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
}
}

That's what we need to have the lights switched on in the first room (bed 1). But before we start testing, we have to put in a few more variables which we have to declare in the beginning of our script.
Let's go back to the part, where our variables are declared:

////////////////////////////////////Declaring the Variables//////////////////////////////////////////////////

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

long int dBed1 = 60000; //delay time in milliseconds for bedroom 1
long int dBed2 = 60000; //delay time in milliseconds for bedroom 2
long int dBed3 = 60000; //delay time in milliseconds for bedroom 3
long int dLiving = 120000; //delay time in milliseconds for living area
long int dBath1 = 180000; //delay time in milliseconds for bathroom 1
long int dBath2 = 180000; //delay time in milliseconds for bathroom 2
long int dBath3 = 180000; //delay time in milliseconds for bathroom 3
long int dBath4 = 180000; //delay time in milliseconds for bathroom 4
long int dKitchen = 120000; //delay time in milliseconds for kitchen
long int dCorridor = 60000; //delay time in milliseconds for corridor
long int dAC1 = 120000; //delay time in milliseconds for AC 1 (bed1)
long int dAC2 = 120000; //delay time in milliseconds for AC 2 (bed2)
long int dAC3 = 120000; //delay time in milliseconds for AC 3 (bed3)
long int dAC4 = 120000; //delay time in milliseconds for AC 4 (living)
long int dMaster = 240000; //delay time in milliseconds for Master Off

//////////////////////////////DO NOT MODIFY BELOW THIS LINE //////////////////////////////

////////////////////////////////////defining Arduino Pins///////////////////////////////////////////////////

const int latchPin = 2; //latchPin connected to Arduino digital pin 2
const int clockPin = 3; //clockPin connected to Arduino digital pin 3
const int dataPin = 4; //dataPin connected to Arduino digital pin 4

const int latchPinOut = 5; //latch Pin output shift register 74HC595 connect to Arduino digital
//pin 5
const int clockPinOut = 6; //clock pin output shift register 74HC595 connect to Arduino digital
//pin 6
const int dataPinOut = 7; //data pin output shift register 74HC595 connect to Arduino digital pin
//7
/////////////////Variables to hold the data for each shift register///////////////////////////////

byte switchVar1 = 0; //Data for input shift register 1
byte switchVar2 = 0; //Data for input shift register 2
byte switchVar3 = 0; //Data for input shift register 3

/////////////////////////////////all the other variables///////////////////////////////////////////////////
unsigned long delayTime[16] = {dBed1, dBed2, dBed3, dLiving, dBath1, dBath2, dBath3,
dBath4, dKitchen, dCorridor, dAC1, dAC2, dAC3, dAC4,
dMaster, 0};
unsigned int switchState[25] = {0}; //array holding the state of each switch
unsigned long lightOutput[17] = {0}; //array holding a integer which converted to binary
//will trigger the relay to switch in our output code
unsigned int lightStatus[17] = {0}; //array holding the switch status of each room on/off
unsigned long roomTimer[17] = {0}; //array holding the time when the PIR was last activated
unsigned int priorityStatus[17] = {0}; //array holding the priority status of each room on/off
unsigned long currentTime = 0; //var to hold a reference time to calculate the up time
//against the preprogrammed delay time
unsigned long endTime = 0; //var to hold a temp result to calculate the up time
//against the preprogrammed delay time


void setup() {
//////////////////////////////////Start Serial for Debugging////////////////////////////////////////

Serial.beginn(9600);

////////////////////////////////////////defining pin modes////////////////////////////////////////////

pinMode(latchPin, OUTPUT); //setting the latch pin to output
pinMode(clockPin, OUTPUT); //setting the clock pin to output
pinMode(dataPin, INPUT); //setting the data pin to input

pinMode(latchPinOut, OUTPUT); //setting the latch pin (74HC595) to output
pinMode(clockPinOut, OUTPUT); //setting the clock pin (74HC595) to output
pinMode(dataPinOut, INPUT); //setting the data pin (74HC595) to input
}
First I changed the pin assignments of the input shift registers as follows
int latchPin = 2; to const int latchPin = 2;
int clockPin = 3; to const int clockPin = 3;
int dataPin = 4; to const int dataPin = 4;

Next I have added the pin assignments for the output shift register:
const int latchPinOut = 5;
const int clockPinOut = 6;
const int dataPinOut = 7;

After that we move down to:
///////////////////////////////all the other variables////////////////////////////////////////////
and add the following lines:
unsigned long lightOutput[17] = {0}; //array holding a integer which converted to binary
//will trigger the relay to switch in our output code
unsigned int lightStatus[17] = {0}; //array holding the switch status of each room on/off
unsigned long roomTimer[17] = {0}; //array holding the time when the PIR was last activated
unsigned int priorityStatus[17] = {0}; //array holding the priority status of each room on/off
unsigned long currentTime = 0; //var to hold a reference time to calculate the up time
//against the preprogrammed delay time
unsigned long endTime = 0; //var to hold a temp result to calculate the up time
//against the preprogrammed delay time

From here we move on to the setup loop and add the following lines:

pinMode(latchPinOut, OUTPUT); //setting the latch pin (74HC595) to output
pinMode(clockPinOut, OUTPUT); //setting the clock pin (74HC595) to output
pinMode(dataPinOut, INPUT); //setting the data pin (74HC595) to input

Last for today we jump back up to the beginning of our variable declaration and add:
long int dBed1 = 60000; //delay time in milliseconds for bedroom 1
long int dBed2 = 60000; //delay time in milliseconds for bedroom 2
long int dBed3 = 60000; //delay time in milliseconds for bedroom 3
long int dLiving = 120000; //delay time in milliseconds for living area
long int dBath1 = 180000; //delay time in milliseconds for bathroom 1
long int dBath2 = 180000; //delay time in milliseconds for bathroom 2
long int dBath3 = 180000; //delay time in milliseconds for bathroom 3
long int dBath4 = 180000; //delay time in milliseconds for bathroom 4
long int dKitchen = 120000; //delay time in milliseconds for kitchen
long int dCorridor = 60000; //delay time in milliseconds for corridor
long int dAC1 = 120000; //delay time in milliseconds for AC 1 (bed1)
long int dAC2 = 120000; //delay time in milliseconds for AC 2 (bed2)
long int dAC3 = 120000; //delay time in milliseconds for AC 3 (bed3)
long int dAC4 = 120000; //delay time in milliseconds for AC 4 (living)
long int dMaster = 240000; //delay time in milliseconds for Master Off

////////////////////////////DO NOT MODIVY BELOW HERE///////////////////////////////////////

keep moving down to:

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

Most of the added statements and variables are more or less explaining itself. However, a couple of things do need to be looked into. I changed the status of the pin assignments from a variable to a constant by adding “const” to the statements. The next variable which needs a little more explanation is the lightOutput[] array. I have assigned a 1 to lightOutput[0] for room 1 (bed1) to switch on the lights and a 0 to switch them back off. The next room (bed2) will have a 2 assigned to lightOutput[1] to switch on the lights and again the 0 to switch them off. The third room will switch on with 4 and room number 4 switches on with 8.
With two shift registers in the output stage we are having a 16 bit output. Lets have a quick look again at binary numbers:
1 in a 16 bit binary will convert to 0000000000000001
2 in a 16 bit binary will convert to 0000000000000010
4 in a 16 bit binary will convert to 0000000000000100
8 in a 16 bit binary will convert to 0000000000001000
16 in a 16 bit binary will convert to 0000000000010000
32 in a 16 bit binary will convert to 0000000000100000
64 in a 16 bit binary will convert to 0000000001000000
and so on......
Our output shift register will activate the output pin according to the bit being set to 1. If we want multiple rooms switched on we have to send a number which converts to a binary corresponding to
to the bits set to 1 where we want to switch on the lights. If we want to switch on the lights in room 1 and room 3 we need to send the number 5 which converts to 0000000000000101. If we want to switch on the lights in the rooms 3, 4 and 7 we need to send the number 76 which will convert to a binary of 0000000001001100.
The last thing I have added today are our Time delay variables, holding the times after which the lights are to switch off if no more human presence is sensed in a room. The AC is set to switch off if a balcony door or a window is opened for more than 2 minutes.
I have declared separate variables and put them right in the beginning of the code to make changes easier at a later state without going through the whole code to find again where to change a particular timer. For easier coding the delay times have been put again in an array.


Here again the complete code to what we have done so far checked and debugged copied back direct from the Arduino IDE. If you just copy and past the code please be careful in some formating issues. You might end up with a lot of “stray'/'” warnings. The bug is in the Serial.print statements because the '”' formates differently in some editors.


/////////////////////Declaring the Variables/////////////////

///////////Timer and Sensitivity Settings to be changed to individual needs////////////////

long int dBed1 = 60000; //delay time in milliseconds for bedroom 1
long int dBed2 = 60000; //delay time in milliseconds for bedroom 2
long int dBed3 = 60000; //delay time in milliseconds for bedroom 3
long int dLiving = 120000; //delay time in milliseconds for living area
long int dBath1 = 180000; //delay time in milliseconds for bathroom 1
long int dBath2 = 180000; //delay time in milliseconds for bathroom 2
long int dBath3 = 180000; //delay time in milliseconds for bathroom 3
long int dBath4 = 180000; //delay time in milliseconds for bathroom 4
long int dKitchen = 120000; //delay time in milliseconds for kitchen
long int dCorridor = 60000; //delay time in milliseconds for corridor
long int dAC1 = 120000; //delay time in milliseconds for AC 1 (bed1)
long int dAC2 = 120000; //delay time in milliseconds for AC 2 (bed2)
long int dAC3 = 120000; //delay time in milliseconds for AC 3 (bed3)
long int dAC4 = 120000; //delay time in milliseconds for AC 4 (living)
long int dMaster = 240000; //delay time in milliseconds for Master Off

////////////////////////////DO NOT MODIVY BELOW HERE///////////////////////////////////////

//////////defining Arduino Pins/////////////////
const int latchPin = 2; //latch pin input connected to
//Arduino digital pin 2
const int clockPin = 3; //clock pin input connected to
//Arduino digital pin 3
const int dataPin = 4; //data pin input connected to
//Arduino digital pin 4
const int latchPinOut = 5; //latch pin output shift register
//74HC595 connected to Arduino
//digital pin 5
const int clockPinOut = 6; //clock pin output shift register
//74HC595 connected to Arduino
//digital pin 6
const int dataPinOut = 7; //data pin output shift register
//74HC595 connected to Arduino
//digital pin 7

//////////Variables to hold the data for each shift register//////////

byte switchVar1 = 0; //data for input shift register 1
byte switchVar2 = 0; //data for input shift register 2
byte switchVar3 = 0; //data for input shift register 3

////////////////all the other variables///////////////////////////
unsigned long delayTime[16] = {dBed1, dBed2, dBed3, dLiving, dBath1, dBath2, dBath3,
dBath4, dKitchen, dCorridor, dAC1, dAC2, dAC3, dAC4,
dMaster, 0};
unsigned int switchState[25] = {0}; //array holding the state of each switch
unsigned long lightOutput[17] = {0}; //array holding a integer which converted to binary
//will trigger the relay to switch in our output code
unsigned int lightStatus[17] = {0}; //array holding the switch status of each room on/off
unsigned long roomTimer[17] = {0}; //array holding the time when the PIR was last activated
unsigned int priorityStatus[17] = {0}; //array holding the priority status of each room on/off
unsigned long currentTime = 0; //var to hold a reference time to calculate the up time
//against the preprogrammed delay time
unsigned long endTime = 0; //var to hold a temp result to calculate the up time
//against the preprogrammed delay time


void setup() {
//////////////Start Serial for Debugging/////////////////////

Serial.begin(9600);

//////////////////defining pin modes////////////////////

pinMode(latchPin, OUTPUT); //setting the latch pin to output
pinMode(clockPin, OUTPUT); //setting the clock pin to output
pinMode(dataPin, INPUT); //setting the data pin to input

}

void loop() {
//////////////////////////////////////getting the input//////////////////////////////////////////////////
//pulse the latch pin, set to high to collect serial data
digitalWrite(latchPin, HIGH);
//give it chance to collect the data
delayMicroseconds(20);
//set latch pin to low to transmit data serially
digitalWrite(latchPin, LOW);

//while in serial mode, collect data into a byte
switchVar1 = shiftIn(dataPin, clockPin);
switchVar2 = shiftIn(dataPin, clockPin);
switchVar3 = shiftIn(dataPin, clockPin);
/////////////do something with the collected Data/////////////////////
//checks for debugging
Serial.println(); //debug only
Serial.print("Switch variable 1: "); //debug only
Serial.println(switchVar1, BIN); //debug only
Serial.println("-------------------"); //debug only
Serial.println(); //debug only
Serial.print("Switch variable 2: "); //debug only
Serial.println(switchVar2, BIN); //debug only
Serial.println("-------------------"); //debug only
Serial.println(); //debug only
Serial.print("Switch variable 3: "); //debug only
Serial.println(switchVar3, BIN); //debug only
Serial.println("-------------------"); //debug only
////////////loop through the 8 input pins to check their status////////////
for(int n=0; n<=7; n++){

//shift register 1
if(switchVar1 & (1)) { //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 1 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(switchVar1 & (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;
}
}
//////////////////processing the input/////////////////////
if(switchState[1] == 0) { //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
roomTimer[0] = millis(); //setting the timer
}
else if(switchState[0] == 0 && lightStatus[0] == 1) { //the PIR not activated but the
//lights are on
Serial.println("We are checking the timer"); //Debug only
currentTime = millis(); //setting time reference
endTime = currentTime - roomTimer[0]; //calculating the inactive time
if(endTime >= delayTime[0]) { //comparing inactive time with
//allowed delay time
Serial.println("Time is up switching off the lights"); //Debug only
lightOutput[0] = 0; //switching off the lights
lightStatus[0] = 0; //resetting the light status
roomTimer[0] = 0; //resetting the room timer
}
}
}
else if(switchState[1] == 1 && lightStatus[0] == 1) { //if priority is activated and the
//lights are on
Serial.println("Priority switch activated switching off the lights"); //Debug only
lightOutput[0] = 0; //switching off the lights
lightStatus[0] = 0; //resetting the light status
roomTimer[0] = 0; //resetting the room timer
priorityStatus[0] = 1; //setting the priority status bed 1
}
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
}
}




///////////////////shiftIn function to get the input data////////////////////////////////////

byte shiftIn(int myDataPin, int myClockPin) {
//////////////////declaring some varables///////////////
int i; //counter
int temp = 0; //var to hold the raw input data
int pinState; //var holding the state of the pin
byte myDataIn = 0; //var holding the complete incoming data

/////////////////setting the pin modes///////////////////
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, INPUT);
///////////////////// loop through the single input pins////////////
for(i=0; i>7; i--) {
digitalWrite(myClockPin, LOW); //setting the clock pin low to trigger the output
delayMicroseconds(2); //give it chance to send the data
temp = digitalRead(myDataPin); //check if the pin was set (switch was pressed)
if(temp) { //if the pin was set
pinState = 1; //we set the pin state to 1 accordingly
myDataIn = myDataIn | (1 << i); //setting the state in our binary output
}
else {
pinState = 0; //if the pin was not set, the pin state stays 0
}
Serial.print("PinState: "); //debug only
Serial.print(pinState); //debug only
Serial.print(" "); //debug only
Serial.println(myDataIn, BIN); //debug only

digitalWrite(myClockPin, HIGH); //setting the clock pin high
}
Serial.println(); //debug only
Serial.println(myDataIn, BIN); //debug only

return myDataIn; //after looping through all the input pins returning the
//collected input data to the main programm
}

No comments:

Post a Comment