#include "SevSeg.h"
SevSeg sevseg;

int num[4] = {0, 0, 0, 0};
bool button = true;

void setup() {
  for (int i = 0; i < 4; i++) {
    pinMode(i + 16, INPUT_PULLUP);
  }
  
  byte numDigits = 4;
  byte digitPins[] = {2, 3, 4, 5};
  byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
  bool resistorsOnSegments = false;
  byte hardwareConfig = COMMON_ANODE;
  bool updateWithDelays = false;
  bool leadingZeros = true;
  bool disableDecPoint = true;

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
  updateWithDelays, leadingZeros, disableDecPoint);
  sevseg.setBrightness(90);
}

void loop() {
  if (button) {
    button = !button; 
    for (int i = 0; i < 4; i++) {
      if (digitalRead(i + 16) == LOW) {
        int j = map(i, 0, 3, 3, 0);
        num[j]++;
        while (digitalRead(i + 16) == LOW) { }
      }
    }
    if (digitalRead(16) == HIGH && digitalRead(17) == HIGH && digitalRead(18) == HIGH && digitalRead(19) == HIGH) {
      button = !button;
    }
  }
  for (int i = 0; i < 4; i++) {
    if (num[i] > 9) {
      num[i] = 0;
    }
  }
  int num0 = num[0];
  for (int i = 1; i < 4; i++) {
    if (num[i] != 0) {
        int power_of_10 = 1;
        for (int j = 0; j < i; j++) {
            power_of_10 *= 10;
        }
        num0 += num[i] * power_of_10;
    }
  }
  sevseg.setNumber(num0, 0);
  sevseg.refreshDisplay();
}