Advertisement
pleasedontcode

**Hazard Detection** rev_01

Feb 10th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Hazard Detection**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-02-10 15:46:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* code for a fire fighting robot using arduino, dc */
  21.     /* motors, relay module, buzzer, and water pump */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Servo.h>          // Include Servo library for controlling servos
  28. #include <Wire.h>           // Include Wire library for I2C communication
  29. #include <Adafruit_Sensor.h> // Include Adafruit Sensor library
  30. #include <DHT.h>            // Include DHT sensor library for temperature and humidity
  31. #include <NewPing.h>        // Include NewPing library for ultrasonic sensor
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. // Define pin numbers
  38. const int relayPin = 7;        // Relay module pin
  39. const int buzzerPin = 8;       // Buzzer pin
  40. const int pumpPin = 9;         // Water pump pin
  41. const int trigPin = 10;        // Ultrasonic sensor trigger pin
  42. const int echoPin = 11;        // Ultrasonic sensor echo pin
  43. const int dhtPin = 2;          // DHT sensor pin
  44.  
  45. // Create instances of the libraries
  46. DHT dht(dhtPin, DHT22);         // DHT22 sensor
  47. NewPing sonar(trigPin, echoPin); // Ultrasonic sensor
  48.  
  49. void setup(void)
  50. {
  51.     // Initialize serial communication for debugging
  52.     Serial.begin(9600);
  53.    
  54.     // Initialize the DHT sensor
  55.     dht.begin();
  56.    
  57.     // Set pin modes
  58.     pinMode(relayPin, OUTPUT);
  59.     pinMode(buzzerPin, OUTPUT);
  60.     pinMode(pumpPin, OUTPUT);
  61.    
  62.     // Initialize the relay, buzzer, and pump to off
  63.     digitalWrite(relayPin, LOW);
  64.     digitalWrite(buzzerPin, LOW);
  65.     digitalWrite(pumpPin, LOW);
  66. }
  67.  
  68. void loop(void)
  69. {
  70.     // Read temperature and humidity
  71.     float temperature = dht.readTemperature();
  72.     float humidity = dht.readHumidity();
  73.  
  74.     // Check if any reads failed and exit early (to try again).
  75.     if (isnan(temperature) || isnan(humidity)) {
  76.         Serial.println("Failed to read from DHT sensor!");
  77.         return;
  78.     }
  79.  
  80.     // Print temperature and humidity to serial monitor
  81.     Serial.print("Temperature: ");
  82.     Serial.print(temperature);
  83.     Serial.print(" °C, Humidity: ");
  84.     Serial.print(humidity);
  85.     Serial.println(" %");
  86.  
  87.     // Measure distance using ultrasonic sensor
  88.     delay(50); // Wait for sensor to stabilize
  89.     unsigned int distance = sonar.ping_cm(); // Get distance in cm
  90.  
  91.     // Print distance to serial monitor
  92.     Serial.print("Distance: ");
  93.     Serial.print(distance);
  94.     Serial.println(" cm");
  95.  
  96.     // Fire fighting logic
  97.     if (temperature > 50.0) { // Example threshold for fire
  98.         digitalWrite(relayPin, HIGH); // Activate relay
  99.         digitalWrite(pumpPin, HIGH);   // Activate water pump
  100.         tone(buzzerPin, 1000);         // Sound buzzer
  101.         Serial.println("Fire detected! Activating systems.");
  102.     } else {
  103.         digitalWrite(relayPin, LOW); // Deactivate relay
  104.         digitalWrite(pumpPin, LOW);   // Deactivate water pump
  105.         noTone(buzzerPin);            // Turn off buzzer
  106.     }
  107.  
  108.     delay(1000); // Wait before next loop iteration
  109. }
  110.  
  111. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement