/*
Project: ShiftRegister_7Seg_Demo
Description: In a real circuit add transistors on each digit!
The gray wires are digits, colored are the segments.
Written for common cathode displays.
Creation date: 11/10/24
Author: AnonEngineering
License: https://en.wikipedia.org/wiki/Beerware
*/
const int MAX_DIGITS = 4;
// pin definitions
const int CLOCK_PIN = 8;
const int LATCH_PIN = 7;
const int DATA_PIN = 6;
const int POT_PIN = A0;
const uint8_t DIGIT_PINS[MAX_DIGITS] = {2, 3, 4, 5};
// lookup table, which segments are on for each value
const int SEG_BYTES[] = {
0xFC, /* 0 */
0x60, /* 1 */
0xDA, /* 2 */
0xF2, /* 3 */
0x66, /* 4 */
0xB6, /* 5 */
0xBE, /* 6 */
0xE0, /* 7 */
0xFE, /* 8 */
0xF6, /* 9 */
0x00 /* blank */
};
// for hardware test
void testDisplay() {
writeShiftDigit(0, 1);
writeShiftDigit(1, 2);
writeShiftDigit(2, 3);
writeShiftDigit(3, 4);
}
void updateDisplay(int value) {
// with leading zero blanking (10 is a blank char)
int displayValK = value < 1000 ? 10 : (value % 10000) / 1000; // kilo
int displayValH = value < 100 ? 10 : (value % 1000) / 100; // hecto
int displayValD = value < 10 ? 10 : (value % 100) / 10; // deka
int displayValU = value % 10; // units
// write values to display
writeShiftDigit(0, displayValK);
writeShiftDigit(1, displayValH);
writeShiftDigit(2, displayValD);
writeShiftDigit(3, displayValU);
}
void writeShiftDigit(int digit, int number) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, SEG_BYTES[number]);
digitalWrite(LATCH_PIN, HIGH);
digitalWrite(DIGIT_PINS[digit], LOW);
digitalWrite(DIGIT_PINS[digit], HIGH);
}
void setup() {
//Serial.begin(115200);
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
for (int i = 0; i < MAX_DIGITS; i++) {
pinMode(DIGIT_PINS[i], OUTPUT);
}
}
void loop() {
int potVal = analogRead(POT_PIN);
// update display often
updateDisplay(potVal);
//testDisplay();
}