How to program 74HC595 shift register?

First of all, I know that there are many tutorials about how to program 74HC595 shift register. Also Arduino provides the code with some pre-made functions. But I really want to get to know 74HC595 and program it by myself. First I connected all the inputs to push buttons and everything was working as it should be, but after that, I just connected the inputs of the chip to an Arduino. The target is to just write the bits that are in the data_bits to LEDs, but instead, every LED just lights up.

Here's the code:

#define data 11
#define latch 8
#define clock 12

boolean data_bits[8] = {LOW, HIGH, LOW, HIGH, LOW, HIGH, HIGH, HIGH};

void update_data(){
  digitalWrite(latch, LOW);
  for(int i = 7; i >= 0; i--){
    digitalWrite(clock, LOW);
    digitalWrite(data, data_bits[i]);
    digitalWrite(clock, HIGH);
    }
  digitalWrite(latch, HIGH);
  }

void setup(){
  pinMode(data, OUTPUT);
  pinMode(latch, OUTPUT);
  pinMode(clock, OUTPUT);
  }
  
void loop(){
    update_data();
  }

ShftOutExmp1_3.gif

Try printing out the index i in your for loop with serial print and looking at the output on the serial monitor.

Do you see what you expect?

Remember that your update_data function will run many hundreds of times a second making any output that is sometimes on and sometimes off flash so fast it will look like it is on all the time.

Grumpy_Mike:
Remember that your update_data function will run many hundreds thousands of times a second making any output that is sometimes on and sometimes off flash so fast it will look like it is on all the time.

One way to test this may be to move the update_data() function into setup so it only runs once and see what the status of the LEDs is. Loop() remains empty.