#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Define the number of rows and columns for the keypad
const byte ROWS = 4;
const byte COLS = 4;
// Define the symbols on the buttons of the keypad
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
// Connect keypad ROW0, ROW1, ROW2, ROW3 to these Arduino pins
byte rowPins[ROWS] = {9, 8, 7, 6};
// Connect keypad COL0, COL1, COL2, COL3 to these Arduino pins
byte colPins[COLS] = {5, 4, 3, 2};
// Create the Keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Create the LCD object (using I2C address 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 4);
// LED pins
const int ledPins[8] = {10, 11, 12, 13, 14, 15, 16, 17};
// Command and state variables
String command = "";
String currentA = "";
String currentB = "";
String currentC = "";
bool ledState[8] = {false, false, false, false, false, false, false, false};
bool inCMode = false;
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
// Initialize the LED pins as outputs
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
// Display initial prompt on the LCD
lcd.setCursor(0, 0);
lcd.print("A: B: ");
lcd.setCursor(0, 1);
lcd.print(">");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '*') {
// Clear input, retain last A, B, and C values
command = "";
lcd.setCursor(1, 1);
lcd.print(" "); // Clear the command area
lcd.setCursor(1, 1);
} else if (key == '#') {
// Refresh LCD
refreshLCD();
} else if (key == 'D') {
// Execute command
lcd.setCursor(0, 2);
lcd.print("Executing...");
executeCommand();
delay(1000); // Delay to display "Executing..." message
lcd.setCursor(0, 2);
lcd.print(" "); // Clear "Executing..." message
} else {
// Append key to the command
if (command.length() < 8) {
command += key;
lcd.setCursor(1, 1);
lcd.print(command);
updateLCD();
}
}
}
}
void refreshLCD() {
lcd.clear();
if (inCMode) {
lcd.setCursor(0, 0);
lcd.print("C: ");
lcd.setCursor(2, 0);
lcd.print(currentC);
} else {
lcd.setCursor(0, 0);
lcd.print("A: B: ");
lcd.setCursor(2, 0);
lcd.print(currentA);
lcd.setCursor(8, 0);
lcd.print(currentB);
}
lcd.setCursor(0, 1);
lcd.print(">");
lcd.setCursor(1, 1);
lcd.print(command);
}
void updateLCD() {
if (command.length() >= 3) {
lcd.setCursor(2, 0);
if (command[0] == 'C') {
lcd.print(command.substring(1, 3));
} else {
lcd.print(command.substring(1, 3));
}
}
if (command.length() >= 6 && !inCMode) {
lcd.setCursor(8, 0);
lcd.print(command.substring(4, 6));
}
}
void toggleLED(int index) {
if (ledState[index]) {
digitalWrite(ledPins[index], LOW);
ledState[index] = false;
} else {
digitalWrite(ledPins[index], HIGH);
ledState[index] = true;
}
}
void executeCommand() {
String cmd = command;
if (!inCMode) {
if (cmd == "A01B01") {
toggleLED(0);
currentA = "01";
currentB = "01";
} else if (cmd == "A01B02") {
toggleLED(1);
currentA = "01";
currentB = "02";
} else if (cmd == "A01B03") {
toggleLED(2);
currentA = "01";
currentB = "03";
} else if (cmd == "A01B04") {
toggleLED(3);
currentA = "01";
currentB = "04";
} else if (cmd == "A01B05") {
toggleLED(4);
currentA = "01";
currentB = "05";
} else if (cmd == "A01B06") {
toggleLED(5);
currentA = "01";
currentB = "06";
} else if (cmd == "A01B07") {
toggleLED(6);
currentA = "01";
currentB = "07";
} else if (cmd == "A01B08") {
toggleLED(7);
currentA = "01";
currentB = "08";
} else if (cmd == "A00B99") {
// Light all LEDs
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], HIGH);
ledState[i] = true;
}
currentA = "00";
currentB = "99";
} else if (cmd == "A00B00") {
// Turn off all LEDs
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], LOW);
ledState[i] = false;
}
currentA = "00";
currentB = "00";
} else if (cmd == "C") {
inCMode = true;
currentC = "";
command = "";
refreshLCD();
return;
} else {
lcd.setCursor(0, 2);
lcd.print("Invalid Command");
}
lcd.setCursor(2, 0);
lcd.print(currentA);
lcd.setCursor(8, 0);
lcd.print(currentB);
} else {
if (cmd == "C00") {
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], LOW);
ledState[i] = false;
}
currentC = "00";
} else if (cmd == "C01") {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[3], HIGH);
ledState[0] = true;
ledState[3] = true;
currentC = "01";
} else if (cmd == "C02") {
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
ledState[1] = true;
ledState[2] = true;
currentC = "02";
} else if (cmd == "C03") {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
ledState[0] = true;
ledState[1] = true;
currentC = "03";
} else if (cmd == "C04") {
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
ledState[2] = true;
ledState[3] = true;
currentC = "04";
} else if (cmd == "C05") {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
ledState[0] = true;
ledState[1] = true;
ledState[2] = true;
ledState[3] = true;
currentC = "05";
} else if (cmd == "C06") {
for (int i = 0; i < 6; i++) {
digitalWrite(ledPins[i], HIGH);
ledState[i] = true;
}
currentC = "06";
} else if (cmd == "C07") {
for (int i = 0; i < 7; i++) {
digitalWrite(ledPins[i], HIGH);
ledState[i] = true;
}
currentC = "07";
} else if (cmd == "C08") {
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], HIGH);
ledState[i] = true;
}
currentC = "08";
} else if (cmd == "C09") {
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], LOW);
ledState[i] = false;
}
currentC = "09";
} else if (cmd == "C10") {
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], HIGH);
ledState[i] = true;
}
currentC = "10";
} else if (cmd == "C") {
inCMode = false;
command = "";
refreshLCD();
return;
} else {
lcd.setCursor(0, 2);
lcd.print("Invalid Command");
}
lcd.setCursor(2, 0);
lcd.print(currentC);
}
command = "";
lcd.setCursor(1, 1);
lcd.print(" "); // Clear the command area
lcd.setCursor(1, 1);
}