#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <math.h>
// Initialize the LCD. Ensure the correct I2C address is set
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust the address if needed
// Keypad configuration
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'C'},
{'4', '5', '6', 'D'},
{'7', '8', '9', 'E'},
{'*', '0', '#', 'F'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Keypad row pins
byte colPins[COLS] = {5, 4, 3, 2}; // Keypad column pins
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Variables for calculations
float r = 0.0, h = 0.0, k = 0.0, x = 0.0, y = 0.0;
bool rIsProvided = false, centerIsProvided = false;
void setup() {
lcd.begin(20, 4); // Initialize LCD with 20x4 dimensions
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("1-r 2-C 3-SF");
lcd.setCursor(0, 1);
lcd.print("4-GF 5-QS 6-AS");
lcd.setCursor(0, 2);
lcd.print("7-GS 8-HS C-Back");
char key = keypad.getKey();
if (key) {
switch (key) {
case '1': getRadius(); break;
case '2': getCenter(); break;
case '3': calculateStandard(); break;
case '4': calculateGeneral(); break;
case '5': calculateQuadratic(); break;
case '6': calculateArithmetic(); break;
case '7': calculateGeometric(); break;
case '8': calculateHarmonic(); break;
case 'C': clearInputs(); break;
}
}
}
// Function to get radius
void getRadius() {
x = getCoordinate("x");
y = getCoordinate("y");
h = getCoordinate("h");
k = getCoordinate("k");
// Calculate radius using the distance formula
r = sqrt(sq(x - h) + sq(y - k));
lcd.clear();
lcd.print("Radius (r): ");
lcd.setCursor(0, 1);
lcd.print(r);
lcd.setCursor(0, 2);
lcd.print("Center (h,k): ");
lcd.setCursor(0, 3);
lcd.print("(");
lcd.print(h);
lcd.print(", ");
lcd.print(k);
lcd.print(")");
delay(5000); // Display result for 5 seconds
lcd.clear();
}
// Function to get center coordinates
void getCenter() {
h = getCoordinate("h");
k = getCoordinate("k");
centerIsProvided = true;
lcd.clear();
lcd.print("Center set:");
lcd.setCursor(0, 1);
lcd.print("(");
lcd.print(h);
lcd.print(", ");
lcd.print(k);
lcd.print(")");
delay(2000);
lcd.clear();
}
// Function to input a coordinate
float getCoordinate(const char* coordName) {
lcd.clear();
lcd.print("Enter ");
lcd.print(coordName);
lcd.print(":");
String input = "";
while (true) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
input += key;
lcd.setCursor(0, 1);
lcd.print(input);
} else if (key == '*') {
return input.toFloat();
}
}
}
// Standard Form Calculation
void calculateStandard() {
lcd.clear();
lcd.print("Standard calculation");
// Add standard form calculations here
delay(2000);
lcd.clear();
}
// Arithmetic Sequence Function
void calculateArithmetic() {
lcd.clear();
lcd.print("Enter a1, d, n:");
float a1 = getCoordinate("a1");
float d = getCoordinate("d");
int n = (int)getCoordinate("n");
float an = a1 + (n - 1) * d; // nth term
float sum = (n / 2.0) * (2 * a1 + (n - 1) * d); // Sum of sequence
lcd.clear();
lcd.print("an = ");
lcd.print(an);
delay(2000);
lcd.clear();
lcd.print("Sum = ");
lcd.print(sum);
delay(2000);
lcd.clear();
}
// Utility to convert decimal to fraction if integer
String decimalToFraction(float num) {
if (num == (int)num) return String((int)num); // Integer case
int denom = 1;
while (num != (int)num && denom < 10000) {
num *= 10;
denom *= 10;
}
int gcdValue = gcd((int)num, denom);
return String((int)num / gcdValue) + "/" + String(denom / gcdValue);
}
// General Form Placeholder
void calculateGeneral() { displayMessage("General form"); }
// Quadratic Form Placeholder
void calculateQuadratic() { displayMessage("Quadratic seq"); }
// Geometric Sequence Placeholder
void calculateGeometric() { displayMessage("Geometric seq"); }
// Harmonic Sequence Placeholder
void calculateHarmonic() { displayMessage("Harmonic seq"); }
// Function to clear inputs
void clearInputs() {
r = h = k = 0.0;
rIsProvided = centerIsProvided = false;
lcd.clear();
lcd.print("Inputs cleared");
delay(1500);
lcd.clear();
}
// Utility function to display a message
void displayMessage(const char* message) {
lcd.clear();
lcd.print(message);
delay(2000);
lcd.clear();
}
// GCD Helper Function
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}