CCCPaste

Smart_Class_Environment rev_29

/********* 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: Smart_Class_Environment
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-10-16 14:48:04
	- Source Code generated by: AlexWind

********* Pleasedontcode.com **********/
/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 
#include 

/****** SYSTEM REQUIREMENT 1 *****/
/* Display the MQ7 and MQ135 sensor values on LCD */
/* and activate the relay when the values are high */

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t MQ135_Sensor_PushButton_PIN = 2;
const uint8_t DHT_sensor_PIN = 10;
const uint8_t MQ7_sensor_PIN = 4;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t relay_PIN = 3;

/***** DEFINITION OF I2C PINS *****/
const uint8_t lcd_I2C_PIN_SDA = A4;
const uint8_t lcd_I2C_PIN_SCL = A5;
const uint8_t lcd_I2C_SLAVE_ADDRESS = 0x27;

// Initialize the LCD object with I2C address and dimensions
LiquidCrystal_I2C lcd(lcd_I2C_SLAVE_ADDRESS, 16, 2);

// Initialize the DHT object with the sensor type and pin
DHT dht(DHT_sensor_PIN, DHT11);

void setup(void)
{
  // Set the input and output pin modes
  pinMode(MQ135_Sensor_PushButton_PIN, INPUT_PULLUP);
  pinMode(MQ7_sensor_PIN, INPUT);
  pinMode(relay_PIN, OUTPUT);

  // Initialize the LCD
  lcd.begin(16, 2);
  lcd.setBacklight(LOW); // Turn off the backlight initially

  // Initialize the DHT sensor
  dht.begin();
}

void loop(void)
{
  // Read the sensor values
  int mq7Value = analogRead(MQ7_sensor_PIN);
  int mq135Value = analogRead(MQ135_Sensor_PushButton_PIN);
  
  // Read temperature and humidity from DHT sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  
  // Check if DHT sensor readings are valid
  if (isnan(temperature) || isnan(humidity)) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("DHT Read Error");
    delay(2000);
    return;
  }

  // Display the sensor values on the LCD
  lcd.setCursor(0, 0);
  lcd.print("MQ7: ");
  lcd.print(mq7Value);

  lcd.setCursor(0, 1);
  lcd.print("MQ135: ");
  lcd.print(mq135Value);

  // Check if the sensor values are high and activate the relay
  if (mq7Value > 500 || mq135Value > 500) {
    digitalWrite(relay_PIN, HIGH);
  } else {
    digitalWrite(relay_PIN, LOW);
  }

  // Delay for a short period
  delay(1000);
}