CCCPaste

**Hazard Detection** rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: **Hazard Detection**
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2025-02-10 15:46:21

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* code for a fire fighting robot using arduino, dc */
	/* motors, relay module, buzzer, and water pump */
/****** END SYSTEM REQUIREMENTS *****/

/* START CODE */

/****** DEFINITION OF LIBRARIES *****/
#include           // Include Servo library for controlling servos
#include            // Include Wire library for I2C communication
#include  // Include Adafruit Sensor library
#include             // Include DHT sensor library for temperature and humidity
#include         // Include NewPing library for ultrasonic sensor

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

// Define pin numbers
const int relayPin = 7;        // Relay module pin
const int buzzerPin = 8;       // Buzzer pin
const int pumpPin = 9;         // Water pump pin
const int trigPin = 10;        // Ultrasonic sensor trigger pin
const int echoPin = 11;        // Ultrasonic sensor echo pin
const int dhtPin = 2;          // DHT sensor pin

// Create instances of the libraries
DHT dht(dhtPin, DHT22);         // DHT22 sensor
NewPing sonar(trigPin, echoPin); // Ultrasonic sensor

void setup(void)
{
    // Initialize serial communication for debugging
    Serial.begin(9600);
    
    // Initialize the DHT sensor
    dht.begin();
    
    // Set pin modes
    pinMode(relayPin, OUTPUT);
    pinMode(buzzerPin, OUTPUT);
    pinMode(pumpPin, OUTPUT);
    
    // Initialize the relay, buzzer, and pump to off
    digitalWrite(relayPin, LOW);
    digitalWrite(buzzerPin, LOW);
    digitalWrite(pumpPin, LOW);
}

void loop(void)
{
    // Read temperature and humidity
    float temperature = dht.readTemperature();
    float humidity = dht.readHumidity();

    // Check if any reads failed and exit early (to try again).
    if (isnan(temperature) || isnan(humidity)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
    }

    // Print temperature and humidity to serial monitor
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.print(" °C, Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");

    // Measure distance using ultrasonic sensor
    delay(50); // Wait for sensor to stabilize
    unsigned int distance = sonar.ping_cm(); // Get distance in cm

    // Print distance to serial monitor
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

    // Fire fighting logic
    if (temperature > 50.0) { // Example threshold for fire
        digitalWrite(relayPin, HIGH); // Activate relay
        digitalWrite(pumpPin, HIGH);   // Activate water pump
        tone(buzzerPin, 1000);         // Sound buzzer
        Serial.println("Fire detected! Activating systems.");
    } else {
        digitalWrite(relayPin, LOW); // Deactivate relay
        digitalWrite(pumpPin, LOW);   // Deactivate water pump
        noTone(buzzerPin);            // Turn off buzzer
    }

    delay(1000); // Wait before next loop iteration
}

/* END CODE */