/* Define shift register pins used for seven segment display */
#define LATCH_PIN 4
#define CLOCK_PIN 7
#define DATA_PIN 8
#define POT_PIN A0
/* Segment byte maps for numbers 0 to 9 */
const byte SEGMENT_MAP[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
/* Byte maps to select digit 1 to 4 */
const byte DIGIT_SELECT[] = {0xF1, 0xF2, 0xF4, 0xF8};
void setup() {
Serial.begin(9600);
/* Set DIO pins to outputs */
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
}
/* Main program */
void loop() {
int potValue = analogRead(POT_PIN);
Serial.print("Potentiometer Value: ");
Serial.println(potValue);
/* Convert potValue to a 4-digit displayable number (0-9999) */
int displayValue = map(potValue, 0, 1023, 0, 9999);
/* Display the value with a scrolling effect */
for (int i = 0; i < 4; i++) {
DisplayDigitWithDelay(displayValue, 100);
}
}
/* Function to display a value on all 4 digits with delay between updates */
void DisplayDigitWithDelay(int value, int delayTime) {
WriteNumberToSegment(0, value / 1000); // Extract thousands digit
WriteNumberToSegment(1, (value / 100) % 10); // Extract hundreds digit
WriteNumberToSegment(2, (value / 10) % 10); // Extract tens digit
WriteNumberToSegment(3, value % 10); // Extract units digit
delay(delayTime);
}
/* Write a decimal number between 0 and 9 to one of the 4 digits of the display */
void WriteNumberToSegment(byte digit, byte value) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, SEGMENT_MAP[value]);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, DIGIT_SELECT[digit]);
digitalWrite(LATCH_PIN, HIGH);
}