#include <Keypad.h>
// BCD IC A (ones digit)
const int bcdA[4] = {22, 24, 26, 28};
// BCD IC B (tens digit)
const int bcdB[4] = {30, 32, 34, 36};
// BCD IC C (hundreds digit)
const int bcdC[4] = {38, 40, 42, 44};
// BCD IC D (thousands digit)
const int bcdD[4] = {46, 48, 50, 52};
// Buzzer
const int buzzerPin = 10;
// Keypad
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
const byte rowPins[ROWS] = {23, 25, 27, 29};
const byte colPins[COLS] = {31, 33, 35, 37};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Global Variables
int counterValueInt = 0; // counter value in integer data type
String counterValueStr = ""; // counter value in string data type
unsigned long previousMillis = 0; // Variable to store the previous time
const long interval = 1000; // Interval in milliseconds (1 second)
bool countDown = false; // Indicator for countdown mode
void setup()
{
for (int i = 0; i < 4; i++)
{
// BCD IC A (ones digit)
pinMode(bcdA[i], OUTPUT);
// BCD IC B (tens digit)
pinMode(bcdB[i], OUTPUT);
// BCD IC C (hundreds digit)
pinMode(bcdC[i], OUTPUT);
// BCD IC D (thousands digit)
pinMode(bcdD[i], OUTPUT);
}
// Buzzer Pin
pinMode(buzzerPin, OUTPUT);
// Keypad Properties
keypad.setDebounceTime(10);
displayCounter();
}
void loop()
{
// Get key pressed
char key = keypad.getKey();
if (key != NO_KEY)
{
// If a key is pressed
if (key >= '0' && key <= '9')
{
// If the key is a digit (0-9), append it to the counter value string
counterValueStr += key;
counterValueInt = counterValueStr.toInt(); // Update the integer value
displayCounter(); // Display immediately after key press
}
else if (key == '*')
{
// If the '*' key is pressed, start or toggle counting down
countDown = !countDown;
}
else if (key == '#')
{
// If the '#' key is pressed, clear the counter value string
counterValueStr = "";
counterValueInt = 0; // Reset the integer value
displayCounter();
}
else if (key == 'A')
{
// If the 'A' key is pressed, perform count-up function if counter is running
if (!countDown)
{
counterValueInt++;
counterValueStr = String(counterValueInt);
displayCounter();
}
}
else if (key == 'B')
{
// If the 'B' key is pressed, perform count-down function if counter is running
if (!countDown && counterValueInt > 0)
{
counterValueInt--;
counterValueStr = String(counterValueInt);
displayCounter();
}
}
else if (key == 'C')
{
// If the 'C' key is pressed, increment counter value by 1 and set to count-up function if counter is not running
if (countDown)
{
counterValueInt++;
counterValueStr = String(counterValueInt);
countDown = false; // Set to count-up function
displayCounter();
}
}
else if (key == 'D')
{
// If the 'D' key is pressed, decrement counter value by 1 and set to count-down function if counter is not running
if (countDown)
{
counterValueInt--;
counterValueStr = String(counterValueInt);
countDown = true; // Set to count-down function
displayCounter();
}
}
}
// If counting down and counter value is greater than 0, decrement it
if (countDown && counterValueInt > 0)
{
counterValueInt--;
counterValueStr = String(counterValueInt);
displayCounter();
}
unsigned long currentMillis = millis(); // Get the current time
// Check if one second has elapsed
if (currentMillis - previousMillis >= interval)
{
// Save the current time for the next iteration
previousMillis = currentMillis;
// Execute code here that you want to run every one second
}
}
void displayCounter()
{
// get each digit in different place
int ones = counterValueInt % 10;
int tens = (counterValueInt / 10) % 10;
int hundreds = (counterValueInt / 100) % 10;
int thousands = counterValueInt / 1000;
for (int i = 0; i < 4; i++)
{
// No display if counter value is empty
if (counterValueStr.length() == 0)
{
digitalWrite(bcdA[i], HIGH);
digitalWrite(bcdB[i], HIGH);
digitalWrite(bcdC[i], HIGH);
digitalWrite(bcdD[i], HIGH);
}
// Display if counter value has 1 digit
if (counterValueStr.length() == 1)
{
digitalWrite(bcdA[i], (ones & (1 << i)) ? HIGH : LOW);
digitalWrite(bcdB[i], HIGH);
digitalWrite(bcdC[i], HIGH);
digitalWrite(bcdD[i], HIGH);
}
// Display if counter value has 2 digits
if (counterValueStr.length() == 2)
{
digitalWrite(bcdA[i], (ones & (1 << i)) ? HIGH : LOW);
digitalWrite(bcdB[i], (tens & (1 << i)) ? HIGH : LOW);
digitalWrite(bcdC[i], HIGH);
digitalWrite(bcdD[i], HIGH);
}
// Display if counter value has 3 digits
if (counterValueStr.length() == 3)
{
digitalWrite(bcdA[i], (ones & (1 << i)) ? HIGH : LOW);
digitalWrite(bcdB[i], (tens & (1 << i)) ? HIGH : LOW);
digitalWrite(bcdC[i], (hundreds & (1 << i)) ? HIGH : LOW);
digitalWrite(bcdD[i], HIGH);
}
// Display if counter value has 4 digits
if (counterValueStr.length() == 4)
{
digitalWrite(bcdA[i], (ones & (1 << i)) ? HIGH : LOW);
digitalWrite(bcdB[i], (tens & (1 << i)) ? HIGH : LOW);
digitalWrite(bcdC[i], (hundreds & (1 << i)) ? HIGH : LOW);
digitalWrite(bcdD[i], (thousands & (1 << i)) ? HIGH : LOW);
}
}
}