#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define ROW_NUM 4
#define COLUMN_NUM 4
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {12, 14, 27, 26};
byte pin_column[COLUMN_NUM] = {25, 33, 32, 35};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
int values[3] = {0, 0, 0};
int current_value = 0;
bool inputting = false;
bool calculate_sum = false;
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
display.setCursor(0, 0);
display.println("Press * to start");
display.display();
}
void loop() {
char key = keypad.getKey();
if (key == '*') {
inputting = true;
display.clearDisplay();
display.setCursor(0, 0);
display.println("Enter 3 values:");
display.display();
}
if (inputting && !calculate_sum) {
if (key) {
if (key >= '0' && key <= '9') {
values[current_value] = values[current_value] * 10 + (key - '0');
display.clearDisplay();
display.setCursor(0, 0);
display.print("Value ");
display.print(current_value + 1);
display.print(": ");
display.println(values[current_value]);
display.display();
if (values[current_value] >= 10) {
current_value++;
if (current_value == 3) {
calculate_sum = true;
}
}
}
}
} else {
if (calculate_sum) {
int sum = values[0] + values[1] + values[2];
display.clearDisplay();
display.setCursor(0, 0);
display.print("Sum: ");
display.println(sum);
display.display();
delay(5000); // Display sum for 5 seconds
current_value = 0;
for (int i = 0; i < 3; i++) {
values[i] = 0;
}
calculate_sum = false;
inputting = false;
}
}
}