// NOTE: DEMO ONLY!!
// Each LED segment should be current limited with a 220ohm resistor
// The clock pin of the shift registers (SHCP) must be connected to
// the GND via a 100nF ceramic capacitor
#include <Button.h>
int compareMatchReg;
volatile int interrupts;
int latchPin = 8;//Pin connected to ST_CP of each shift register
int clockPin = 12; //Pin connected to SH_CP of each shift register
int dataPin = 11; //Pin connected to DS of first shift register
// Connect Q7S of the first shift register to DS of the next register(s)
Button btnUp(A0);
Button btnDown(A1);
byte arrNums[] = {
B11111100,B01100000,B11011010,
B11110010,B01100110,B10110110,
B10111110,B11100000,B11111110,
B11110110};
byte blank = B11111111;
// Set initial digit values here,
// Could probably break this up into array once passed as a 5 digit number
int n1 = 1;
int n2 = 9;
int n3 = 8;
int n4 = 6;
int s1 = 0;
void setup()
{
Serial.begin(9600);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
btnUp.begin();
btnDown.begin();
// Set up timer interrupts to keep the LED segments lit up
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
compareMatchReg = 15624; // preload timer from calc above
TCNT1 = compareMatchReg; // preload timer
TCCR1B |= (1 << CS11 + CS10); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts
}
ISR(TIMER1_OVF_vect) // interrupt service routine
{
TCNT1 = compareMatchReg; // preload timer
interrupts++;
// Refresh each digit in the 4 digit LED
// Uses persistence of vision, in fact it is updating
// each digit so fast the eyes don't see it blink
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 128);
shiftOut(dataPin, clockPin, LSBFIRST, ~arrNums[n1]);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[s1]);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 64);
shiftOut(dataPin, clockPin, LSBFIRST, ~arrNums[n2]);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[s1]);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 32);
shiftOut(dataPin, clockPin, LSBFIRST, ~arrNums[n3]);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[s1]);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 16);
shiftOut(dataPin, clockPin, LSBFIRST, ~arrNums[n4]);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[s1]);
digitalWrite(latchPin, HIGH);
}
void loop(){
if(btnUp.pressed()){
n4++;
}
if(btnDown.pressed()){
n4--;
}
n4 = constrain(n4,0,9);
}