Translate

Friday, 7 March 2014

Room Management System - Coding the Input - part 3

Today we will make the first part of our light control system work. But first we need to do some coding again since we are still missing our shiftIn – function:


//////////////////////////////////////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=7; i>0; 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
}

Let's have a closer look what we are doing in out shiftIn – function:
The shiftIn function will return a byte with each bit in the byte corresponding to a input pin of the shift register. The left bit (bit 7) = pin 8 and bit 0 = pin 1.

When we are calling our function we need to pass on the variables holding the location of our data and our clock pin which is done with the function call in the main loop:

switchVarn = shiftIn(dataPin, clockPin);

Our function starts with

byte shiftIn(int myDataPin, myClockPin) {

Since we got our pin locations, we loop through our input pins. In the end of our loop we'll be holding the clock pin high.
At the beginning of each loop we set the clock low to get the necessary low to high drop to get the shift register's data pin to change state based on the value of the next bit in the serial information flow. The shift register transmits the data of it's pin in a reverse order from pin 8 down to pin 1. Thats also the reason why we are counting down in our for loop:

for(i=7; i>0; i—) {

The Serial.print statements are again for debugging and to follow the work flow of our sketch.

Everything ready? Please make sure you double checked your connections, upload the sketch to your Arduino board, switch on the serial monitor and start pressing some buttons. If everything went well you will get the feedback in the serial monitor to every button you press. If the numbers are passing to fast go back to the main loop and add a 1 to 2 second delay:

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

delay(1000);

}

In the next part we will be back at the breadboard to build the output stage.

No comments:

Post a Comment