|
| 1 | +#include <LiquidCrystal_I2C.h> |
| 2 | +#include "DHT.h" |
| 3 | +//Modified by Muhamad Iqbal Nurmanditya |
| 4 | +#define DHTPIN 2 // what digital pin we're connected to |
| 5 | +#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 |
| 6 | +#define triger 6 |
| 7 | +#define echo 4 |
| 8 | + |
| 9 | +LiquidCrystal_I2C lcd(0x27, 16, 2); |
| 10 | +DHT dht(DHTPIN, DHTTYPE); |
| 11 | +void setup() { |
| 12 | + lcd.init(); |
| 13 | + lcd.backlight(); |
| 14 | + dht.begin(); |
| 15 | + lcd.print("Jarak, Suhu"); //Greeting |
| 16 | + lcd.setCursor(0,1); |
| 17 | + lcd.print("& Kelembaban"); |
| 18 | + delay(2000); |
| 19 | + lcd.clear(); |
| 20 | + pinMode (triger, OUTPUT); //trigger sebagai output |
| 21 | + pinMode (echo, INPUT); //echo sebagai input |
| 22 | + lcd.setCursor(0,0); |
| 23 | + lcd.print ("Suhu "); |
| 24 | + lcd.setCursor(12,0); |
| 25 | + lcd.print("C"); |
| 26 | + lcd.setCursor(0,1); |
| 27 | + lcd.print("K:"); |
| 28 | + lcd.setCursor(9,1); |
| 29 | + lcd.print ("J: "); |
| 30 | + lcd.setCursor(14,1); |
| 31 | + lcd.print("cm"); |
| 32 | +} |
| 33 | +void loop() { |
| 34 | + //delay (1000); |
| 35 | + digitalWrite (triger, HIGH); //mengirim suara |
| 36 | + delayMicroseconds(10); //selama 10 mikro detik |
| 37 | + digitalWrite (triger, LOW); //berhenti mengirim suara |
| 38 | + |
| 39 | + float jarak = pulseIn(echo, HIGH); //membaca data dan di |
| 40 | + jarak=jarak/1000000; //konversi mikro detik ke detik |
| 41 | + jarak=jarak*330/2; //data mentah di ubah ke dalam meter |
| 42 | + jarak=jarak*100; //menguba data ke dalam centi meter |
| 43 | + |
| 44 | + //cd.clear(); |
| 45 | + lcd.setCursor(5,0); |
| 46 | + lcd.print(" "); |
| 47 | + lcd.setCursor(5,0); |
| 48 | + lcd.print(getTemp( "c")); |
| 49 | + |
| 50 | + lcd.setCursor(2,1); |
| 51 | + lcd.print(getTemp( "h")); |
| 52 | + lcd.print("%"); |
| 53 | + lcd.setCursor(11,1); |
| 54 | + lcd.print(jarak); |
| 55 | + |
| 56 | + delay (500); |
| 57 | +} |
| 58 | +float getTemp(String req) |
| 59 | +{ |
| 60 | + // Reading temperature or humidity takes about 250 milliseconds! |
| 61 | + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) |
| 62 | + float h = dht.readHumidity(); |
| 63 | + // Read temperature as Celsius (the default) |
| 64 | + float t = dht.readTemperature(); |
| 65 | + // Read temperature as Fahrenheit (isFahrenheit = true) |
| 66 | + float f = dht.readTemperature(true); |
| 67 | + |
| 68 | + // Compute heat index in Fahrenheit (the default) |
| 69 | + float hif = dht.computeHeatIndex(f, h); |
| 70 | + // Compute heat index in Celsius (isFahreheit = false) |
| 71 | + float hic = dht.computeHeatIndex(t, h, false); |
| 72 | + // Check if any reads failed and exit early (to try again). |
| 73 | + if (isnan(h) || isnan(t) || isnan(f)) { |
| 74 | + //Serial.println("Failed to read from DHT sensor!"); |
| 75 | + return -273; |
| 76 | + } |
| 77 | + // Compute heat index in Kelvin |
| 78 | + float k = t + 273.15; |
| 79 | + if(req =="c"){ |
| 80 | + return t;//return Cilsus |
| 81 | + }else if(req =="f"){ |
| 82 | + return f;// return Fahrenheit |
| 83 | + }else if(req =="h"){ |
| 84 | + return h;// return humidity |
| 85 | + }else if(req =="hif"){ |
| 86 | + return hif;// return heat index in Fahrenheit |
| 87 | + }else if(req =="hic"){ |
| 88 | + return hic;// return heat index in Cilsus |
| 89 | + }else if(req =="k"){ |
| 90 | + return k;// return temprature in Kelvin |
| 91 | + }else{ |
| 92 | + return 0.000;// if no reqest found, retun 0.000 |
| 93 | + } |
| 94 | +} |
0 commit comments