7seg

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.8 kB
3
Indexable
// Define the keypad layout
char key[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};

// Define output pins for columns
#define pin1 8
#define pin2 9
#define pin3 10

// Define input pins for rows
#define pin5 2
#define pin6 3
#define pin7 4

// Define input and output pins for keypad
int input_pins[3] = {2, 3, 4};
int output_pins[3] = {8, 9, 10};

// Variables to store key press and digit
char Pressed;
char digit;

// Segment pin definitions for 7-segment display
#define A 5
#define B 6
#define C 7
#define D 11
#define E 12
#define F 13
#define G A0

// Setup function, executed once on startup
void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Set up pins for keypad columns and rows
  for (int i = 0; i < 3; i++) {
    pinMode(output_pins[i], OUTPUT);
    digitalWrite(output_pins[i], HIGH);
    pinMode(input_pins[i], INPUT_PULLUP);
  }

  // Set up pins for 7-segment display segments
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);

  // Turn off all segments initially
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  digitalWrite(C, LOW);
  digitalWrite(D, LOW);
  digitalWrite(E, LOW);
  digitalWrite(F, LOW);
  digitalWrite(G, LOW);
}

// Function to detect keypad key press
char mykey(void) {
  int r, c;
  for (r = 0; r < 3; r++) {
    digitalWrite(output_pins[r], LOW);
    for (c = 0; c < 3; c++) {
      if (digitalRead(input_pins[c]) == LOW) {
        // Print debug information for button press detection
        Serial.print("Button pressed at row ");
        Serial.print(r);
        Serial.print(", column ");
        Serial.println(c);

        // Debounce by waiting for a short time
        delay(50);
        if (digitalRead(input_pins[c]) == LOW) {
          while (digitalRead(input_pins[c]) == LOW) {
            // Wait until the button is released
          }
          // Reset the row output and return the pressed key
          digitalWrite(output_pins[r], HIGH);
          return key[r][c];
        }
      }
    }
    // Reset the row output
    digitalWrite(output_pins[r], HIGH);
  }
  return '$'; // Return special character for no key press
}

// Main loop function
void loop() {
  Pressed = mykey(); // Call mykey function to get pressed key
  if (Pressed != '$') {
    // Display logic using segment pins
    switch (Pressed) {
      // Define segment pin states for each digit
      case '1':
        digitalWrite(A, HIGH);
        digitalWrite(B, LOW);
        digitalWrite(C, LOW);
        digitalWrite(D, HIGH);
        digitalWrite(E, HIGH);
        digitalWrite(F, HIGH);
        digitalWrite(G, HIGH);
        break;

      case '2':
        digitalWrite(A, LOW);
        digitalWrite(B, LOW);
        digitalWrite(C, HIGH);
        digitalWrite(D, LOW);
        digitalWrite(E, LOW);
        digitalWrite(F, HIGH);
        digitalWrite(G, LOW);
        break;

      case '3':
        digitalWrite(A, LOW);
        digitalWrite(B, LOW);
        digitalWrite(C, LOW);
        digitalWrite(D, LOW);
        digitalWrite(E, HIGH);
        digitalWrite(F, HIGH);
        digitalWrite(G, LOW);
        break;

      // Define cases for other digits

      default:
        // Turn off all segments for unhandled cases
        digitalWrite(A, LOW);
        digitalWrite(B, LOW);
        digitalWrite(C, LOW);
        digitalWrite(D, LOW);
        digitalWrite(E, LOW);
        digitalWrite(F, LOW);
        digitalWrite(G, LOW);
        break;
    }

    // Add delay to control display refresh rate
    delay(100);
  }
}