#include <LiquidCrystal.h>
// Pins for 7-segment display
const int segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
// Pins for push buttons
const int buttonPin1 = 10;
const int buttonPin2 = 11;
// Pins for LCD
const int rs = 12, en = 13, d4 = A0, d5 = A1, d6 = A2, d7 = A3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int count = 0;
void setup() {
// Initialize LCD
lcd.begin(16, 2);
// Initialize push buttons
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
// Initialize 7-segment display pins
for (int i = 0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Display initial count
updateDisplays();
}
void loop() {
// Check if button 1 is pressed
if (digitalRead(buttonPin1) == LOW) {
count++;
if (count > 14) { // E in hexadecimal
count = 0;
}
updateDisplays();
delay(200); // Debouncing delay
}
// Check if button 2 is pressed
if (digitalRead(buttonPin2) == LOW) {
count--;
if (count < 0) {
count = 14; // E in hexadecimal
}
updateDisplays();
delay(200); // Debouncing delay
}
}
void updateDisplays() {
// Update 7-segment display
for (int i = 0; i < 8; i++) {
if (shouldLight(i, count)) {
digitalWrite(segmentPins[i], HIGH);
} else {
digitalWrite(segmentPins[i], LOW);
}
}
// Update LCD display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Count: ");
lcd.print(count, HEX);
}
bool shouldLight(int segment, int number) {
// Define the segments needed to display each digit in hexadecimal
const int segments[16][8] = {
{0, 0, 0, 0, 0, 0, 1, 1}, // 0
{1, 0, 0, 1, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0, 1}, // 2
{0, 0, 0, 0, 1, 1, 0, 1}, // 3
{1, 0, 0, 1, 1, 0, 0, 1}, // 4
{0, 1, 0, 0, 1, 0, 0, 1}, // 5
{0, 1, 0, 0, 0, 0, 0, 1}, // 6
{0, 0, 0, 1, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0, 1}, // 8
{0, 0, 0, 0, 1, 0, 0, 1}, // 9
{0, 0, 0, 1, 0, 0, 0, 1}, // A
{1, 1, 0, 0, 0, 0, 0, 1}, // b
{0, 1, 1, 0, 0, 0, 1, 1}, // C
{1, 0, 0, 0, 0, 1, 0, 1}, // d
{0, 1, 1, 0, 0, 0, 0, 1}, // E
{0, 1, 1, 1, 0, 0, 0, 1} // F
};
// Check if the specified segment should be lit for the given number
return segments[number][segment];
}