#include <LedControl.h>

int DIN = 11;
int CS = 10;
int CLK = 13;
LedControl lc = LedControl(DIN, CLK, CS, 0);

int displayValue = 0;

int buttonPinA = 6;
int buttonPinB = 7;

bool countingStarted = false;
bool countingDown = false;
bool countingFinished = false; 

void setup() {
  lc.shutdown(0, false);
  lc.setIntensity(0, 15);

  pinMode(buttonPinA, INPUT_PULLUP);
  pinMode(buttonPinB, INPUT_PULLUP);
}

void loop() {
 
  bool buttonAState = digitalRead(buttonPinA);

  if (buttonAState == LOW && !countingStarted) {
    countingStarted = true;
    countingDown = false;
  }


  bool buttonBState = digitalRead(buttonPinB);

  if (buttonBState == LOW && !countingStarted) {
    countingStarted = true;
    countingDown = true;
  }

  if (countingStarted && !countingFinished) { 
    angkaMatrix(displayValue);
    delay(1000);

    if (countingDown) {
      displayValue--;

      if (displayValue < 0) {
        displayValue = 9;
      }
    } else {
      displayValue++;

      if (displayValue > 9) {
        displayValue = 0;
        countingFinished = true; 
      }
    }
  }
}

void angkaMatrix(int num) {
  lc.clearDisplay(0);

  byte numbers[10][8] = {
   {B00111100,B01000010,B01000010,B01000010,B01000010,B01000010,B01000010,B00111100}, // 0
  {B00011000,B00011100,B00011000,B00011000,B00011000,B00011000,B00011000,B00111100}, //1
  {B00111100,B01000010,B01100000,B00110000,B00011000,B00001100,B00000110,B01111110}, //2
  {B00111100,B01000010,B01000000,B01111110,B01000000,B01000010,B00111100,B00000000}, // 3
  {B00100000,B00110000,B00101000,B00100100,B01111110,B00100000,B00100000,B00100000}, // 4
  {B00111110,B00000010,B00001110,B00010000,B00100000,B00100000,B00100000,B00011110}, // 5
  {B00011000,B00000100,B00000010,B00000010,B00011110,B00100010,B00100010,B00011100}, // 6
  {B00111110,B01000000,B01000000,B00100000,B00010000,B00001000,B00001000,B00001000}, // 7
  {B00000000,B00011100,B00100010,B00100010,B00011100,B00100010,B00100010,B00011100}, // 8
  {B00011100,B00100010,B00100010,B00111100,B00100000,B00100000,B00100010,B00011100}  // 9
  };

  if (num >= 0 && num <= 9) {
    for (int i = 0; i < 8; i++) {
      lc.setRow(0, i, numbers[num][i]);
    }
  }
}