#include #include #include // Initialize the LCD with the I2C address (usually 0x27) LiquidCrystal_I2C lcd(0x27, 16, 2); // Define the keymap for the keypad const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = { {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; // Define the connections to the rows and columns for DIYables Keypad byte rowPins[ROWS] = {9, 8, 7, 6}; byte colPins[COLS] = {5, 4, 3, 2}; // Create the keypad object Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); const int buttonPin = 10; // Pin for mode toggle button bool mode = false; // false for length, true for weight String input = ""; // Store input from keypad char fromUnit = ' '; // Store the unit to convert from char toUnit = ' '; // Store the unit to convert to unsigned long lastActivityTime = 0; // To track inactivity const unsigned long timeout = 60000; // 1 minute timeout void setup() { lcd.init(); // Initialize the LCD lcd.backlight(); // Turn on the backlight lcd.print("Enter No."); pinMode(buttonPin, INPUT_PULLUP); // Initialize button pin as input with internal pull-up resistor } void loop() { if (digitalRead(buttonPin) == LOW) { mode = !mode; lcd.backlight(); // Turn on the backlight lastActivityTime = millis(); // Reset the inactivity timer if (mode) { lcd.clear(); lcd.print("Enter Wt."); } else { lcd.clear(); lcd.print("Enter No."); } delay(300); // Debounce delay } char key = keypad.getKey(); if (key) { lastActivityTime = millis(); // Reset the inactivity timer lcd.backlight(); // Turn on the backlight when a key is pressed if (key == 'A' || key == 'B' || key == 'C' || key == 'D') { if (input == "") { // If no input, set the conversion unit if (fromUnit == ' ') { fromUnit = key; lcd.clear(); lcd.print("From unit:"); lcd.setCursor(0, 1); lcd.print(getUnitName(fromUnit)); } else { // Convert value if both units are set toUnit = key; performConversion(); } } else { // If input exists, set the unit and display the result if (fromUnit == ' ') { fromUnit = key; lcd.clear(); lcd.print("From unit:"); lcd.setCursor(0, 1); lcd.print(getUnitName(fromUnit)); } else { toUnit = key; performConversion(); } } } else if (key == '#') { // Clear input and reset units input = ""; fromUnit = ' '; toUnit = ' '; lcd.clear(); lcd.print(mode ? "Enter Wt." : "Enter No."); } else { // Append key to input if (key == '*') { key = '.'; // Replace '*' with '.' } if (input.length() < 16) { // Ensure input fits on the LCD input += key; lcd.setCursor(0, 0); // Start at the beginning of the first line lcd.print(mode ? "Enter Wt: " : "Enter No: "); lcd.print(input); lcd.print(" "); // Add a space to avoid overwriting } } } // Check for inactivity timeout if (millis() - lastActivityTime > timeout) { lcd.noBacklight(); // Turn off the backlight input = ""; fromUnit = ' '; toUnit = ' '; lcd.clear(); lcd.print(mode ? "Enter Wt." : "Enter No."); lastActivityTime = millis(); // Reset the inactivity timer to prevent repeated clearing } } String getUnitName(char unit) { if (mode) { switch (unit) { case 'A': return "lbs"; case 'B': return "kg"; case 'C': return "grams"; case 'D': return "mg"; default: return ""; } } else { switch (unit) { case 'A': return "inches"; case 'B': return "mm"; case 'C': return "cm"; case 'D': return "m"; default: return ""; } } } void performConversion() { float value = input.toFloat(); float result = 0; String resultUnit; // Perform the unit conversion based on the selected units if (mode) { // Weight mode if (fromUnit == 'A') { // Pounds if (toUnit == 'B') { result = value * 0.453592; resultUnit = "kg"; } else if (toUnit == 'C') { result = value * 453.592; resultUnit = "grams"; } else if (toUnit == 'D') { result = value * 453592; resultUnit = "mg"; } } else if (fromUnit == 'B') { // Kilograms if (toUnit == 'A') { result = value / 0.453592; resultUnit = "lbs"; } else if (toUnit == 'C') { result = value * 1000.0; resultUnit = "grams"; } else if (toUnit == 'D') { result = value * 1000000.0; resultUnit = "mg"; } } else if (fromUnit == 'C') { // Grams if (toUnit == 'A') { result = value / 453.592; resultUnit = "lbs"; } else if (toUnit == 'B') { result = value / 1000.0; resultUnit = "kg"; } else if (toUnit == 'D') { result = value * 1000.0; resultUnit = "mg"; } } else if (fromUnit == 'D') { // Milligrams if (toUnit == 'A') { result = value / 453592; resultUnit = "lbs"; } else if (toUnit == 'B') { result = value / 1000000.0; resultUnit = "kg"; } else if (toUnit == 'C') { result = value / 1000.0; resultUnit = "grams"; } } } else { // Length mode if (fromUnit == 'A') { // Inches if (toUnit == 'B') { result = value * 25.4; resultUnit = "mm"; } else if (toUnit == 'C') { result = value * 2.54; resultUnit = "cm"; } else if (toUnit == 'D') { result = value * 0.0254; resultUnit = "m"; } } else if (fromUnit == 'B') { // Millimeters if (toUnit == 'A') { result = value / 25.4; resultUnit = "inches"; } else if (toUnit == 'C') { result = value / 10.0; resultUnit = "cm"; } else if (toUnit == 'D') { result = value / 1000.0; resultUnit = "m"; } } else if (fromUnit == 'C') { // Centimeters if (toUnit == 'A') { result = value / 2.54; resultUnit = "inches"; } else if (toUnit == 'B') { result = value * 10.0; resultUnit = "mm"; } else if (toUnit == 'D') { result = value / 100.0; resultUnit = "m"; } } else if (fromUnit == 'D') { // Meters if (toUnit == 'A') { result = value * 39.3701; resultUnit = "inches"; } else if (toUnit == 'B') { result = value * 1000.0; resultUnit = "mm"; } else if (toUnit == 'C') { result = value * 100.0; resultUnit = "cm"; } } } // Display results on LCD lcd.clear(); lcd.setCursor(0, 0); lcd.print("Value "); lcd.print(input); lcd.print(" "); lcd.print(getUnitName(fromUnit)); lcd.setCursor(0, 1); lcd.print("Equal "); // Removed the colon here lcd.print(result); lcd.print(" "); lcd.print(getUnitAbbreviation(resultUnit)); // Reset input and units input = ""; fromUnit = ' '; toUnit = ' '; } String getUnitAbbreviation(String unit) { if (unit == "inches") return "in"; if (unit == "mm") return "mm"; if (unit == "cm") return "cm"; if (unit == "m") return "m"; if (unit == "lbs") return "lbs"; if (unit == "kg") return "kg"; if (unit == "grams") return "g"; if (unit == "mg") return "mg"; return ""; }