#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.
}

void setup() {

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

  updateShiftRegister();
  Serial.begin(9600);
  Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
}
void loop() {
  // available reports received bytes. read removes one byte from the USB serial buffer.
  if (Serial.available()) {
    const char ch = Serial.read();
    if (ch >= '0' && ch <= '7') {
      const uint8_t led = ch - '0';  // ASCII subtraction turns a digit character into 0..7.
      bitSet(leds, led); updateShiftRegister();
      Serial.print("Turned on LED "); Serial.println(led);
    }
    if (ch == 'x') {
      leds = 0; updateShiftRegister(); Serial.println("Cleared");
    }
    // Other characters, including a terminal's newline, are ignored. No wires use D0/D1.
  }
}
