Translate

Thursday 6 March 2014

Room Management System - Coding the Input Stage - part 2

No long small talk today, right into coding the main loop.

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/////////////////////
//check for debug
Serial.println(); //debug only
Serial.print(“Switch variable 1: “); //debug only
Serial.println(switchVar1, BIN); //debug only
Serial.println(“--------------------------”); //debug only
Serial.print(“Switch variable 2: “); //debug only
Serial.println(switchVar2, BIN); //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 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;
}
}
}

Let's have a look at our main loop. In the first part we pulse the latch pin, set it to high to collect serial data, put in a small delay to give it chance to collect all data. Next we set the latch pin back on low to transmit the collected data serially to our Arduino board. In the next step we collect the transmitted data into our switch variable (switchVar1 to switchVar3) by calling the “shiftIn” function.
For debugging and to see what the switchVar1 to 3 are holding, we print them out with the following Serial.print() commands. Since we want to process binary numbers, I am using additional the command “Serial.println(switchVar1, BIN)” which shows the held result as a binary. Later, when the system is ready and everything is working well, all the Serial.print commands can be deleted or commented out.
In the next statement, we are checking if n-th bit is set. At this point I am guessing that you all now the relation between bit, byte and binary. OK, a quick refresher:
1 byte holds 8 bits. We are working with 8 bit shift registers, meaning every shift register can handle 8 switch statements. Again, our shift register inputs know only on or off, respectively 1 or 0. Every statement uses 1 bit of processor memory. Binary numbers, as well only know 1 and 0. For processing we need to know if a switch or button was activated (1) or not (0). Every shift register on our board is giving as a 8 bit binary code like 00000000 if nothing was activated or 00000001 if the first button was pressed.
To find out the state or the number which bit is holding we are using the following statements:
if(switchVar2 & (1 << 0)) or if(switchVar2 & (1)) to check if the first bit was set, is 1. If it was set, we load it into our switchState[] - array for further processing.
The same we do with the second bit if(switchVar2 & (1 << 1)) and so on.
Here a quick reference table how it words:

switchVar1 & (1 << 0) or switchVar1 & (1)      00000001
switchVar1 & (1 << 1)                                      00000010
switchVar1 & (1 << 2)                                      00000100
switchVar1 & (1 << 3)                                      00001000
switchVar1 & (1 << 4)                                      00010000
switchVar1 & (1 << 5)                                      00100000
switchVar1 & (1 << 6)                                      01000000
switchVar1 & (1 << 7)                                      10000000

Some good explanation about bit operations can be found here: http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/

The “Serial.println(“Switch n was activated”)” is for know only a feedback to see if our little sketch is right and the right switch is recognised.

In the next part we have a closer look at the shift in function to make the sketch work.


1 comment:

  1. A little mistake in here as well. I should not just copy and past but...
    Please check the last statement under shift register 2.

    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)) { //<<<<<<<<<<<<< Please change to
    //if(switchVar2 & (1 << 7)) {
    Serial.println(“Switch 13 was activated”); //debug only
    switchState[12] = 1;
    }
    else {
    switchState[12] = 0;
    }

    //shift register 3

    ReplyDelete