#include <DHT.h>
#include <Servo.h>
#define DHTPIN 2
#define DHTTYPE DHT22
#define TRIG_PIN 3
#define ECHO_PIN 4
#define SERVO_PIN 5
#define POTENTIOMETER_PIN A0
#define PIN_IJO 7
#define PIN_MERAH 8
#define BUZZ 13
DHT dht(DHTPIN, DHTTYPE);
Servo servo;
//int buttonState1 = 0;
//int buttonState2 = 0;
//int buttonState3 = 0;
void setup() {
pinMode(PIN_IJO, INPUT_PULLUP);
pinMode(PIN_MERAH, INPUT_PULLUP);
dht.begin();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
servo.attach(SERVO_PIN);
noTone(BUZZ);
}
void loop() {
buttonState_IJO = digitalRead(PIN_IJO]);
buttonState_MERAH = digitalRead(PIN_MERAH);
if (buttonState_IJO == HIGH) {
funcButtonRED();
} else if (buttonState_MERAH == HIGH)
funcButtonGREEN();
} else {
funcDefault();
}
}
if (buttonState1 == HIGH) {
// Button 1 is pressed, activate sensors and control servo based on conditions
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
float distance = getUltrasonicDistance();
if (temperature >= 25 && humidity >= 60 && distance <= 100) {
// Condition 1: Temperature is high, humidity is high, and distance is low
// Perform servo control action for condition 1
servo.write(90);
tone(BUZZ, 1000);
delay(500);
} else if (temperature <= 15 && humidity <= 40 && distance > 100) {
// Condition 2: Temperature is low, humidity is low, and distance is high
// Perform servo control action for condition 2
servo.write(0);
noTone(BUZZ);
delay(500);
} else {
// Condition 3: Default condition
// Perform servo control action for condition 3
servo.write(45);
tone(BUZZ, 1000);
delay(500);
}
// Additional actions for the activated sensors can be performed here
}
if (buttonState2 == HIGH) {
// Button 2 is pressed, allow manual control of the servo
manualServoControl();
}
if (buttonState3 == HIGH) {
// Button 3 is pressed, reset the program
resetProgram();
}
}
float getUltrasonicDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
float duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void manualServoControl() {
int potValue = analogRead(POTENTIOMETER_PIN);
int servoAngle = map(potValue, 0, 1023, 0, 180);
servo.write(servoAngle);
}
// DISINI SEDANG MENYIAPKAN FUNCTION UNTUK SETIAP TOMBOL
// Button Red for
void funcButtonRED() {
}
//Button Green only for
void funcButtonGREEN() {
}
void funcButtonYELLOW() {
}
//default fofr all function
void funcDefault() {
}
//BAGIAN INI MERUPAKAN FUNCTION UNTUK MELAKUKAN RESET PROGRAM DENGAN MEMANGGIL MELALUI TOMBOL
void resetProgram() {
// Reset the program to its initial state
servo.write(0);
// Additional reset actions for other components can be implemented here
}