Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <IRremote.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- // Set the LCD address to 0x27 for a 16 chars and 2 line display
- LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
- int RECV_PIN = 10; //Pin input ir receiver
- const unsigned long IRButton1 = 0xFF30CF; //define tombol 1 berdasarkan pembacaan nilai hex tombol
- const unsigned long IRButton2 = 0xFF18E7; //define tombol 2 berdasarkan pembacaan nilai hex tombol
- const unsigned long IRButton3 = 0xFF7A85; //define tombol 3 berdasarkan pembacaan nilai hex tombol
- const unsigned long IRButton4 = 0xFF10EF; //define tombol 4 berdasarkan pembacaan nilai hex tombol
- const int ledPin1 = 6; //define pin output led 1
- const int ledPin2 = 7; //define pin output led 2
- const int ledPin3 = 8; //define pin output led 3
- const int gen_starter = 9; // output untuk menghidupkan relay genset
- const int pln_on = 2; // input sensing pln on / off
- const int gen_on = 3; // input sensing genset on / off
- const int gen_kontaktor = 4; // output untuk menghidupkan kontaktor genset
- const int gen_stop = 5; // output untuk menghidupkan relay utk matikan genset
- /*
- const int pln_kontaktor = 5; // output untuk menghidupkan kontaktor pln
- gak perlu, krn (2biji)kontaktor pln nyala/matinya langsung dari nyala/matinya listrik pln
- */
- const int pir = 13;
- const int gas = 11;
- const int alm = 12; //ok
- boolean gas_alm = false;
- boolean PLN_DOWN = false;
- boolean GEN_ON = false;
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- unsigned long BUTTONPRESSED=0;
- boolean button1=false, button2=false, button3=false, button4=false;
- boolean wait_dulu_pln_down=false, wait_dulu_pln_up=false;
- boolean suwer_pln_down = false, suwer_pln_up=false;
- boolean udah_notice=false;
- unsigned long start_wait_pln=0;
- const unsigned long limit_wait_pln=5000; // 5detik
- boolean is_home = true;
- void setup() {
- Serial.begin(9600);
- lcd.begin(16,2);
- Serial.println("SMART HOME APPLICATION");
- // Turn on the blacklight and print a message.
- lcd.backlight();
- homeLCD();
- pinMode(ledPin1, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- pinMode(ledPin3, OUTPUT);
- pinMode(gen_starter, OUTPUT);
- pinMode(gen_stop, OUTPUT);
- pinMode(pln_on, INPUT);
- pinMode(gen_on, INPUT);
- pinMode(gen_kontaktor, OUTPUT);
- //pinMode(pln, OUTPUT);
- pinMode(alm, OUTPUT);
- pinMode(gas, INPUT);
- pinMode(pir, INPUT);
- irrecv.enableIRIn(); // Start the receiver
- }
- void loop() {
- // ngurusin IRremote
- if (irrecv.decode(&results)) {
- BUTTONPRESSED = results.value;
- Serial.print("BUTTONPRESSED: ");
- Serial.println(BUTTONPRESSED, HEX);
- // jangan resume dulu sebelum beres 1loop
- //irrecv.resume(); // receive the next value
- //delay(1000);
- switch(BUTTONPRESSED) {
- case IRButton1: // button1
- button1 = !button1;
- if(button1) digitalWrite(ledPin1, HIGH);
- else digitalWrite(ledPin1, LOW);
- break;
- case IRButton2: // button2
- button2 = !button2;
- if(button2) digitalWrite(ledPin2, HIGH);
- else digitalWrite(ledPin2, LOW);
- break;
- case IRButton3: // button3
- button3 = !button3;
- if(button3) digitalWrite(ledPin3, HIGH);
- else digitalWrite(ledPin3, LOW);
- break;
- }
- }
- // ngurusin sensor gas
- gas_alm = digitalRead(gas)==LOW;
- if(gas_alm) {
- digitalWrite(alm, HIGH);
- delay(200);
- digitalWrite(alm, LOW);
- if(is_home) {
- Serial.println("GAS BOCOR BRO");
- lcd.clear();
- lcd.setCursor(4,0);
- lcd.print("KEBOCORAN");
- lcd.setCursor(6,1);
- lcd.print("GAS");
- is_home = false;
- }
- } else {
- if(!is_home) {
- is_home = true;
- homeLCD();
- }
- }
- // ngurusin PLN & GENSET
- if(wait_dulu_pln_down) {
- if( millis()-start_wait_pln>limit_wait_pln ) {
- suwer_pln_down = true;
- wait_dulu_pln_down = false;
- }
- }
- if(wait_dulu_pln_up) {
- if( millis()-start_wait_pln>limit_wait_pln ) {
- suwer_pln_up = true;
- wait_dulu_pln_up = false;
- }
- }
- PLN_DOWN = digitalRead(pln_on)==LOW; //membaca status PLN
- GEN_ON = digitalRead(gen_on)==HIGH; //membaca status GENSET
- if(PLN_DOWN) {
- if(GEN_ON) { // genset udah nyala
- // do noting
- if(wait_dulu_pln_down) wait_dulu_pln_down=false; // to make sure
- if(udah_notice) udah_notice = false;
- } else { // genset masih mate
- if(!udah_notice) {
- Serial.print("PLN DOWN... wait ");
- Serial.print(limit_wait_pln/1000, DEC);
- Serial.println(" detik to make sure");
- udah_notice = true;
- }
- if(!wait_dulu_pln_down) { // tunggu dulu 5detik
- wait_dulu_pln_down = true;
- start_wait_pln = millis();
- }
- if(suwer_pln_down) { // udah 5detik masih down
- suwer_pln_down = false; // reset variable
- udah_notice = false; // reset variable
- while(1) {
- // pijit tombol starter genset (kalo belum dipijit)
- if(!digitalRead(gen_starter)) digitalWrite(gen_starter, HIGH);
- delay(200);
- if( digitalRead(gen_on) ) { // genset udah nyala?
- digitalWrite(gen_starter, LOW); // stop mijit starter genset
- digitalWrite(gen_kontaktor, HIGH); // nyalakan (1 biji)kontaktor genset
- Serial.println("SWITCHED to GENSET POWER SOURCE");
- break; // exit dari while()
- } // kalo masih belum nyala juga, lanjut mijit starter
- }
- }
- }
- } else { // PLN gak DOWN
- if(GEN_ON) { // ternyata genset juga nyala
- if(!udah_notice) {
- Serial.print("PLN UP... wait ");
- Serial.print(limit_wait_pln/1000, DEC);
- Serial.println(" detik to make sure");
- udah_notice = true;
- }
- if(!wait_dulu_pln_up) { // tunggu dulu 5detik
- wait_dulu_pln_up = true;
- start_wait_pln = millis();
- }
- if(suwer_pln_up) { // udah 5detik, bener up
- suwer_pln_up = false; // reset variable
- udah_notice = false; // reset variable
- while(1) {
- // pijit tombol OFF genset (kalo belum dipijit)
- if(!digitalRead(gen_stop)) digitalWrite(gen_stop, HIGH);
- delay(200);
- if( !digitalRead(gen_on) ) { // genset udah mati?
- digitalWrite(gen_stop, LOW); // stop mijit OFF genset
- digitalWrite(gen_kontaktor, LOW); // matikan (1 biji)kontaktor genset
- Serial.println("SWITCHED BACK to PLN POWER SOURCE");
- break; // exit dari while()
- } // kalo masih belum MATI juga, lanjut mijit stop
- }
- }
- } else { // genset mate kok
- // do nothing
- if(wait_dulu_pln_up) wait_dulu_pln_up=false; // to make sure
- if(udah_notice) udah_notice = false;
- }
- }
- if(BUTTONPRESSED) {
- BUTTONPRESSED = 0;
- // barulah resume kalo abis dipijit dan beres 1 loop
- irrecv.resume(); // receive the next value
- }
- };
- void homeLCD() {
- lcd.setCursor(3,0);
- lcd.print("SMART HOME");
- lcd.setCursor(2,1);
- lcd.print("APPLICATION");
- };
Advertisement
Add Comment
Please, Sign In to add comment