shiftIn function

hi all.

im useing arduino and C for allmost 2years now.
one of the first projects, have been to get the 74HC165 working on arduino.

i never got this working nice, also never realy needed it.
until now. so i needed to solve this.

i see many projects on the WEB but they use 4pins. and i get strange results from them.
and i know i should be possible with 3 pins.

but i got it working perfect now, and i have been comparing my program and the one that comes with the IDE “shiftIn”

the problem with the “shiftIn” function is that it allways misses the first bit.

here be differnce between my function and the “shiftÏn” function from the IDE

IDE “shiftIn” function

uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
  uint8_t value = 0;
  uint8_t i;

  for (i = 0; i < 8; ++i) {
    digitalWrite(clockPin, HIGH);
    if (bitOrder == LSBFIRST)
      value |= digitalRead(dataPin) << i;
      value |= digitalRead(dataPin) << (7 - i);
    digitalWrite(clockPin, LOW);
  return value;

my fnction

byte shiftin(byte dataPin, byte clkPin, byte bitOrder) {
  byte shiftvalue = 0;
  byte x;
  for (x = 0; x < 8; x++) {
    digitalWrite(clkPin, LOW);
    if (bitOrder == LSBFIRST)
      shiftvalue |= digitalRead(dataPin) << x;
      shiftvalue |= digitalRead(dataPin) << (7 - x);
    digitalWrite(clkPin, HIGH);
  return shiftvalue;

the only differnce is that i keep the clkpin:
clk_LOW → get data → clk_HIGH

and the IDE function does:
clk_HIGH → get data - > clk_LOW

this way you miss the first bit.

kind regards, Emile B

Thank you. I've spent about 4 hours trying to figure out why I was missing bytes from my shift register, and you provided the solution.

(though - for any future readers - I was getting inconsistent results before, trying to trigger the shift register from a button press, using code from the Arduino tutorial page at: