// Servo Sweep example for the ESP32
// https://wokwi.com/arduino/projects/323706614646309460
#include <ESP32Servo.h>
const int servoPin = 18;
const int servopin2=4;
#define PIN_TRIG 5
#define PIN_ECHO 21
Servo servo1;
Servo servo2;
//void setup() {
//servo.attach(servoPin, 500, 2400);
//}
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
void setup() {
servo1.attach(servoPin, 500, 2400);
Serial.begin(9600);
analogReadResolution(10);
pinMode(15,INPUT);
pinMode(14,OUTPUT);
pinMode(26, OUTPUT);
servo2.attach(servopin2, 500, 2400);
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
int pos=0;
int analogValue = analogRead(15);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
if(celsius>=25)
{
digitalWrite(14, HIGH);
for (pos = 0; pos <= 180; pos += 1) {
servo1.write(pos);
//Serial.print("Fan is running.........");
delay(5);
Serial.print("FAN IS RUNNING.........");
}
for (pos = 180; pos >= 0; pos -= 1) {
servo1.write(pos);
//Serial.print("Fan is running.........");
delay(5);
Serial.print("FAN IS RUNNING.........");
}
}
else
{
digitalWrite(14, LOW);
servo1.write(0);
Serial.print("fan beoomes off.........");
delay(1000);
}
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration = pulseIn(PIN_ECHO, HIGH);
int dis=0;
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
dis=duration/58;
if(dis<100)
{
digitalWrite(4, HIGH);
tone(26,600);
servo2.write(90);
Serial.print("door open");
delay(5);
}
else
{
digitalWrite(4, LOW);
servo2.write(0);
noTone(26);
Serial.print("door locked.....");
}
//Serial.print("Distance in inches: ");
//Serial.println(duration / 148);
}