#include <ESP32Servo.h>
#include <DHT.h>
const int servoPin = 13;
Servo servo;
const int servoButon1Pin = 21; // Düşük hız
const int servoButon2Pin = 19; // Orta hız
const int servoButon3Pin = 17; // Yüksek hız
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11 (AM2302
DHT dht(DHTPIN, DHTTYPE);
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
int button1State = HIGH;
int button2State = HIGH;
int button3State = HIGH;
int motorSpeedChoiceLow = 1;
int motorSpeedChoiceMed = 0;
int motorSpeedChoiceHigh = 0;
void setup() {
Serial.begin(9600);
dht.begin();
servo.attach(servoPin, 500, 2400);
pinMode(servoButon1Pin, INPUT_PULLUP);
pinMode(servoButon2Pin, INPUT_PULLUP);
pinMode(servoButon3Pin, INPUT_PULLUP);
}
void loop() {
// Motor kismi
//#####################################################################################
servoRun();
// DHT11 Sensor kismi
//#####################################################################################
dhtRun();
//#####################################################################################
}
void dhtRun(){
hum = dht.readHumidity();
temp= dht.readTemperature();
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
//if (temp>2)
//{
//Fan buraya gelecek
//Serial.print("");
//Serial.print(" HOT CLIMATE");
//}
delay(500); //Delay 2 sec.
}
void servoRun(){
button1State = digitalRead(servoButon1Pin);
button2State = digitalRead(servoButon2Pin);
button3State = digitalRead(servoButon3Pin);
if (button1State == LOW) {
motorSpeedChoiceLow=1;
motorSpeedChoiceMed=0;
motorSpeedChoiceHigh=0;
} else if (button2State == LOW) {
motorSpeedChoiceLow=0;
motorSpeedChoiceMed=1;
motorSpeedChoiceHigh=0;
} else if (button3State == LOW) {
motorSpeedChoiceLow=0;
motorSpeedChoiceMed=0;
motorSpeedChoiceHigh=1;
}
if(motorSpeedChoiceLow == 1){
rotateServo(25); // Düşük hız
}else if(motorSpeedChoiceMed == 1){
rotateServo(10); // Orta hız
}else if(motorSpeedChoiceHigh == 1){
rotateServo(5); // Yüksek hız
}
}
void rotateServo(int delayTime) {
for (int i = 0; i <= 90; i += 1) {
servo.write(i);
delay(delayTime);
}
delay(100); // Durma süresi
// Servo motoru 0 dereceye geri döndür
for (int i = 90; i >= 0; i -= 1) {
servo.write(i);
delay(delayTime);
}
delay(100); // Durma süresi
//########################################################################################
}