// code gray &relay,lcd,keypad
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Konfigurasi LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Sesuaikan alamat I2C jika diperlukan
// Konfigurasi Keypad
const byte ROWS = 4; // 4 baris
const byte COLS = 4; // 4 kolom
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Pin Arduino untuk baris
byte colPins[COLS] = {5, 4, 3, 2}; // Pin Arduino untuk kolom
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Konfigurasi pin relay
const int relayCount = 6; // Jumlah relay
int relayPins[relayCount] = {A0, A1, A2, A3, 0, 1}; // Sesuaikan dengan pin yang Anda gunakan
String input = "";
void setup() {
// Inisialisasi LCD
lcd.init();
lcd.backlight();
lcd.setCursor(1, 0);
lcd.print("Simulasi tool");
delay(5000);
lcd.clear();
// Inisialisasi pin relay
for (int i = 0; i < relayCount; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Mematikan semua relay saat awal
}
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Jika tombol '#' ditekan, proses input
processInput();
} else if (key == '*') {
// Jika tombol '*' ditekan, reset input
input = "";
lcd.setCursor(0, 0);
lcd.print("Lantai: ");
} else {
// Tambahkan input dari keypad ke string input
input += key;
lcd.setCursor(0, 0);
lcd.print("Lantai: " + input);
}
}
}
void processInput() {
int decimalValue = input.toInt();
if (decimalValue >= 1 && decimalValue <= 63) {
int grayCode = decimalToGray(decimalValue);
displayGrayCode(grayCode);
controlRelays(grayCode);
} else {
lcd.setCursor(0, 1);
lcd.print("Invalid Data");
}
input = "";
}
int decimalToGray(int num) {
return num ^ (num >> 1);
}
void displayGrayCode(int grayCode) {
lcd.setCursor(0, 1);
lcd.print("Gray: ");
for (int i = relayCount - 1; i >= 0; i--) {
lcd.print((grayCode >> i) & 1);
}
}
void controlRelays(int grayCode) {
for (int i = 0; i < relayCount; i++) {
int state = (grayCode >> i) & 1;
digitalWrite(relayPins[i], state);
}
}