#include <Arduino.h>
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'C', '0', '='}
};
byte rowPins[ROWS] = {2, 4, 5, 16};
byte colPins[COLS] = {13, 14, 25};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter numbers:");
}
void loop() {
char key = keypad.getKey();
if (key) {
static int num1 = 0, num2 = 0, num3 = 0;
static int count = 0;
if (key == 'C') {
num1 = num2 = num3 = count = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter numbers:");
} else if (key == '=') {
int sum = num1 + num2 + num3;
float average = (float)sum / 3.0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sum: ");
lcd.print(sum);
lcd.setCursor(0, 1);
lcd.print("Avg: ");
lcd.print(average, 2);
} else {
if (count == 0)
num1 = num1 * 10 + (key - '0');
else if (count == 1)
num2 = num2 * 10 + (key - '0');
else if (count == 2)
num3 = num3 * 10 + (key - '0');
count = (count + 1) % 3;
lcd.setCursor(0, 1);
lcd.print(num1);
lcd.print(" ");
lcd.print(num2);
lcd.print(" ");
lcd.print(num3);
}
}
delay(100);
}