// 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
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)
unsigned long curMillis = 0;
int interval = 200; // Set the number dwell time before changing
int btnUp = 2;
int btnDown = 3;
int btnReset = 13;
int loopCount = 0;
byte arrNums[] = {
B11111100,B01100000,B11011010,
B11110010,B01100110,B10110110,
B10111110,B11100000,B11111110,
B11110110};
// Set initial state (lit up with 0's) or return to zero with the reset button
void setZero(){
loopCount = 0;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[0]);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[0]);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[0]);
digitalWrite(latchPin, HIGH);
}
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(btnUp, INPUT_PULLUP);
pinMode(btnDown, INPUT_PULLUP);
pinMode(btnReset, INPUT_PULLUP);
Serial.begin(9600);
setZero();
}
// --- Use the buttons: red = decrement, green = increment
void loop(){
if(digitalRead(btnUp) == LOW || digitalRead(btnDown) == LOW ){
if(digitalRead(btnUp) == LOW){
loopCount++;
loopCount = (loopCount > 999) ? 0 : loopCount;
}else{
loopCount--;
loopCount = (loopCount <= 0) ? 999 : loopCount;
}
digitalWrite(latchPin, LOW);
// If using common anode, xor array values to flip the bits
/*
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[loopCount%10] ^ B11111111);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[loopCount/10%10] ^ B11111111);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[loopCount/100%10] ^ B11111111);
*/
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[loopCount%10]);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[loopCount/10%10]);
shiftOut(dataPin, clockPin, LSBFIRST, arrNums[loopCount/100%10]);
digitalWrite(latchPin, HIGH);
delay(interval);
}
if(digitalRead(btnReset) == LOW){
setZero();
}
}