0% found this document useful (0 votes)
11 views7 pages

Code Explanation

The document outlines a program that uses a 4x4 keypad to control a 4-digit counter displayed via BCD outputs. It details the setup of the keypad, counter variables, and functions for handling key presses, updating the counter, and displaying values. Additionally, it includes features for buzzer alerts and resetting the counter, ensuring smooth user interaction.

Uploaded by

s2022100596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Code Explanation

The document outlines a program that uses a 4x4 keypad to control a 4-digit counter displayed via BCD outputs. It details the setup of the keypad, counter variables, and functions for handling key presses, updating the counter, and displaying values. Additionally, it includes features for buzzer alerts and resetting the counter, ensuring smooth user interaction.

Uploaded by

s2022100596
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Libraries and Pin Configuration

#include <Keypad.h>

• Includes the Keypad library to handle user input from a 4x4 matrix keypad.

const int bcdA[4] = {22, 24, 26, 28};

const int bcdB[4] = {30, 32, 34, 36};

const int bcdC[4] = {38, 40, 42, 44};

const int bcdD[4] = {46, 48, 50, 52};

• Defines the BCD output pins for a 4-digit 7-segment display.

• Each array represents one digit (A, B, C, and D).

const int buzzerPin = 10;

• Defines the buzzer pin.

2. Keypad Setup

const byte ROWS = 4;

const byte COLS = 4;

• Defines the dimensions of the 4x4 keypad.

char keys[ROWS][COLS] = {

{'1','2','3','A'},

{'4','5','6','B'},

{'7','8','9','C'},

{'*','0','#','D'}

};

• Defines the key mapping for the keypad.

const byte rowPins[ROWS] = {23, 25, 27, 29};

const byte colPins[COLS] = {31, 33, 35, 37};

• Assigns Arduino pins to the keypad rows and columns.

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

• Creates a Keypad object.


3. Counter Variables

int counterValueInt = 0;

String counterValueStr = "";

• counterValueInt: Stores the current counter value as an integer.

• counterValueStr: Stores the counter as a string (useful for digit-by-digit input).

unsigned long previousMillis = 0;

const long interval = 1000; // 1 second interval

• Implements a millis() timer for counting every second.

bool isCounterRunning = false;

bool isCountingUp = false;

bool isReset = true;

• isCounterRunning: Tracks if the counter is active.

• isCountingUp: Tracks if the counter is increasing (true) or decreasing (false).

• isReset: Tracks whether the counter has been reset.

4. Setup Function

void setup() {

Serial.begin(9600);

• Starts serial communication for debugging.

for (int i = 0; i < 4; i++) {

pinMode(bcdA[i], OUTPUT);

pinMode(bcdB[i], OUTPUT);

pinMode(bcdC[i], OUTPUT);

pinMode(bcdD[i], OUTPUT);

• Sets BCD pins as outputs.

pinMode(buzzerPin, OUTPUT);

keypad.addEventListener(keypadEvent);

keypad.setDebounceTime(10);
• Configures the buzzer and keypad.

displayCounter();

• Initializes the counter display.

5. Main Loop (Handling Keypad & Timer)

void loop() {

char button = keypad.getKey();

• Reads a keypad button press.

if (button) {

Serial.println(button);

• Prints the pressed button to Serial Monitor.

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {

previousMillis = currentMillis;

• Implements a non-blocking timer (instead of delay()).

if (isCounterRunning) {

if (isCountingUp) {

if (counterValueInt == 9999) {

counterValueInt = 0;

displayCounter();

resetCounter();

} else {

counterValueInt++;

} else {
if (counterValueInt == 0){

counterValueInt = 0;

displayCounter();

resetCounter();

} else {

counterValueInt--;

displayCounter();

• If counting is active, it increments/decrements counterValueInt every second.

• If counting reaches 9999, it resets to 0.

• If counting reaches 0, it stays at 0.

6. Keypad Event Handling

void keypadEvent(KeypadEvent key) {

if (keypad.getState() == PRESSED) {

tone(buzzerPin, 1100, 10);

• Plays a short beep when a key is pressed.

if (key >= '0' && key <= '9') {

if (counterValueStr.length() == 4) {

counterValueStr = counterValueStr.substring(1) + key;

} else {

counterValueStr += key;

counterValueInt = counterValueStr.toInt();

displayCounter();
}

• Handles number input: If 4 digits are entered, it removes the first and adds the new.

if (key == '*') {

if (isCounterRunning) return;

isCounterRunning = true;

displayCounter();

• * Starts the counter.

if (key == '#') {

isCounterRunning = false;

displayCounter();

• # Stops the counter.

if (key == 'A') isCountingUp = true;

if (key == 'B') isCountingUp = false;

• A sets count-up mode.

• B sets count-down mode.

if (key == 'C' && !isCounterRunning) {

counterValueInt++;

isCountingUp = true;

displayCounter();

if (key == 'D' && !isCounterRunning) {

counterValueInt--;

isCountingUp = false;

displayCounter();

• C and D allow manual increments/decrements when the counter is stopped.


7. Displaying the Counter on 7-Segment BCD

void displayCounter() {

int ones = counterValueInt % 10;

int tens = (counterValueInt / 10) % 10;

int hundreds = (counterValueInt / 100) % 10;

int thousands = counterValueInt / 1000;

• Extracts individual digits for BCD representation.

for (int i = 0; i < 4; i++) {

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);

• Uses bitwise operations to send BCD signals to display each digit.

8. Buzzer Alerts

if (counterValueInt == 5 && !isCountingUp && isCounterRunning) {

tone(buzzerPin, 2000, 200);

• Sounds a buzzer alert when countdown reaches critical numbers (5, 4, 3, 2, 1, 0).

• Similarly, it beeps at high values (9995-9999) while counting up.

9. Resetting the Counter

void resetCounter() {

counterValueInt = 0;

counterValueStr = "";

isCounterRunning = false;

isCountingUp = false;
isReset = true;

displayCounter();

delay(1000);

• Resets everything when the counter finishes.

Summary

1. The code uses a 4x4 keypad to control a 4-digit counter, which is displayed using BCD (Binary-
Coded Decimal) outputs.

2. It defines the keypad layout, assigns row and column pins, and initializes the Keypad library for
detecting key presses.

3. The counter's value is stored as both an integer (counterValueInt) and a string (counterValueStr)
for display handling.

4. The setup() function configures the BCD output pins, the buzzer pin, and initializes the display.

5. The loop() function continuously checks for keypresses and updates the counter every second if
counting is active.

6. The keypad's keypadEvent() function handles key presses to set or modify the counter value,
start/stop counting, or change counting direction.

7. The displayCounter() function converts the counter value into BCD format and updates the
display accordingly.

8. The buzzer provides sound feedback when the counter reaches specific values, especially near
zero in countdown mode.

9. The resetCounter() function resets the counter, stops counting, and clears the display when
needed.

10. The program ensures smooth user interaction by implementing debounce timing and updating
the display in real time.

You might also like