Advertisement
Hodge-1053

Arduino: Keypad with 7-Segment Display

Oct 29th, 2022 (edited)
1,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | Source Code | 0 0
  1. #include <Keypad.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27, 20, 4);
  5.  
  6. const int a = 23;
  7. const int b = 25;
  8. const int c = 27;
  9. const int d = 29;
  10. const int e = 31;
  11. const int f = 33;
  12. const int g = 35;
  13. const int p = 37;
  14.  
  15. const int Rows = 4;
  16. const int Cols = 4;
  17.  
  18. char keys [Rows][Cols] =
  19. {
  20.   {'1', '2', '3', 'A'},
  21.   {'4', '5', '6', 'B'},
  22.   {'7', '8', '9', 'C'},
  23.   {'*', '0', '#', 'D'}
  24. };
  25.  
  26. byte rowPins [Rows] = {1,2,3,4};
  27. byte colPins [Cols] = {5,6,7,8};
  28.  
  29. Keypad keypad = Keypad (makeKeymap(keys), rowPins, colPins, Rows, Cols);
  30.  
  31. void setup()
  32. {
  33.   lcd.init();
  34.   lcd.backlight();
  35.   lcd.clear();
  36.   lcd.setCursor(0, 0);
  37.  
  38.   pinMode(p, OUTPUT);
  39.   pinMode(a, OUTPUT);
  40.   pinMode(a, OUTPUT);
  41.   pinMode(b, OUTPUT);
  42.   pinMode(c, OUTPUT);
  43.   pinMode(d, OUTPUT);
  44.   pinMode(e, OUTPUT);
  45.   pinMode(f, OUTPUT);
  46.   pinMode(g, OUTPUT);
  47. }
  48.  
  49. void keyPress()
  50. {
  51.   char key = keypad.getKey();
  52.   if (key)
  53.   {
  54.     lcd.clear();
  55.     lcd.print("U Pressed: ");
  56.     lcd.print(key);
  57.   }
  58.  
  59.   if (key == '1')
  60.   {
  61.     digitalWrite(a, HIGH);
  62.   }
  63.  
  64.   else if (key == '2')
  65.   {
  66.     digitalWrite(b, HIGH);
  67.   }
  68.  
  69.   else if (key == '3')
  70.   {
  71.     digitalWrite(c, HIGH);
  72.   }
  73.  
  74.   else if (key == '4')
  75.   {
  76.     digitalWrite(d, HIGH);
  77.   }
  78.  
  79.   else if (key == '5')
  80.   {
  81.     digitalWrite(e, HIGH);
  82.   }
  83.  
  84.   else if (key == '6')
  85.   {
  86.     digitalWrite(f, HIGH);
  87.   }
  88.  
  89.   else if (key == '7')
  90.   {
  91.     digitalWrite(g, HIGH);
  92.   }
  93.  
  94.   else if (key == '8')
  95.   {
  96.     digitalWrite(p, HIGH);
  97.   }
  98. }
  99. void loop()
  100. {
  101.   keyPress();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement