#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <math.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','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() {
Wire.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter c (cm):");
}
void loop() {
char key = keypad.getKey();
if (key) {
static char buffer[10];
static int index = 0;
if (key == '#') {
buffer[index] = '\0';
float circumference = atof(buffer);
float radius = circumference / (2 * M_PI);
float area = M_PI * pow(radius, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Area: ");
lcd.setCursor(6, 0);
lcd.print(area);
index = 0;
} else {
buffer[index++] = key;
lcd.setCursor(index - 1, 1);
lcd.print(key);
}
}
}