const int digitPins[4] = {2, 4, 16, 17}; // Pins for common anode digits
const int segmentPins[7] = {18, 5, 23, 22, 21, 19, 25}; // Pins for segments (A, B, C, D, E, F, G)

const int delayCyfry = 100;
const int delayLiczba = 100;

// Define each digit in terms of segments (0-9)
const byte digitSegments[10] = {
  B11000000, // 0
  B11111001, // 1
  B10100100, // 2
  B10110000, // 3
  B10011001, // 4
  B10010010, // 5
  B10000010, // 6
  B11111000, // 7
  B10000000, // 8
  B10010000  // 9
};

unsigned long startTime = 0; // Start time of the stopwatch

void setup() {
  for (int i = 0; i < 4; i++) {
    pinMode(digitPins[i], OUTPUT);
    digitalWrite(digitPins[i], LOW); // Turn off all digits initially
  }

  for (int i = 0; i < 7; i++) {
    pinMode(segmentPins[i], OUTPUT);
    digitalWrite(segmentPins[i], LOW); // Turn off all segments initially
  }

  startTime = millis(); // Record the start time
}

void displayDigit(int digit) {
  for (int i = 0; i < 7; i++) {
    digitalWrite(segmentPins[i], bitRead(digitSegments[digit], i)); // Set segment pins according to the digit pattern
  }
}

void loop() {
  unsigned long elapsedTime = millis() - startTime; // Calculate elapsed time
  unsigned int seconds = (elapsedTime / 1000) % 60; // Calculate seconds
  unsigned int minutes = (elapsedTime / 1000 / 60) % 60; // Calculate minutes

  displayDigit(minutes / 10); // Display minutes (tens digit)
  digitalWrite(digitPins[0], HIGH); // Turn on the first digit
  delay(delayCyfry);
  digitalWrite(digitPins[0], LOW); // Turn off the first digit
  
  displayDigit(minutes % 10); // Display minutes (units digit)
  digitalWrite(digitPins[1], HIGH); // Turn on the second digit
  delay(delayCyfry);
  digitalWrite(digitPins[1], LOW); // Turn off the second digit
  
  displayDigit(seconds / 10); // Display seconds (tens digit)
  digitalWrite(digitPins[2], HIGH); // Turn on the third digit
  delay(delayCyfry);
  digitalWrite(digitPins[2], LOW); // Turn off the third digit
  
  displayDigit(seconds % 10); // Display seconds (units digit)
  digitalWrite(digitPins[3], HIGH); // Turn on the fourth digit
  delay(delayCyfry);
  digitalWrite(digitPins[3], LOW); // Turn off the fourth digit
  
  delay(delayLiczba); // Update the display every 500 milliseconds
}