Let's Make Robots!

arduino Shift in, can anyone explain to me this function please ?

hello guys,

i am trying to make a shiftIn with a shift register, in which i connected 3 push buttons to the shift register and 3 Leds to the arduino

so i am trying to make this work but first can anyone please explain to me what does this function mean ?

byte shiftIn(int myDataPin, int myClockPin) {
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;

  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
//we will be holding the clock pin high 8 times (0,..,7) at the
//end of each time through the for loop

//at the begining of each loop when we set the clock low, it will
//be doing the necessary low to high drop to cause the shift
//register's DataPin to change state based on the value
//of the next bit in its serial information flow.
//The register transmits the information about the pins from pin 7 to pin 0
//so that is why our function counts down
  for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    //delayMicroseconds(0.2);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      //set the bit to 0 no matter what
      myDataIn = myDataIn | (1 << i);
    }
    else {
      //turn it off -- only necessary for debuging
     //print statement since myDataIn starts as 0
      pinState = 0;
    }

    //Debuging print statements
    //Serial.print(pinState);
    //Serial.print("     ");
    Serial.println (myDataIn, BIN);

    digitalWrite(myClockPin, 1);

  }
  //debuging print statements whitespace
  //Serial.println();
  //Serial.println(myDataIn, BIN);
  return myDataIn;
}

AttachmentSize
circuit.jpg1.31 MB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

If your real-life setup matches the picture you show, then you tact switches are in sideways. 

no the thing is i always get confused when i use the pushbuttons of fritzing but in real life everything is good because every push button has just 2 pins not 4

No need to get confused. Left mouse click on the pin shows you the connections...

ah ok thanks i didn't know about that, well i consider it as 2 pins button not 4

The bar | is a bitwise or and the << is bitshift left. If I'm reading it right it's like saying "binary add myDataIn to a left shifted version of myDataIn."

Debounce is up to you to handle. I don't know how well software debounce would work.

so the bar is like the logic gates or something OR, NOR, XOR ...

It's stepping through and reading each of the 8 pins in the shift register then sending the information to the serial monitor.

yes but i don't understand the way it is coded !

what does this mean by the way ? myDataIn = myDataIn | (1 << i); (the way of writing it)

and what about debouncing ?