#include <Arduino.h>
const uint8_t LATCH_PIN = 11;  // ST_CP, IC pin 12, copies shifted bits to visible outputs.
const uint8_t CLOCK_PIN = 9;   // SH_CP, IC pin 11, advances the serial shift register.
const uint8_t DATA_PIN = 12;   // DS, IC pin 14, supplies one bit for each clock edge.
uint8_t leds = 0;  // Eight bits remember which LEDs should be on.
void updateShiftRegister() {
  digitalWrite(LATCH_PIN, LOW);
  // LSBFIRST sends bit 0 first; after eight clocks it reaches Q7, not Q0.
  shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, leds);
  digitalWrite(LATCH_PIN, HIGH);  // All outputs change together after the complete byte.
}

const uint8_t LIGHT_PIN = A0;
void setup() {

  pinMode(LATCH_PIN, OUTPUT); pinMode(DATA_PIN, OUTPUT); pinMode(CLOCK_PIN, OUTPUT);

}
void loop() {
  const int reading = analogRead(LIGHT_PIN);  // LDR to +5 V, fixed 1 kΩ to GND.
  uint8_t count = reading / 57;  // Retain the supplied threshold scaling, not a lux conversion.
  if (count > 8) count = 8;
  leds = 0;
  for (uint8_t i = 0; i < count; ++i) {
    leds |= uint8_t(1U << i);  // Shift a 1 into place; OR keeps bits that are already set.
  }
  updateShiftRegister();
}
