Advertisement
Syehaji93

alarm pintu dan suhu

Nov 6th, 2020
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. LiquidCrystal_I2C lcd(0x27, 20, 4); // Kalau gagal ganti 0x3F ke 0x27 for a 16 chars and 2 line display
  6.  
  7. // sensor diletakkan di pin 2
  8. #define ONE_WIRE_BUS 2
  9.  
  10. // setup sensor
  11. OneWire oneWire(ONE_WIRE_BUS);
  12.  
  13. // berikan nama variabel,masukkan ke pustaka Dallas
  14. DallasTemperature sensorSuhu(&oneWire);
  15.  
  16. float suhuSekarang;
  17. const int pinBuzzer = 5;
  18. long millisSuhu;
  19. int timerVal = 10;
  20. int timerState = 1;
  21.  
  22. //variable pintu
  23. int sensorInput = 3;
  24. int S = 10; // count seconds
  25. long millisPintu;
  26. int pintuState = 1;
  27.  
  28. void setup(void)
  29. {
  30.   lcd.init(); // initialize the lcd
  31.   lcd.backlight();
  32.   pinMode(pinBuzzer, OUTPUT);
  33.   Serial.begin(9600);
  34.   sensorSuhu.begin();
  35.  
  36.   pinMode(sensorInput, INPUT_PULLUP); //input sensor pintu
  37. }
  38.  
  39. void loop(void)
  40. {
  41.   suhuSekarang = ambilSuhu();
  42.   int sensorState = digitalRead(sensorInput);
  43.   Serial.println(suhuSekarang);
  44.   Serial.println(sensorState);
  45.   //delay(1000);
  46.  
  47.   /*Blok Kulkas*/
  48.   lcd.clear();
  49.   lcd.setCursor(0, 0);
  50.   lcd.print("Suhu:");
  51.   lcd.setCursor(6, 0);
  52.   lcd.print(suhuSekarang);
  53.  
  54.   if(suhuSekarang < 0)
  55.   {
  56.     digitalWrite(pinBuzzer, HIGH);
  57.  
  58.     lcd.setCursor(0, 1);
  59.     lcd.print("Suhu terlalu rendah");
  60.   }
  61.   else if(suhuSekarang >= 0 && suhuSekarang <= 10)
  62.   {
  63.     lcd.setCursor(0, 1);
  64.     lcd.print("Suhu normal");
  65.   }
  66.   else if(suhuSekarang > 10.00) //jika suhu lebih dari 10 derajat dan lebih dari 10 detik
  67.   {
  68.     if(millis() > millisSuhu + 1000)
  69.     {
  70.       millisSuhu = millis();
  71.       if(timerState == 1)
  72.       {
  73.         timerVal--;
  74.         lcd.setCursor(0, 1);
  75.         lcd.print("Awas Suhu Tinggi.!");
  76.        
  77.         if(timerVal == 0)
  78.         {
  79. //          digitalWrite(pinBuzzer, HIGH);
  80.           lcd.setCursor(0, 1);
  81.           lcd.print("Suhu tinggi > 30 M");
  82.           delay(100);
  83.           timerState = 0;
  84.         }
  85.         else if(timerVal > 0 && timerVal < 10 && suhuSekarang <= 10.00)
  86.         {
  87.           lcd.setCursor(0, 1);
  88.           lcd.print("Suhu normal");
  89.           timerState = 0;
  90.           timerVal = 10;
  91.         }
  92.       }
  93.     }
  94.   }
  95.  
  96.   /*Blok pintu*/
  97.   if(sensorState == 0) //jika pintu terbuka dan lebih dari 10 detik
  98.   {
  99.     if(millis() > millisPintu + 1000)
  100.     {
  101.       millisPintu = millis();
  102.       if(pintuState == 1)
  103.       {
  104.         S--;
  105.         lcd.setCursor(0,2);
  106.         lcd.print(S);
  107.         lcd.setCursor(0,3);
  108.         lcd.print("Awas Pintu Terbuka.!");
  109.         if(S == 0)
  110.         {
  111.           lcd.setCursor(0,2);
  112.           lcd.print(S);
  113.           lcd.setCursor(0,3);
  114.           lcd.print("Segera Tutup Pintu.!");
  115. //          alarmConstant();
  116.           pintuState = 0;
  117.         }
  118.       }
  119.     }
  120.   }
  121.   else if(sensorState == 1) //jika pintu tertutup
  122.   {
  123.     lcd.setCursor(0,3);
  124.     lcd.print("Pintu Tertutup.!");
  125.     pintuState = 1;
  126.     S = 10;
  127.   }
  128. }
  129.  
  130. /* Ambil value sensor suhu */
  131. float ambilSuhu()
  132. {
  133.   sensorSuhu.requestTemperatures();
  134.   float suhu = sensorSuhu.getTempCByIndex(0);
  135.   return suhu;
  136. }
  137.  
  138. void alarmConstant()
  139. {
  140.   digitalWrite(pinBuzzer, HIGH);
  141. }
  142.  
  143. void alarmOff()
  144. {
  145.   digitalWrite(pinBuzzer, LOW);
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement