#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD with I2C address 0x27
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Button Pin Definitions
#define BUTTON_UP 26
#define BUTTON_DOWN 27
#define BUTTON_LEFT 28
#define BUTTON_RIGHT 29
#define BUTTON_ENTER 30
// Variables
int currentIndex = 0; // Current variable index
int currentDigit = 0; // Active digit (0 = first, 1 = second)
int values[4] = {0, 0, 0, 0}; // Array for Length, Width, Rotational Range, Distance
int inputDigits[2] = {0, 0}; // Temporary input digits for active variable
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize Buttons
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(BUTTON_LEFT, INPUT_PULLUP);
pinMode(BUTTON_RIGHT, INPUT_PULLUP);
pinMode(BUTTON_ENTER, INPUT_PULLUP);
// Load Initial Value
loadCurrentValue();
updateLCD();
}
void loop() {
// Increment Current Digit
if (digitalRead(BUTTON_UP) == LOW) {
inputDigits[currentDigit] = (inputDigits[currentDigit] + 1) % 10;
delay(200); // Debounce
updateLCD();
}
// Decrement Current Digit
if (digitalRead(BUTTON_DOWN) == LOW) {
inputDigits[currentDigit] = (inputDigits[currentDigit] - 1 + 10) % 10;
delay(200); // Debounce
updateLCD();
}
// Move to Previous Variable
if (digitalRead(BUTTON_LEFT) == LOW) {
saveCurrentValue();
currentIndex = (currentIndex - 1 + 4) % 4; // Wrap around
loadCurrentValue();
delay(200); // Debounce
updateLCD();
}
// Move to Next Variable
if (digitalRead(BUTTON_RIGHT) == LOW) {
saveCurrentValue();
currentIndex = (currentIndex + 1) % 4; // Wrap around
loadCurrentValue();
delay(200); // Debounce
updateLCD();
}
// Switch Digits or Save Input
if (digitalRead(BUTTON_ENTER) == LOW) {
if (currentDigit == 0) {
currentDigit = 1; // Move to the second digit
} else {
saveCurrentValue(); // Save the input value
currentDigit = 0; // Reset to the first digit
currentIndex = (currentIndex + 1) % 4; // Move to next variable
loadCurrentValue(); // Load new variable's value
}
delay(200); // Debounce
updateLCD();
}
}
// Save the input value to the values array
void saveCurrentValue() {
values[currentIndex] = inputDigits[0] * 10 + inputDigits[1];
}
// Load the value of the current variable into the input digits
void loadCurrentValue() {
inputDigits[0] = values[currentIndex] / 10; // Extract first digit
inputDigits[1] = values[currentIndex] % 10; // Extract second digit
}
// Update the LCD Display
void updateLCD() {
lcd.clear();
// Display Variables
lcd.setCursor(0, 0);
lcd.print("Length: ");
lcd.print(values[0]);
lcd.setCursor(0, 1);
lcd.print("Width: ");
lcd.print(values[1]);
lcd.setCursor(0, 2);
lcd.print("Rotation: ");
lcd.print(values[2]);
lcd.setCursor(0, 3);
lcd.print("Distance: ");
lcd.print(values[3]);
// Highlight Current Variable and Show Input
lcd.setCursor(12, currentIndex); // Position for the input
lcd.print("<");
lcd.print(inputDigits[0]);
lcd.print(inputDigits[1]);
lcd.print(">");
// Highlight Active Digit
if (currentDigit == 0) {
lcd.setCursor(13, currentIndex); // Highlight first digit
} else {
lcd.setCursor(14, currentIndex); // Highlight second digit
}
lcd.blink();
}