title: Toggle a single bit on Arduino + 74HC165 + SPI
body: <p>Please, help me. I'm trying to connect the 74HC165 (8-bit input shift register) to Arduino UNO via SPI to check the state of 8 buttons. The response is going to be processe to change variable "controls", but it doesn't work properly.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;SPI.h&gt;
  
  /*  Latch contact */
  enum { REG_LATCH = 8 };
  int speed = 100;
  
  /*  Variable to store the Controls state for further transfer */
  static uint8_t controls = 0;
  
  void setup()
  {
    /*  Turn ON the UART  */
    Serial.begin(9600);
    /*  Turn On the SPI */
    SPI.begin();
    pinMode(REG_LATCH, OUTPUT);
    digitalWrite(REG_LATCH, HIGH);
  }
  
  void loop()
  {
    /*  Storing the previous system state   */
    static uint8_t previous_input = 00000000;
  
    digitalWrite(REG_LATCH, LOW);
    digitalWrite(REG_LATCH, HIGH);
  
    /*  Read the inputs from shift register */  
    uint8_t input = SPI.transfer(0);
  
    /* If anything has ben changed - report to UART */
  
    if (input != previous_input)
      {  
        /*  Remebmer changed positions */
        uint8_t changed = input ^ previous_input;
  
        /*  Remember current state */
        previous_input = input;       
  
        Serial.println("Buttons State:\t\tControls State:");
        /* Run trough all inputs from shift register  */
        for (int i = 0; i &lt; 8; i++)
          {   
            /* Print the state of currently checked input*/
            Serial.print(input &amp; 1);
            /* If button is pressed and previously it was not, then  */
            if ((input &amp; 1) &amp; (changed &amp; 1))
              {
                /* Highlight the current changes in system */
                Serial.print("_");                            
                /* Toggle controls in this position */
                controls = controls ^ (1 &lt;&lt; i);
               }; /*Otherwise do nothing*/
  
            /*  Move to next bit of inputs. */
            input &gt;&gt;= 1;
            changed &gt;&gt;= 1;
          };
  
        Serial.print("\t\t");  
        for (int i = 0; i &lt; 8; i++)
          {   
            /* Print current control  */
            Serial.print(controls);
            controls &gt;&gt;= 1;
          }
        Serial.println();       
    }
  }
  </code></pre>
  
  <p>And what it writes to the Serial:</p>
  
  <p>1) After pressing button 1:</p>
  
  <pre><code>Buttons State:      Controls State:
  1_0000000       10000000
  </code></pre>
  
  <p>2) When it releases:</p>
  
  <pre><code>Buttons State:      Controls State:
  00000000        00000000
  </code></pre>
  
  <p>3) After pressing button 7:</p>
  
  <pre><code>Buttons State:      Controls State:
  0000001_0       64321684210
  </code></pre>
  
  <p>4) After its releasing:</p>
  
  <pre><code>Buttons State:      Controls State:
  00000000        00000000
  </code></pre>
  
  <p>5) After pressing button 1:</p>
  
  <pre><code>Buttons State:      Controls State:
  1_0000000       10000000
  </code></pre>
  
  <p>6) When it releases:</p>
  
  <pre><code>Buttons State:      Controls State:
  00000000        00000000
  </code></pre>
  
  <p>How it supposed to work:</p>
  
  <p>1) After pressing button 1:</p>
  
  <pre><code>Buttons State:      Controls State:
      1_0000000       10000000
  </code></pre>
  
  <p>2) After its release:</p>
  
  <pre><code>Buttons State:      Controls State:
      00000000        10000000
  </code></pre>
  
  <p>3) After pressing button 7:</p>
  
  <pre><code>Buttons State:      Controls State:
  0000001_0       10000010
  </code></pre>
  
  <p>4) After its releasing</p>
  
  <pre><code>Buttons State:      Controls State:
  00000000        10000010
  </code></pre>
  
  <p>5) After next pressing of button 1:</p>
  
  <pre><code>Buttons State:      Controls State:
      1_0000000       00000010
  </code></pre>
  
  <p>6) After its releasing:</p>
  
  <pre><code>Buttons State:      Controls State:
      00000000        00000010
  </code></pre>
  
  <p>Help me, please.
  What am I doing wrong?</p>