// Pin configuration for 74HC595
const int latchPin = 8;
const int clockPin = 12;
const int dataPin = 11;
// Pin configuration for pushbuttons
const int upButtonPin = 2;
const int downButtonPin = 3;
const int stopButtonPin = 4;
const int resetButtonPin = 5;
// Variables
const float TIME = 50; // 350 .... ~1 second
long counter = 0;
int countDirection = 2; // 1 for increment, 0 for decrement
// Define the segments for each digit
const byte segments[] =
{
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110 // 9
};
void setup()
{
// Set the pins as outputs
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// Set the pins as outputs
pinMode(upButtonPin, INPUT_PULLUP);
pinMode(downButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
// Initialize the display
updateDisplay();
}
void loop()
{
// Check the state of the up button
if (digitalRead(upButtonPin) == LOW)
{
countDirection = 1;
}
else if (digitalRead(upButtonPin) == HIGH)
{
// nothing
}
// Check the state of the down button
if (digitalRead(downButtonPin) == LOW)
{
countDirection = 0;
}
else if (digitalRead(downButtonPin) == HIGH)
{
// nothing
}
// Check the state of the stop button
if (digitalRead(stopButtonPin) == LOW)
{
countDirection = 2;
}
else if (digitalRead(stopButtonPin) == HIGH)
{
// nothing
}
// Check the state of the reset button
if (digitalRead(resetButtonPin) == LOW)
{
countDirection = 2;
counter = 0;
}
else if (digitalRead(resetButtonPin) == HIGH)
{
// nothing
}
// Increment or decrement the counter based on the count direction
if (countDirection == 1)
{
incrementCounter();
}
else if (countDirection == 0)
{
decrementCounter();
}
// delay loop
for (int i = 0; i <= TIME; i++)
{
updateDisplay();
}
}
// Functions //
void updateDisplay()
{
// Extract individual digits
long digitTenMillions = counter / 10000000;
long digitMillions = (counter / 1000000) % 10;
long digitHundredThousands = (counter / 100000) % 10;
long digitTenThousands = (counter / 10000) % 10;
long digitThousands = (counter / 1000) % 10;
long digitHundreds = (counter / 100) % 10;
long digitTens = (counter / 10) % 10;
long digitOnes = counter % 10;
// Display each digit on its corresponding display
// digitOnes
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11111110);
shiftOut(dataPin, clockPin, LSBFIRST, segments[digitOnes]);
digitalWrite(latchPin, HIGH);
// Add a delay for persistence of vision
delayMicroseconds(10);
}
// digitTens
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11111101);
shiftOut(dataPin, clockPin, LSBFIRST, segments[digitTens]);
digitalWrite(latchPin, HIGH);
// Add a delay for persistence of vision
delayMicroseconds(10);
}
// digitHundreds
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11111011);
shiftOut(dataPin, clockPin, LSBFIRST, segments[digitHundreds]);
digitalWrite(latchPin, HIGH);
// Add a delay for persistence of vision
delayMicroseconds(10);
}
// digitThousands
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11110111);
shiftOut(dataPin, clockPin, LSBFIRST, segments[digitThousands]);
digitalWrite(latchPin, HIGH);
// Add a delay for persistence of vision
delayMicroseconds(10);
}
// digitTenThousands
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11101111);
shiftOut(dataPin, clockPin, LSBFIRST, segments[digitTenThousands]);
digitalWrite(latchPin, HIGH);
// Add a delay for persistence of vision
delayMicroseconds(10);
}
// digitHundredThousands
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11011111);
shiftOut(dataPin, clockPin, LSBFIRST, segments[digitHundredThousands]);
digitalWrite(latchPin, HIGH);
// Add a delay for persistence of vision
delayMicroseconds(10);
}
// digitMillions
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B10111111);
shiftOut(dataPin, clockPin, LSBFIRST, segments[digitMillions]);
digitalWrite(latchPin, HIGH);
// Add a delay for persistence of vision
delayMicroseconds(10);
}
// digitTenMillions
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B01111111);
shiftOut(dataPin, clockPin, LSBFIRST, segments[digitTenMillions]);
digitalWrite(latchPin, HIGH);
// Add a delay for persistence of vision
delayMicroseconds(10);
}
}
void incrementCounter()
{
counter = (counter + 1);
}
void decrementCounter()
{
counter = (counter - 1);
}