/*
* 7-Segment Display Counter with Arduino Uno
*
* This project demonstrates a digital counter using:
* - 4-digit 7-segment display (common cathode)
* - Push buttons for control (Up, Down, Reset)
* - Automatic counting modes
*
* Features:
* - Count up: 0000 to 9999
* - Count down: 9999 to 0000
* - Manual increment/decrement with buttons
* - Reset to 0000
* - Auto counting mode
* - Speed control
* - Display multiplexing
*/
// 7-Segment Display Pin Definitions
// Segment pins (a, b, c, d, e, f, g, dp)
#define SEG_A 2
#define SEG_B 3
#define SEG_C 4
#define SEG_D 5
#define SEG_E 6
#define SEG_F 7
#define SEG_G 8
#define SEG_DP 9 // Decimal point (optional)
// Digit select pins (D1, D2, D3, D4)
#define DIGIT_1 10 // Thousands
#define DIGIT_2 11 // Hundreds
#define DIGIT_3 12 // Tens
#define DIGIT_4 13 // Ones
// Button pins
#define BTN_UP A0 // Increment counter
#define BTN_DOWN A1 // Decrement counter
#define BTN_RESET A2 // Reset to 0
#define BTN_MODE A3 // Toggle auto count mode
// Counter variables
int counter = 0;
int maxCount = 9999;
int minCount = 0;
// Button states
bool lastUpState = HIGH;
bool lastDownState = HIGH;
bool lastResetState = HIGH;
bool lastModeState = HIGH;
// Auto count mode
bool autoCountMode = false;
bool countUp = true; // true = count up, false = count down
unsigned long lastAutoCount = 0;
int autoCountDelay = 1000; // 1 second delay
// Display refresh
unsigned long lastRefresh = 0;
int currentDigit = 0;
// 7-segment digit patterns (0-9)
// Array represents: a, b, c, d, e, f, g
byte digitPatterns[10][7] = {
{1,1,1,1,1,1,0}, // 0
{0,1,1,0,0,0,0}, // 1
{1,1,0,1,1,0,1}, // 2
{1,1,1,1,0,0,1}, // 3
{0,1,1,0,0,1,1}, // 4
{1,0,1,1,0,1,1}, // 5
{1,0,1,1,1,1,1}, // 6
{1,1,1,0,0,0,0}, // 7
{1,1,1,1,1,1,1}, // 8
{1,1,1,1,0,1,1} // 9
};
// Segment pins array for easy access
int segmentPins[8] = {SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G, SEG_DP};
// Digit pins array
int digitPins[4] = {DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4};
void setup() {
// Initialize Serial for debugging
Serial.begin(9600);
Serial.println(F("7-Segment Counter Initialized"));
Serial.println(F("=============================="));
Serial.println(F("Controls:"));
Serial.println(F(" BTN_UP (A0): Increment"));
Serial.println(F(" BTN_DOWN (A1): Decrement"));
Serial.println(F(" BTN_RESET (A2): Reset to 0"));
Serial.println(F(" BTN_MODE (A3): Auto Count Mode"));
Serial.println(F(""));
// Configure segment pins as outputs
for (int i = 0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT);
digitalWrite(segmentPins[i], LOW);
}
// Configure digit pins as outputs
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
digitalWrite(digitPins[i], HIGH); // Common cathode: HIGH = OFF
}
// Configure button pins as inputs with pull-up resistors
pinMode(BTN_UP, INPUT_PULLUP);
pinMode(BTN_DOWN, INPUT_PULLUP);
pinMode(BTN_RESET, INPUT_PULLUP);
pinMode(BTN_MODE, INPUT_PULLUP);
// Display startup animation
startupAnimation();
}
void loop() {
// Check button states
checkButtons();
// Auto counting mode
if (autoCountMode) {
autoCount();
}
// Refresh display (multiplexing)
refreshDisplay();
}
// Startup animation - count from 0 to 9 on all digits
void startupAnimation() {
for (int i = 0; i <= 9; i++) {
unsigned long startTime = millis();
while (millis() - startTime < 100) {
displayNumber(i * 1111); // Show same digit on all positions
}
}
counter = 0; // Reset to 0 after animation
}
// Check all button states
void checkButtons() {
// Read button states
bool upState = digitalRead(BTN_UP);
bool downState = digitalRead(BTN_DOWN);
bool resetState = digitalRead(BTN_RESET);
bool modeState = digitalRead(BTN_MODE);
// Up button - Increment counter
if (upState == LOW && lastUpState == HIGH) {
delay(50); // Debounce
if (digitalRead(BTN_UP) == LOW) {
incrementCounter();
autoCountMode = false; // Disable auto mode on manual input
}
}
lastUpState = upState;
// Down button - Decrement counter
if (downState == LOW && lastDownState == HIGH) {
delay(50); // Debounce
if (digitalRead(BTN_DOWN) == LOW) {
decrementCounter();
autoCountMode = false; // Disable auto mode on manual input
}
}
lastDownState = downState;
// Reset button - Reset to 0
if (resetState == LOW && lastResetState == HIGH) {
delay(50); // Debounce
if (digitalRead(BTN_RESET) == LOW) {
resetCounter();
autoCountMode = false; // Disable auto mode on reset
}
}
lastResetState = resetState;
// Mode button - Toggle auto count
if (modeState == LOW && lastModeState == HIGH) {
delay(50); // Debounce
if (digitalRead(BTN_MODE) == LOW) {
toggleAutoMode();
}
}
lastModeState = modeState;
}
// Increment counter
void incrementCounter() {
counter++;
if (counter > maxCount) {
counter = minCount; // Wrap around
}
Serial.print(F("Counter: "));
Serial.println(counter);
}
// Decrement counter
void decrementCounter() {
counter--;
if (counter < minCount) {
counter = maxCount; // Wrap around
}
Serial.print(F("Counter: "));
Serial.println(counter);
}
// Reset counter to 0
void resetCounter() {
counter = 0;
Serial.println(F("Counter Reset!"));
}
// Toggle auto counting mode
void toggleAutoMode() {
autoCountMode = !autoCountMode;
if (autoCountMode) {
Serial.println(F("Auto Count Mode: ON"));
Serial.print(F("Direction: "));
Serial.println(countUp ? "UP" : "DOWN");
} else {
Serial.println(F("Auto Count Mode: OFF"));
}
}
// Auto counting function
void autoCount() {
unsigned long currentTime = millis();
if (currentTime - lastAutoCount >= autoCountDelay) {
if (countUp) {
incrementCounter();
// Switch to count down when reaching max
if (counter >= maxCount) {
countUp = false;
}
} else {
decrementCounter();
// Switch to count up when reaching min
if (counter <= minCount) {
countUp = true;
}
}
lastAutoCount = currentTime;
}
}
// Display a 4-digit number
void displayNumber(int number) {
// Extract individual digits
int digit1 = (number / 1000) % 10; // Thousands
int digit2 = (number / 100) % 10; // Hundreds
int digit3 = (number / 10) % 10; // Tens
int digit4 = number % 10; // Ones
// Display each digit in sequence (multiplexing)
displayDigit(0, digit1);
delay(2);
displayDigit(1, digit2);
delay(2);
displayDigit(2, digit3);
delay(2);
displayDigit(3, digit4);
delay(2);
}
// Display a single digit at a specific position
void displayDigit(int position, int digit) {
// Turn off all digits
for (int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], HIGH); // Common cathode: HIGH = OFF
}
// Set segments for the digit
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], digitPatterns[digit][i]);
}
// Turn on the selected digit
digitalWrite(digitPins[position], LOW); // Common cathode: LOW = ON
}
// Refresh display with current counter value
void refreshDisplay() {
displayNumber(counter);
}
// Function to display a specific pattern (for testing)
void testPattern() {
// Display "8888" - all segments on
for (int i = 0; i < 100; i++) {
displayNumber(8888);
}
}
// Function to blank the display
void blankDisplay() {
for (int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], HIGH);
}
for (int i = 0; i < 8; i++) {
digitalWrite(segmentPins[i], LOW);
}
}
// Function to display custom message (optional)
void displayCustom(int d1, int d2, int d3, int d4) {
for (int i = 0; i < 50; i++) {
displayDigit(0, d1);
delay(2);
displayDigit(1, d2);
delay(2);
displayDigit(2, d3);
delay(2);
displayDigit(3, d4);
delay(2);
}
}
Comments
Post a Comment