// Pin mapping for 7-segment display (for common cathode)
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8}; // A, B, C, D, E, F, G
const int digitPins[] = {9, 10, 11}; // DIG1, DIG2, DIG3
// Segment patterns for digits 0-9
const byte digitPatterns[10] = {
B00111111, // 0
B00000110, // 1
B01011011, // 2
B01001111, // 3
B01100110, // 4
B01101101, // 5
B01111101, // 6
B00000111, // 7
B01111111, // 8
B01101111 // 9
};
// Array to hold the numbers to display on each digit
int initialNumber[3] = {0, 0, 0}; // Initialize with zeros (3 digits)
void setup() {
Serial.begin(9600); // Start serial communication
initializeDisplay(); // Call the setup function for the display
Serial.println("Enter 3-digit number:"); // Initial prompt
}
void loop() {
updateDisplay(); // Continuously display the numbers on each digit
readUserInput(); // Check for user input
}
// Function to setup the display
void initializeDisplay() {
// Set segment pins as output
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Set digit pins as output and turn off all digits initially
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
digitalWrite(digitPins[i], HIGH); // Turn off all digits
}
}
// Function to display different numbers on each digit
void updateDisplay() {
for (int i = 0; i < 3; i++) { // Loop through each digit
int number = initialNumber[i]; // Get the number for the current digit
byte segments = digitPatterns[number]; // Get segment pattern for the number
// Set the segments for the current number
for (int j = 0; j < 7; j++) {
digitalWrite(segmentPins[j], (segments >> j) & 0x01); // Set segments
}
// Activate only the current digit
digitalWrite(digitPins[i], LOW); // LOW to turn on the current digit
// Short delay to allow the digit to stay on
delay(2); // 2ms delay for persistence of vision
// Turn off the current digit before moving to the next
digitalWrite(digitPins[i], HIGH); // HIGH turns off the digit
}
}
// Function to check for user input
void readUserInput() {
if (Serial.available() > 0) {
String input = Serial.readString(); // Read the input
input.trim(); // Remove any leading/trailing whitespace
// Check if the input is exactly 3 digits
if (input.length() == 3 && input.charAt(0) >= '0' && input.charAt(0) <= '9' &&
input.charAt(1) >= '0' && input.charAt(1) <= '9' &&
input.charAt(2) >= '0' && input.charAt(2) <= '9') {
// Fill the initialNumber array with the input
for (int i = 0; i < 3; i++) {
initialNumber[i] = input.charAt(i) - '0'; // Convert char to int
}
Serial.print("Displaying: "); // Print the display numbers to the Serial Monitor
Serial.println(input);
Serial.println("Enter 3-digit number:"); // Prompt for next input
} else {
Serial.println("Error: Please enter exactly 3 digits.");
Serial.println("Enter 3-digit number:"); // Prompt for next input
}
}
}