Using a 74HC595 manually

Hello everyone!

I am trying to control a 74HC595 shift register using an Arduino. When my code didn't work I tried the standard example which uses the built-in function (ShiftOut()) and confirmed that the IC does work and all the breadboard connections and pins are wired correctly, so the question remains, what is wrong with my code? And possibly how does the built-in ShiftOut() actually work, and how does it differ from what I wrote?

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 4;
////Pin connected to DS of 74HC595
int dataPin = 10;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  digitalWrite(latchPin, LOW);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);
  Serial.begin(115200);


  bool bit = false;
  Serial.println("Before for loop");
  for (int i = 0; i <8; i++)
  {
    // Write bit to dataPin
    if (bit) digitalWrite(dataPin, HIGH);
    else digitalWrite(dataPin, LOW);
    // Take clock pin HIGH
    digitalWrite(clockPin, HIGH);
    // Wait until 74HC595 writes the bit
    delay(20);
    // Take clock pin LOW
    digitalWrite(clockPin, LOW);
    bit != bit;
    // Debug outputs to serial
    Serial.println("For loop");
    Serial.println(i);
  }
  Serial.println("After for loop");

  // Take latch pin HIGH
  digitalWrite(latchPin, HIGH);
  delay(20);
  digitalWrite(latchPin, LOW);
}

void loop() {
  
}

What it does is that writes one bit a at a time on the dataPin (SER in my datasheet), then takes the clockPin (SRCLK in my datasheet) high for 20ms and switches it low again, and after 8 bits switches the latchPin (RCLK in my datasheet) high for 20ms and the low again in order to transfer the 8 written bits from the shift register to the storage register. The Output Enable pin (OE inverted in the datasheet) is wired permanently LOW so that outputs are always enabled. So is the SRCLR inverted input (shift register clear) wired always LOW. The only connections are the three pins written in the code and which, when used with ShiftOut() work.

What am I doing wrong here? Can someone give me a hand?

So what do you imagine this operation does?

bit != bit;

Perhaps you imagine it means

bit = !bit;

which is doesn't, since its the comparison operator 'not equal'...

Note that the operators +=, -=, ^= are formed from binary operators, and ! is not
a binary operator... Also the comparison operators are already used, so <=, != etc are
not available for this idiom.

You were exactly right. I thought it meant

bit = !bit;

and that it would invert the boolean, but now that you said it I realize it's a comparison operator and the statement just evaluates to true without doing anything. I changed that instruction and now it works, thank you very much!

That was a wonderful example of "how to ask a question on a large public form"!
Clear statement of the problem and various evidence. Short, complete, nicely formatted and correctly posted source code. Statement of how you thought it was working. Just ... pretty close to perfect!
(and look how little time it took to get a helpful answer!)

how does the built-in ShiftOut() actually work

The source code for ShiftOut() is buried somewhere in your Arduino install directory, and you can look at it ("Open Source", you know.)
It might be easier to browse online in the Arduino github repositories.