/*
Project: ShiftRegister_7Seg_Demo
Description: Counts in hexadecimal, 0 - F
Written for common cathode displays.
Creation date: 5/21/25
Author: AnonEngineering
License: https://en.wikipedia.org/wiki/Beerware
*/
const unsigned long ONE_SEC = 1000;
// pin definitions
const int CLOCK_PIN = 12;
const int LATCH_PIN = 11;
const int DATA_PIN = 10;
// lookup table, which segments are on for each value
const byte SEG_BYTES[] = { // hex 0x00 - 0x0F and blank
// mapped order
// D F G B A C DP E
0b11011101, // 0: A B C D E F
0b00010100, // 1: B C
0b10111001, // 2: A B D E G
0b10111100, // 3: A B C D G
0b01110100, // 4: B C F G
0b11101100, // 5: A C D F G
0b11101101, // 6: A C D E F G
0b00011100, // 7: A B C
0b11111101, // 8: A B C D E F G
0b11111100, // 9: A B C D F G
0b01111101, // A: A B C E F G
0b11100101, // b: C D E F G
0b11001001, // C: A D E F G
0b10110101, // d: B C D E G
0b11101001, // E: A D E F G
0b01101001, // F: A E F G
0b00000000 // Blank
};
unsigned long previousTime = 0;
int count = 0;
// for hardware test
void testDisplay(int number) {
writeShiftDigit(number);
}
void writeShiftDigit(int number) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, SEG_BYTES[number]);
digitalWrite(LATCH_PIN, HIGH);
}
void setup() {
//Serial.begin(115200);
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
}
void loop() {
if (millis() - previousTime >= ONE_SEC) {
previousTime = millis();
count++;
if (count > 16) count = 0;
}
// update display often
writeShiftDigit(count);
//testDisplay(10); // put a number here to test lookup table
}