/* Arduino 7 Segment scoreboard 
 * Using the MAX7219CNG LED Driver
 
Modified oroginal code from https://www.brainy-bits.com to add score down buttons and reset buttons
*/

#include "LedControl.h"  // Library used for communcation with 7 segment

LedControl lc=LedControl(12,11,10,1);  //  (DIN, CLK, LOAD, number of Max7219 chips)

// Variable to hold current scores
int scoreRed=0;
int scoreBlue=0;

// Variables to split whole number into single digits
int rightdigit;
int leftdigit;

// Switches pin connection to Arduino UNO
#define redUp 2
#define blueUp 3
#define redDown 4
#define blueDown 5
#define redReset 6
#define blueReset 7

void setup() {
  pinMode(redUp,INPUT_PULLUP);
  pinMode(blueUp,INPUT_PULLUP);
  pinMode(redDown,INPUT_PULLUP);
  pinMode(blueDown,INPUT_PULLUP);
  pinMode(redReset,INPUT_PULLUP);
  pinMode(blueReset,INPUT_PULLUP);
  
  lc.shutdown(0,false);  // Wake up MAX7219

  lc.setIntensity(0,15);  // Set brightness to medium

  lc.clearDisplay(0);  // Clear all displays connected to MAX7219 chip #
  lc.setScanLimit(0,4); 

// Put zeros on both displays at startup
  
  lc.setDigit(0,0,0,false);  // (Max7219 chip #, Digit, value, DP on or off) Red Score
  lc.setDigit(0,1,0,false);
  
  lc.setDigit(0,2,0,false);  // (Max7219 chip #, Digit, value, DP on or off) Blue Score
  lc.setDigit(0,3,0,false);

}


void loop() { 

  // If switch redUp is clicked
  if (!digitalRead(redUp)) {
    
    scoreRed++;  // Increase scoreRed by 1
  
    // convert whole number to single digits
    rightdigit=scoreRed%10;
    leftdigit=scoreRed%100/10;

    // Display extracted digits on the display
    lc.setDigit(0,0,leftdigit,false);
    lc.setDigit(0,1,rightdigit,false);

    // Wait until switch is released to continue
    while (!digitalRead(redUp)) { 
    }
    delay(5);  // Small delay to debounce the switch
  }

  // If switch redDown is clicked
  if (!digitalRead(redDown)) {
    
    scoreRed--;  // Decrease scoreRed by 1
  
    // convert whole number to single digits
    rightdigit=scoreRed%10;
    leftdigit=scoreRed%100/10;

    // Display extracted digits on the display
    lc.setDigit(0,0,leftdigit,false);
    lc.setDigit(0,1,rightdigit,false);

    // Wait until switch is released to continue
    while (!digitalRead(redDown)) { 
    }
    delay(5);  // Small delay to debounce the switch
  }

    // If switch redReset is clicked
    if (!digitalRead(redReset)) {
    
    scoreRed=00;  // Reset scoreRed to 0
  
    lc.setDigit(0,0,0,false);
    lc.setDigit(0,1,0,false);

    // Wait until switch is released to continue
    while (!digitalRead(redReset)) { 
    }
    delay(5);  // Small delay to debounce the switch
  }

    // If switch blueUp is clicked
    if (!digitalRead(blueUp)) {
      
    scoreBlue++;  // Increase scoreBlue by 1

    // convert whole number to single digits
    rightdigit=scoreBlue%10;
    leftdigit=scoreBlue%100/10;

    // Display extracted digits on the display
    lc.setDigit(0,2,leftdigit,false);
    lc.setDigit(0,3,rightdigit,false);

    // Wait until switch is released to continue
    while (!digitalRead(blueUp)) { 
    }    
    delay(5); // Small delay to debounce the switch
  }

  // If switch blueDown is clicked
  if (!digitalRead(blueDown)) {
    
    scoreBlue--;  // Decrease scoreBlue by 1
  
    // convert whole number to single digits
    rightdigit=scoreBlue%10;
    leftdigit=scoreBlue%100/10;

    // Display extracted digits on the display
    lc.setDigit(0,2,leftdigit,false);
    lc.setDigit(0,3,rightdigit,false);
    
    // Wait until switch is released to continue
    while (!digitalRead(blueDown)) { 
    }
    delay(5);  // Small delay to debounce the switch
  }

    // If switch blueReset is clicked
    if (!digitalRead(blueReset)) {
    
    scoreBlue=00;  // Reset scoreBlue to 0
  
    lc.setDigit(0,2,0,false);
    lc.setDigit(0,3,0,false);

    // Wait until switch is released to continue
    while (!digitalRead(blueReset)) { 
    }
    delay(5);  // Small delay to debounce the switch
  }
  
}
