// Pin definitions
const int DSR = 2;     // Serial Input Right
const int DSL = 3;     // Serial Input Left
const int S0 = 4;      // Mode Select 0
const int S1 = 5;      // Mode Select 1
const int MR = 6;      // Master Reset
const int OE = 7;      // Output Enable
const int CP = 8;      // Clock Pulse
const int PINS[8] = {9, 10, 11, 12, 13, A0, A1, A2}; // Parallel inputs P0-P7





void setup() {
  // Set pin modes
  pinMode(DSR, OUTPUT);
  pinMode(DSL, OUTPUT);
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(MR, OUTPUT);
  pinMode(OE, OUTPUT);
  pinMode(CP, OUTPUT);
  for (int i = 0; i < 8; i++) {
    pinMode(PINS[i], OUTPUT);
  }

  // Initialize all control pins
  digitalWrite(MR, HIGH); // Ensure no reset
  digitalWrite(OE, LOW);  // Enable outputs
  digitalWrite(CP, LOW);
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);

  Serial.begin(9600);
}

void loop() {
  // Mode 1: Parallel Load
  Serial.println("Parallel Load");
  setMode(LOW, LOW);
  parallelLoad(0b10101010); // Example data
  clockPulse();
  delay(1000);

  // Mode 2: Serial Shift Left
  Serial.println("Serial Shift Left");
  setMode(LOW, HIGH);
  for (int i = 0; i < 8; i++) {
    digitalWrite(DSL, (i % 2 == 0)); // Example alternating bits
    clockPulse();
    delay(500);
  }

  // Mode 3: Serial Shift Right
  Serial.println("Serial Shift Right");
  setMode(HIGH, LOW);
  for (int i = 0; i < 8; i++) {
    digitalWrite(DSR, (i % 2 == 1)); // Example alternating bits
    clockPulse();
    delay(500);
  }

  // Mode 4: Hold
  Serial.println("Hold");
  setMode(HIGH, HIGH);
  delay(2000); // Hold data for 2 seconds

  // Reset the shift register
  digitalWrite(MR, LOW);
  delay(10);
  digitalWrite(MR, HIGH);
}


void setMode(bool s1, bool s0) {
  digitalWrite(S1, s1);
  digitalWrite(S0, s0);
}

void clockPulse() {
  digitalWrite(CP, HIGH);
  delayMicroseconds(10);
  digitalWrite(CP, LOW);
}

void parallelLoad(uint8_t data) {
  for (int i = 0; i < 8; i++) {
    digitalWrite(PINS[i], (data >> i) & 0x01);
  }
}
74hc299Breakout