// Define pin numbers
const int segPins[] = { 7, 8, 9, 10, 11, 12, 13}; // Pins for segments A, B, C, D, E, F, G
const int digitPins[] = {5, 6}; // Pins for digit selection
const int switchPins[] = {A1, A2, A3, A4}; // Pins for dip switches
// Define 7-segment display patterns for digits 0-9 (only segments A-G)
const int digits[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
};
void setup() {
// Initialize the dip switch pins as inputs
for (int i = 0; i < 4; ++i) {
pinMode(switchPins[i], INPUT_PULLUP);
}
// Initialize the 7-segment display pins as outputs
for (int i = 0; i < 7; ++i) {
pinMode(segPins[i], OUTPUT);
}
for (int i = 0; i < 2; ++i) {
pinMode(digitPins[i], OUTPUT);
}
}
void loop() {
// Read the states of the dip switches
int switchStates[4];
for (int i = 0; i < 4; ++i) {
switchStates[i] = digitalRead(switchPins[i]);
}
// Check if the first two switches are active
if (switchStates[0] == LOW && switchStates[1] == LOW) {
// Counting for digit 1
countAndDisplay(0);
} else if (switchStates[0] == HIGH && switchStates[1] == HIGH) {
// Counting for digit 2
countAndDisplay(1);
}
// Check if the next two switches are active
if (switchStates[2] == LOW && switchStates[3] == LOW) {
// Counting for digit 1
countAndDisplay(0);
} else if (switchStates[2] == HIGH && switchStates[3] == HIGH) {
// Counting for digit 2
countAndDisplay(1);
}
}
// Function to count and display on the selected digit
void countAndDisplay(int digitIndex) {
static int count = 0; // Static variable to hold the count value
displayDigit(count, digitIndex); // Display the current count on the selected digit
count = (count + 1) % 10; // Increment count and wrap around at 10
delay(500); // Delay for visual effect, adjust as needed
}
// Function to display a digit on a 7-segment display
void displayDigit(int digit, int digitIndex) {
// Turn off all digits
for (int i = 0; i < 2; ++i) {
digitalWrite(digitPins[i], HIGH);
}
// Select the desired digit
digitalWrite(digitPins[digitIndex], LOW);
// Display only segments A-G for the digit
for (int i = 0; i < 7; ++i) {
digitalWrite(segPins[i], digits[digit][i]);
}
}
Tidak ada komentar:
Posting Komentar