Advertisement
pleasedontcode

Smart_Class_Environment rev_29

Oct 16th, 2023
64
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: Smart_Class_Environment
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-10-16 14:48:04
  15.     - Source Code generated by: AlexWind
  16.  
  17. ********* Pleasedontcode.com **********/
  18. /****** DEFINITION OF LIBRARIES *****/
  19. #include <Arduino.h>
  20. #include <Wire.h>
  21. #include <LiquidCrystal_I2C.h>
  22. #include <DHT.h>
  23.  
  24. /****** SYSTEM REQUIREMENT 1 *****/
  25. /* Display the MQ7 and MQ135 sensor values on LCD */
  26. /* and activate the relay when the values are high */
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t MQ135_Sensor_PushButton_PIN = 2;
  34. const uint8_t DHT_sensor_PIN = 10;
  35. const uint8_t MQ7_sensor_PIN = 4;
  36.  
  37. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  38. const uint8_t relay_PIN = 3;
  39.  
  40. /***** DEFINITION OF I2C PINS *****/
  41. const uint8_t lcd_I2C_PIN_SDA = A4;
  42. const uint8_t lcd_I2C_PIN_SCL = A5;
  43. const uint8_t lcd_I2C_SLAVE_ADDRESS = 0x27;
  44.  
  45. // Initialize the LCD object with I2C address and dimensions
  46. LiquidCrystal_I2C lcd(lcd_I2C_SLAVE_ADDRESS, 16, 2);
  47.  
  48. // Initialize the DHT object with the sensor type and pin
  49. DHT dht(DHT_sensor_PIN, DHT11);
  50.  
  51. void setup(void)
  52. {
  53.   // Set the input and output pin modes
  54.   pinMode(MQ135_Sensor_PushButton_PIN, INPUT_PULLUP);
  55.   pinMode(MQ7_sensor_PIN, INPUT);
  56.   pinMode(relay_PIN, OUTPUT);
  57.  
  58.   // Initialize the LCD
  59.   lcd.begin(16, 2);
  60.   lcd.setBacklight(LOW); // Turn off the backlight initially
  61.  
  62.   // Initialize the DHT sensor
  63.   dht.begin();
  64. }
  65.  
  66. void loop(void)
  67. {
  68.   // Read the sensor values
  69.   int mq7Value = analogRead(MQ7_sensor_PIN);
  70.   int mq135Value = analogRead(MQ135_Sensor_PushButton_PIN);
  71.  
  72.   // Read temperature and humidity from DHT sensor
  73.   float temperature = dht.readTemperature();
  74.   float humidity = dht.readHumidity();
  75.  
  76.   // Check if DHT sensor readings are valid
  77.   if (isnan(temperature) || isnan(humidity)) {
  78.     lcd.clear();
  79.     lcd.setCursor(0, 0);
  80.     lcd.print("DHT Read Error");
  81.     delay(2000);
  82.     return;
  83.   }
  84.  
  85.   // Display the sensor values on the LCD
  86.   lcd.setCursor(0, 0);
  87.   lcd.print("MQ7: ");
  88.   lcd.print(mq7Value);
  89.  
  90.   lcd.setCursor(0, 1);
  91.   lcd.print("MQ135: ");
  92.   lcd.print(mq135Value);
  93.  
  94.   // Check if the sensor values are high and activate the relay
  95.   if (mq7Value > 500 || mq135Value > 500) {
  96.     digitalWrite(relay_PIN, HIGH);
  97.   } else {
  98.     digitalWrite(relay_PIN, LOW);
  99.   }
  100.  
  101.   // Delay for a short period
  102.   delay(1000);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement