const int digitPins[] = {2, 15, 14};  // Common anode digit pins
const int segmentPins[] = {12, 4, 19, 26, 27, 13, 18, 25};  // 7-segment segment pins

// Define common anode 7-segment patterns for digits 0-9
const byte digitPatterns[] = {
  B00111111,  // 0
  B00000110,  // 1
  B01011011,  // 2
  B01001111,  // 3
  B01100110,  // 4
  B01101101,  // 5
  B01111101,  // 6
  B00000111,  // 7
  B01111111,  // 8
  B01101111   // 9
};

const int displayInterval = 1000;  // Update every 1000 milliseconds (1 second)
unsigned long previousMillis = 0;

int count = 0;
int hundreds = 0;
int tens = 0;
int ones = 0;
void setup() {
Serial.begin(96000);
  for (int i = 0; i < 3; i++) {
    pinMode(digitPins[i], OUTPUT);
    digitalWrite(digitPins[i], LOW);  // Common anode, so set LOW initially to turn off
  }
  for (int i = 0; i < 8; i++) {
    pinMode(segmentPins[i], OUTPUT);
    digitalWrite(segmentPins[i], HIGH);  // Turn off all segments initially
  }
  previousMillis=millis();
}

void loop() {
  
  unsigned long currentMillis = millis();
  displayCount(count);
    if (currentMillis - previousMillis >= displayInterval) {
     previousMillis = currentMillis;    
    displayCount(count);    
    count++;
    Serial.println(count);
    if (count > 999) {
      count = 1;
    }
  }
}

void displayCount(int count) {

   hundreds = count / 100;
  tens = (count / 10) % 10;
  ones = count % 10;

  displayDigit(0, ones);
  delay(1);
  displayDigit(1, tens);
  delay(1);
  displayDigit(2, hundreds);
  delay(1);
}

void displayDigit(int digit, int value) {
  for (int i = 0; i < 3; i++)
    digitalWrite(digitPins[i], LOW);
  for (int segment = 0; segment < 8; segment++)
    digitalWrite(segmentPins[segment], HIGH);
  digitalWrite(digitPins[digit], HIGH);

  for (int segment = 0; segment < 8; segment++) {
    if (bitRead(digitPatterns[value], segment)) {
      digitalWrite(segmentPins[segment], LOW);
    }
  }
}