#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonPin = 5;
const int buttonPin2 = 6;
const int buttonPin3 = 7;
int lampu_1 = 2;
int lampu_2 = 3;
int kipas = 4;
int ledPin = 8;
int oldValue = LOW; // default/idle value for pin 8 is low.
int oldValue2 = LOW;
int oldValue3 = LOW;
void lcd_i2c(String text = "", int kolom = 0, int baris = 0) {
byte bar[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
if (text == "") {
lcd.init(); //jika error pakai lcd.init();
lcd.backlight();
lcd.createChar(0, bar);
lcd.setCursor(0, 0);
lcd.print("Loading..");
for (int i = 0; i < 16; i++) {
lcd.setCursor(i, 1);
lcd.write(byte(0));
delay(100);
}
delay(50);
lcd.clear();
} else {
lcd.setCursor(kolom, baris);
lcd.print(text + " ");
}
}
void lampu_1_on() {
//Aktif low, dibalik jadi High jika terbalik
digitalWrite(lampu_1, LOW);
}
void lampu_1_off() {
digitalWrite(lampu_1, HIGH);
}
void lampu_2_on() {
//Aktif low, dibalik jadi High jika terbalik
digitalWrite(lampu_2, LOW);
}
void lampu_2_off() {
digitalWrite(lampu_2, HIGH);
}
void kipas_on() {
//Aktif low, dibalik jadi High jika terbalik
digitalWrite(kipas, LOW);
}
void kipas_off() {
digitalWrite(kipas, HIGH);
}
void setup()
{
Serial.begin(115200);
Serial.println("Press the button.");
lcd_i2c();
// Initialize the pin for reading the button.
pinMode(buttonPin, INPUT);
pinMode(lampu_1, OUTPUT);
lampu_1_off();
pinMode(lampu_2, OUTPUT);
lampu_2_off();
pinMode(kipas, OUTPUT);
kipas_off();
pinMode(ledPin, OUTPUT);
}
void loop()
{
// Read the value of pin 8.
int newValue = digitalRead(buttonPin);
int newValue2 = digitalRead(buttonPin2);
int newValue3 = digitalRead(buttonPin3);
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue != oldValue)
{
if(newValue == HIGH)
{
Serial.println("relay hidup.");
lampu_1_on();
digitalWrite(ledPin, HIGH);
lcd.setCursor(0,0);
lcd.print("L1:ONN");
}
else
{
Serial.println("relay mati.");
lampu_1_off();
digitalWrite(ledPin, LOW);
lcd.setCursor(0,0);
lcd.print("L1:OFF");
}
// Remember the value for the next time.
oldValue = newValue;
}
if(newValue2 != oldValue2)
{
if(newValue2 == HIGH)
{
Serial.println("relay2 hidup.");
lampu_2_on();
lcd.setCursor(0,1);
lcd.print("L2:ONN");
}
else
{
Serial.println("relay2 mati.");
lampu_2_off();
lcd.setCursor(0,1);
lcd.print("L2:OFF");
}
// Remember the value for the next time.
oldValue2 = newValue2;
}
if(newValue3 != oldValue3)
{
if(newValue3 == HIGH)
{
Serial.println("kipas hidup.");
kipas_on();
lcd.setCursor(9,0);
lcd.print("K:ONN");
}
else
{
Serial.println("kipas mati.");
kipas_off();
lcd.setCursor(9,0);
lcd.print("K:OFF");
}
// Remember the value for the next time.
oldValue3 = newValue3;
}
// Slow down the sketch.
// Also for debouncing the button.
delay(100);
}