#include <DHT.h>
#include <Servo.h>
#define DHTPIN 2 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // DHT type
#define MQ137PIN A0 // Analog pin where the MQ137 is connected
#define LAMP_PIN 3 // Digital pin for controlling the lamp
#define FAN_PIN 4 // Digital pin for controlling the fan
#define SERVO_PIN_1 5 // Digital pin for controlling the first servo motor
#define SERVO_PIN_2 6 // Digital pin for controlling the second servo motor
DHT dht(DHTPIN, DHTTYPE);
Servo conveyorServo1; // Create a servo object for the first conveyor belt
Servo conveyorServo2; // Create a servo object for the second conveyor belt
unsigned long previousConveyorTime = 0; // Variable to store the time when the conveyor belts were last activated
const unsigned long conveyorInterval = 4 * 60 * 60 * 1000; // 4 hours in milliseconds
// Calibration phase
bool calibrationPhase = true;
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(LAMP_PIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT);
conveyorServo1.attach(SERVO_PIN_1);
conveyorServo2.attach(SERVO_PIN_2);
// Calibration phase
Serial.println("Calibrating Servo Range. Adjust the servos manually and press Enter when ready.");
while (!Serial.available()) {
delay(100);
}
Serial.read(); // Clear the buffer
calibrationPhase = false;
Serial.println("Calibration completed. Starting normal operation.");
}
void loop() {
delay(2000); // Delay between readings
// Read temperature and humidity from DHT22
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Read ammonia levels from MQ137
int ammoniaValue = analogRead(MQ137PIN);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" *C\t");
Serial.print("Ammonia Level: ");
Serial.print(ammoniaValue);
Serial.println(" ppm"); // Display ammonia level with unit "ppm"
// Control the lamp, fan, and conveyor belts based on temperature and ammonia level thresholds
if (temperature <= 20.0 && ammoniaValue <=20) {
// Turn on the lamp when the temperature is <= 20.0 degrees Celsius
digitalWrite(LAMP_PIN, HIGH);
digitalWrite(FAN_PIN, LOW);
Serial.println("Lamp ON");
Serial.println("Fan OFF");
Serial.println("Conveyor OFF");
}
else if (temperature <= 20.0 && ammoniaValue >=25) {
// Turn on the lamp when the temperature is <= 20.0 degrees Celsius
digitalWrite(LAMP_PIN, HIGH);
digitalWrite(FAN_PIN, HIGH);
Serial.println("Lamp ON");
Serial.println("Fan ON");
Serial.println("Conveyor ON");
}
else if (temperature >= 28.0 && ammoniaValue <=20) {
// Turn on the lamp when the temperature is <= 20.0 degrees Celsius
digitalWrite(LAMP_PIN, LOW);
digitalWrite(FAN_PIN, HIGH);
Serial.println("Lamp OFF");
Serial.println("Fan ON");
Serial.println("Conveyor OFF");
}
else if (temperature >= 28.0 && ammoniaValue >=25) {
// Turn on the lamp when the temperature is <= 20.0 degrees Celsius
digitalWrite(LAMP_PIN, LOW);
digitalWrite(FAN_PIN, HIGH);
Serial.println("Lamp OFF");
Serial.println("Fan ON");
Serial.println("Conveyor ON");
}
else if (ammoniaValue >= 25) {
digitalWrite(FAN_PIN, HIGH);
Serial.println("Lamp OFF");
Serial.println("Fan ON");
Serial.println("Conveyor ON");
}
else {
// Turn off the lamp for other temperature ranges
digitalWrite(LAMP_PIN, LOW);
Serial.println("Lamp OFF");
Serial.println("Fan OFF");
}
if (ammoniaValue > 25) {
// If ammonia level is >25 ppm
if (!calibrationPhase) {
// Turn on the first servo motor for 1 cycle
conveyorServo1.write(180); // Rotate the first conveyor belt to 180 degrees
delay(1000); // Conveyor belt on for 1 second
conveyorServo1.write(0); // Rotate the first conveyor belt to 0 degrees
delay(1000); // Delay between cycles
Serial.println("First Conveyor Belt ON for 1 cycle");
// Add a delay before starting the second servo
delay(2000);
// Turn on the second servo motor for 1 cycle
conveyorServo2.write(180); // Rotate the second conveyor belt to 180 degrees
delay(1000); // Conveyor belt on for 1 second
conveyorServo2.write(0); // Rotate the second conveyor belt to 0 degrees
delay(1000); // Delay between cycles
Serial.println("Second Conveyor Belt ON for 1 cycle");
}
} else {
// Turn off both servo motors for other ammonia conditions
if (!calibrationPhase) {
conveyorServo1.write(0); // Rotate the first conveyor belt to 0 degrees
conveyorServo2.write(0); // Rotate the second conveyor belt to 0 degrees
Serial.println("Conveyor Belts OFF");
}
}
// Check if it's time to activate the conveyor belts every 4 hours
unsigned long currentMillis = millis();
if (currentMillis - previousConveyorTime >= conveyorInterval) {
// Turn on the servo motors for 1 cycle each
for (int i = 0; i < 2; i++) {
conveyorServo1.write(180); // Rotate the first conveyor belt to 180 degrees
delay(1000); // Conveyor belt on for 1 second
conveyorServo1.write(0); // Rotate the first conveyor belt to 0 degrees
delay(1000); // Delay between cycles
Serial.println("First Conveyor Belt ON for 1 cycle");
// Add a delay before starting the second servo
delay(2000);
conveyorServo2.write(180); // Rotate the second conveyor belt to 180 degrees
delay(1000); // Conveyor belt on for 1 second
conveyorServo2.write(0); // Rotate the second conveyor belt to 0 degrees
delay(1000); // Delay between cycles
Serial.println("Second Conveyor Belt ON for 1 cycle");
}
// Update the time when the conveyor belts were last activated
previousConveyorTime = currentMillis;
Serial.println("Conveyor Belts ON for 2 cycles every 4 hours");
}
// You can use the 'temperature', 'humidity', and 'ammoniaValue' variables in your project as needed.
}