const int echoPin = A0; // Define the Echo pin
const int tempPin = A1; // Define the Temperature pin
const int triggerPin = 13;
// Define the Trigger pin
int rev = 4;
const int fanPin = 2;
int door = 12 ; // Define the Fan pin
long duration; // Variable to store the duration of the sound wave
float distance; // Variable to store the distance in centimeters
float temperature; // Variable to store the temperature in Celsius
void setup() {
pinMode(triggerPin, OUTPUT); // Set the Trigger pin as an output
pinMode(echoPin, INPUT); // Set the Echo pin as an input
pinMode(fanPin, OUTPUT);
pinMode(door, OUTPUT);
pinMode(rev, OUTPUT); // Set the Fan pin as an output
Serial.begin(9600); // Initialize the serial communication
}
void loop() {
// Generate a pulse to the Trigger pin
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Measure the duration of the sound wave
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.034 / 2;
// Read the temperature
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int analogValue = analogRead(A1);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Temperature: ");
Serial.print(celsius);
Serial.println(" C");
// If the distance is less than 20 cm, open the door
if (distance < 60) {
digitalWrite(door, HIGH);
digitalWrite(rev, LOW);
// Code to open the door goes here
}
else{
digitalWrite(rev, HIGH);
digitalWrite(door, LOW);
}
// If the temperature is greater than 25 C, turn on the fan
if (celsius > 35) {
Serial.println("Turning on fan");
digitalWrite(2, HIGH);
delay(1000);
} else {
digitalWrite(2, LOW);
}
delay(100); // Wait for half a second before taking another reading
}