// Define the connection of LEDs to Arduino pins
const int ledPins[] = {13, 12, 11, 10, 9, 8, 7, 6}; // Arduino pins in descending order

void setup() {
  Serial.begin(9600);
  // Set all pins as outputs and start with all LEDs turned off
  for (int i = 0; i < 8; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW); // Ensure all LEDs start turned off
  }
  Serial.println("Setup complete: All LEDs initially turned OFF.");
}

void loop() {
  uint8_t PORTX = 0b00000000; // Simulate an 8-bit register controlling LEDs

  // Iterate through each LED, turning it on and off for demonstration
  for (int pin = 0; pin < 8; pin++) {
    PORTX |= (1 << pin); // Turn on LED at the current pin
    updateLeds(PORTX);
    Serial.print("LED at Arduino pin ");
    Serial.print(ledPins[pin]);
    Serial.println(" turned ON.");
    delay(1000);

    PORTX &= ~(1 << pin); // Turn off LED at the current pin
    updateLeds(PORTX);
    Serial.print("LED at Arduino pin ");
    Serial.print(ledPins[pin]);
    Serial.println(" turned OFF.");
    delay(1000);
  }

  // Turn on all even-numbered Arduino pins (12, 10, 8, 6)
  PORTX |= 0b10101010;
  updateLeds(PORTX);
  Serial.println("Even-numbered Arduino pins (12, 10, 8, 6) turned ON.");
  delay(5000);

  // Toggle the same even-numbered pins
  PORTX ^= 0b10101010;
  updateLeds(PORTX);
  Serial.println("Even-numbered Arduino pins (12, 10, 8, 6) TOGGLED.");
  delay(5000);

  // Masking operation: Only allow changes to lower four bits
  uint8_t mask = 0b00001111;
  PORTX = (PORTX & mask);
  updateLeds(PORTX);
  Serial.println("Higher bits (4-7) are MASKED; lower bits (0-3) unchanged.");
  delay(5000);

  // Set all accessible LEDs to ON (lower four bits)
  PORTX = (PORTX & mask) | 0b00001111;
  updateLeds(PORTX);
  Serial.println("All lower four LEDs (Arduino pins 13, 12, 11, 10) set to ON.");
  delay(5000);

  // Resetting for next loop
  PORTX = 0b00000000;
  updateLeds(PORTX);
  Serial.println("All LEDs turned OFF. Restarting sequence...");
  delay(7000); // Longer delay before restarting sequence
}

void updateLeds(uint8_t portx) {
  for (int i = 0; i < 8; i++) {
    digitalWrite(ledPins[i], portx & (1 << i) ? HIGH : LOW);
  }
}
$abcdeabcde151015202530354045505560fghijfghij