#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHT_PIN 2
int buzzer = 13;
int sensormq2 = A0;
int sensorThres1 = 400;
int sensormq7 = A1;
int sensorThres2 = 400;
DHT dht(DHT_PIN, DHT22);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
dht.begin();
lcd.init();
lcd.backlight();
pinMode(buzzer, OUTPUT);
pinMode(mq2, INPUT); // pinmode buat mq 2 yaitu pin alanog A0
pinMode(mq7, INPUT); // pinmode buat mq 7 yaitu pin alanog A1
}
void loop() {
int suhu = dht.readTemperature();
int kelembapan = dht.readHumidity();
if(isnan(suhu) || isnan(kelembapan)){
Serial.println("Sensor Rusak");
} else {
Serial.print("Sensor Ok ");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(suhu);
lcd.print("C");
lcd.setCursor(9, 0);
lcd.print("H:");
lcd.print(kelembapan);
lcd.print("%");
}
mq2();
mq7();
delay(1000);
}
void mq2() {
int analogSensor = analogRead(sensormq2);
lcd.setCursor(0, 1);
lcd.print("mq2:");
lcd.print(analogSensor);
// Checks if it has reached the threshold value
if (analogSensor > sensorThres1)
{
tone(buzzer, 1000, 200);
}
else
{
noTone(buzzer);
}
delay(100);
}
void mq7() {
int analogSensor1 = analogRead(sensormq7);
lcd.setCursor(9, 1);
lcd.print("mq7:");
lcd.print(analogSensor1);
// Checks if it has reached the threshold value
if (analogSensor1 > sensorThres2)
{
tone(buzzer, 1000, 200);
}
else
{
noTone(buzzer);
}
delay(100);
}