#include <SPI.h> // import SPI library
#define LATCH_PIN 27 // RCLK (ST_CP)
#define BUTTON_INC 34 // Increase count
#define BUTTON_DEC 35 // Decrease count
// ===== Digit patterns (Common Cathode) ====
byte digits[10] =
{
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
//======= specific syntax for interupt========
volatile bool BTN_INC = false; //check if score increase button is pressed
volatile bool BTN_DEC = false; //check if score decrease button is pressed
int LeftDigit = 0;
int RightDigit = 0;
void IRAM_ATTR INC_ISR() // interupt function for score increase
{
BTN_INC = true;
}
void IRAM_ATTR DEC_ISR() // interupt function for score decrease
{
BTN_DEC = true;
}
void setup()
{
pinMode(LATCH_PIN, OUTPUT);
pinMode(BUTTON_INC, INPUT_PULLUP);
pinMode(BUTTON_DEC, INPUT_PULLUP);
SPI.begin();
digitalWrite(LATCH_PIN, HIGH);
attachInterrupt(digitalPinToInterrupt(BUTTON_INC),INC_ISR,FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON_DEC),DEC_ISR,FALLING);
}
void loop()
{
delay(2000);
LeftDigit++;
if(LeftDigit > 9)
{
LeftDigit = 0;
}
if(BTN_INC)
{
BTN_INC = false;
RightDigit++;
if(RightDigit > 9)
{
RightDigit = 0;
}
}
if(BTN_DEC)
{
BTN_DEC = false;
RightDigit--;
if(RightDigit < 0)
{
RightDigit = 9;
}
}
digitalWrite(LATCH_PIN,LOW);
SPI.transfer(digits[RightDigit]);
SPI.transfer(digits[LeftDigit]);
digitalWrite(LATCH_PIN,HIGH);
}