justhrun

smarthome1.ino

Dec 22nd, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.22 KB | None | 0 0
  1. #include <IRremote.h>
  2. #include <Wire.h>
  3.  
  4. #include <LiquidCrystal_I2C.h>
  5. // Set the LCD address to 0x27 for a 16 chars and 2 line display
  6. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  7.  
  8. int RECV_PIN = 10;                  //Pin input ir receiver
  9. const unsigned long IRButton1 = 0xFF30CF;  //define tombol 1 berdasarkan pembacaan nilai hex tombol
  10. const unsigned long IRButton2 = 0xFF18E7;  //define tombol 2 berdasarkan pembacaan nilai hex tombol
  11. const unsigned long IRButton3 = 0xFF7A85;  //define tombol 3 berdasarkan pembacaan nilai hex tombol
  12. const unsigned long IRButton4 = 0xFF10EF;  //define tombol 4 berdasarkan pembacaan nilai hex tombol
  13. const int ledPin1 = 6;  //define pin output led 1
  14. const int ledPin2 = 7;  //define pin output led 2
  15. const int ledPin3 = 8;  //define pin output led 3
  16.  
  17. const int gen_starter = 9;     // output untuk menghidupkan relay genset
  18. const int pln_on = 2;          // input sensing pln on / off
  19. const int gen_on = 3;          // input sensing genset on / off
  20. const int gen_kontaktor = 4;   // output untuk menghidupkan kontaktor genset
  21. const int gen_stop = 5; // output untuk menghidupkan relay utk matikan genset
  22. /*
  23. const int pln_kontaktor = 5;   // output untuk menghidupkan kontaktor pln
  24. gak perlu, krn (2biji)kontaktor pln nyala/matinya langsung dari nyala/matinya listrik pln
  25. */
  26.  
  27. const int pir = 13;
  28. const int gas = 11;
  29. const int alm = 12; //ok
  30.  
  31. boolean gas_alm = false;
  32. boolean PLN_DOWN = false;
  33. boolean GEN_ON = false;
  34.  
  35. IRrecv irrecv(RECV_PIN);
  36. decode_results results;
  37. unsigned long BUTTONPRESSED=0;
  38.  
  39. boolean button1=false, button2=false, button3=false, button4=false;
  40.  
  41. boolean wait_dulu_pln_down=false, wait_dulu_pln_up=false;
  42. boolean suwer_pln_down = false, suwer_pln_up=false;
  43. boolean udah_notice=false;
  44. unsigned long start_wait_pln=0;
  45. const unsigned long limit_wait_pln=5000; // 5detik
  46.  
  47. boolean is_home = true;
  48.  
  49. void setup() {
  50.   Serial.begin(9600);
  51.   lcd.begin(16,2);
  52.   Serial.println("SMART HOME APPLICATION");
  53.   // Turn on the blacklight and print a message.
  54.   lcd.backlight();
  55.   homeLCD();
  56.  
  57.   pinMode(ledPin1, OUTPUT);
  58.   pinMode(ledPin2, OUTPUT);
  59.   pinMode(ledPin3, OUTPUT);
  60.   pinMode(gen_starter, OUTPUT);
  61.   pinMode(gen_stop, OUTPUT);
  62.   pinMode(pln_on, INPUT);
  63.   pinMode(gen_on, INPUT);
  64.   pinMode(gen_kontaktor, OUTPUT);
  65.   //pinMode(pln, OUTPUT);
  66.   pinMode(alm, OUTPUT);
  67.   pinMode(gas, INPUT);
  68.   pinMode(pir, INPUT);
  69.  
  70.   irrecv.enableIRIn(); // Start the receiver
  71.  
  72. }
  73.  
  74. void loop() {
  75.  
  76.   // ngurusin IRremote
  77.   if (irrecv.decode(&results)) {
  78.     BUTTONPRESSED = results.value;
  79.     Serial.print("BUTTONPRESSED: ");
  80.     Serial.println(BUTTONPRESSED, HEX);
  81.     // jangan resume dulu sebelum beres 1loop
  82.     //irrecv.resume(); // receive the next value
  83.     //delay(1000);
  84.  
  85.     switch(BUTTONPRESSED) {
  86.       case IRButton1:              // button1
  87.            button1 = !button1;
  88.            if(button1) digitalWrite(ledPin1, HIGH);
  89.            else digitalWrite(ledPin1, LOW);
  90.            break;
  91.  
  92.       case IRButton2:              // button2
  93.            button2 = !button2;
  94.            if(button2) digitalWrite(ledPin2, HIGH);
  95.            else digitalWrite(ledPin2, LOW);
  96.            break;
  97.  
  98.       case IRButton3:              // button3
  99.            button3 = !button3;
  100.            if(button3) digitalWrite(ledPin3, HIGH);
  101.            else digitalWrite(ledPin3, LOW);
  102.            break;
  103.     }
  104.   }
  105.  
  106.   // ngurusin sensor gas
  107.   gas_alm = digitalRead(gas)==LOW;
  108.   if(gas_alm) {
  109.     digitalWrite(alm, HIGH);  
  110.     delay(200);              
  111.     digitalWrite(alm, LOW);
  112.     if(is_home) {
  113.       Serial.println("GAS BOCOR BRO");
  114.       lcd.clear();
  115.       lcd.setCursor(4,0);
  116.       lcd.print("KEBOCORAN");
  117.       lcd.setCursor(6,1);
  118.       lcd.print("GAS");
  119.       is_home = false;
  120.     }
  121.   } else {
  122.     if(!is_home) {
  123.       is_home = true;
  124.       homeLCD();
  125.     }
  126.   }
  127.  
  128.   // ngurusin PLN & GENSET
  129.   if(wait_dulu_pln_down) {
  130.     if( millis()-start_wait_pln>limit_wait_pln ) {
  131.       suwer_pln_down = true;
  132.       wait_dulu_pln_down = false;
  133.     }
  134.   }
  135.  
  136.   if(wait_dulu_pln_up) {
  137.     if( millis()-start_wait_pln>limit_wait_pln ) {
  138.       suwer_pln_up = true;
  139.       wait_dulu_pln_up = false;
  140.     }
  141.   }
  142.  
  143.   PLN_DOWN = digitalRead(pln_on)==LOW;   //membaca status PLN
  144.   GEN_ON = digitalRead(gen_on)==HIGH;    //membaca status GENSET
  145.   if(PLN_DOWN) {
  146.     if(GEN_ON) {        // genset udah nyala
  147.       // do noting
  148.       if(wait_dulu_pln_down) wait_dulu_pln_down=false; // to make sure
  149.       if(udah_notice) udah_notice = false;
  150.     } else {            // genset masih mate
  151.       if(!udah_notice) {
  152.         Serial.print("PLN DOWN... wait ");
  153.         Serial.print(limit_wait_pln/1000, DEC);
  154.         Serial.println(" detik to make sure");
  155.         udah_notice = true;
  156.       }
  157.       if(!wait_dulu_pln_down) { // tunggu dulu 5detik
  158.         wait_dulu_pln_down = true;
  159.         start_wait_pln = millis();
  160.       }
  161.       if(suwer_pln_down) { // udah 5detik masih down
  162.         suwer_pln_down = false;   // reset variable
  163.         udah_notice = false;   // reset variable
  164.         while(1) {
  165.           // pijit tombol starter genset (kalo belum dipijit)
  166.           if(!digitalRead(gen_starter)) digitalWrite(gen_starter, HIGH);
  167.           delay(200);
  168.           if( digitalRead(gen_on) ) {        // genset udah nyala?
  169.             digitalWrite(gen_starter, LOW);  // stop mijit starter genset
  170.             digitalWrite(gen_kontaktor, HIGH);  // nyalakan (1 biji)kontaktor genset
  171.             Serial.println("SWITCHED to GENSET POWER SOURCE");
  172.             break;  // exit dari while()
  173.           }  // kalo masih belum nyala juga, lanjut mijit starter
  174.         }
  175.       }
  176.     }
  177.   } else {    // PLN gak DOWN
  178.     if(GEN_ON) {              // ternyata genset juga nyala
  179.       if(!udah_notice) {
  180.         Serial.print("PLN UP... wait ");
  181.         Serial.print(limit_wait_pln/1000, DEC);
  182.         Serial.println(" detik to make sure");
  183.         udah_notice = true;
  184.       }
  185.       if(!wait_dulu_pln_up) { // tunggu dulu 5detik
  186.         wait_dulu_pln_up = true;
  187.         start_wait_pln = millis();
  188.       }
  189.       if(suwer_pln_up) { // udah 5detik, bener up
  190.         suwer_pln_up = false;   // reset variable
  191.         udah_notice = false;   // reset variable
  192.         while(1) {
  193.           // pijit tombol OFF genset (kalo belum dipijit)
  194.           if(!digitalRead(gen_stop)) digitalWrite(gen_stop, HIGH);
  195.           delay(200);
  196.           if( !digitalRead(gen_on) ) {        // genset udah mati?
  197.             digitalWrite(gen_stop, LOW);      // stop mijit OFF genset
  198.             digitalWrite(gen_kontaktor, LOW);  // matikan (1 biji)kontaktor genset
  199.             Serial.println("SWITCHED BACK to PLN POWER SOURCE");
  200.             break;  // exit dari while()
  201.           }  // kalo masih belum MATI juga, lanjut mijit stop
  202.         }
  203.       }
  204.     } else {  // genset mate kok
  205.       // do nothing
  206.       if(wait_dulu_pln_up) wait_dulu_pln_up=false; // to make sure
  207.       if(udah_notice) udah_notice = false;
  208.     }
  209.   }
  210.  
  211.   if(BUTTONPRESSED) {
  212.     BUTTONPRESSED = 0;
  213.     // barulah resume kalo abis dipijit dan beres 1 loop
  214.     irrecv.resume(); // receive the next value
  215.   }
  216. };
  217.  
  218. void homeLCD() {
  219.   lcd.setCursor(3,0);
  220.   lcd.print("SMART HOME");
  221.   lcd.setCursor(2,1);
  222.   lcd.print("APPLICATION");
  223. };
Advertisement
Add Comment
Please, Sign In to add comment