0% found this document useful (0 votes)
12 views10 pages

Capstone

Coding of automatic exhaust fan

Uploaded by

bernie oplas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Capstone

Coding of automatic exhaust fan

Uploaded by

bernie oplas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <Wire.

h>

#include <LiquidCrystal_I2C.h>

#include "Adafruit_SHT4x.h"

// Define pins

const int relay_pin = A1;

const int buzzer_pin = A2;

// Initialize SHT41 sensor

Adafruit_SHT4x sht4x = Adafruit_SHT4x();

// Define LCD parameters

LiquidCrystal_I2C lcd(0x27, 16, 2);

// Non-blocking timer variables for sensor

unsigned long previousSensorMillis = 0;

const long sensorInterval = 1000; // SHT41 can be read more frequently, 1


second is fine

// Non-blocking timer variables for buzzer

unsigned long previousBuzzerMillis = 0;

const long buzzerOnDuration = 100; // Time the buzzer is ON (e.g., 100ms)

const long buzzerOffDuration = 3000; // Time the buzzer is OFF (3 seconds)

// Non-blocking timer variables for "FANMATIC" text

unsigned long previousTextMillis = 0;

const long textInterval = 30000; // 30 seconds


// Sensor data variables

float humidity = 0.0;

float temperature = 0.0;

// State variable for buzzer

bool buzzerState = LOW; // Tracks the current state of the buzzer

void setup() {

Serial.begin(9600);

Wire.begin();

lcd.init();

lcd.backlight();

// SHT41 sensor initialization

if (! sht4x.begin()) {

Serial.println("Couldn't find SHT4x");

while (1) delay(1);

Serial.println("Found SHT4x sensor");

pinMode(relay_pin, OUTPUT);

pinMode(buzzer_pin, OUTPUT);

// Initial read to populate values from SHT41

sensors_event_t humidity_event, temp_event;

sht4x.getEvent(&humidity_event, &temp_event);
if (!isnan(temp_event.temperature) && !
isnan(humidity_event.relative_humidity)) {

temperature = temp_event.temperature;

humidity = humidity_event.relative_humidity;

void loop() {

unsigned long currentMillis = millis();

// Sensor reading logic (runs every 1 second)

if (currentMillis - previousSensorMillis >= sensorInterval) {

previousSensorMillis = currentMillis;

sensors_event_t humidity_event, temp_event;

sht4x.getEvent(&humidity_event, &temp_event);

if (!isnan(temp_event.temperature) && !
isnan(humidity_event.relative_humidity)) {

temperature = temp_event.temperature;

humidity = humidity_event.relative_humidity;

Serial.print("Temp: ");

Serial.print(temperature);

Serial.print(" °C");

Serial.print(" Hum: ");

Serial.print(humidity);
Serial.println(" %");

// "FANMATIC" text display logic (runs every 30 seconds)

if (currentMillis - previousTextMillis >= textInterval) {

previousTextMillis = currentMillis;

// Display "FANMATIC" and then clear after a short delay to be seen

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("FANMATIC");

delay(3000); // Wait for 3 seconds to make the text visible

lcd.clear(); // Clear the screen before the main display loop resumes

// Main LCD display logic (runs continuously unless interrupted by


FANMATIC)

lcd.setCursor(0, 0);

lcd.print("Hum: ");

lcd.print(humidity);

lcd.print(" %");

lcd.setCursor(0, 1);

lcd.print("Temp: ");

lcd.print(temperature);

lcd.print("`C");
// Control buzzer and relay based on humidity OR temperature

if (humidity > 60.0 || temperature > 25.0) {

digitalWrite(relay_pin, LOW);

// Buzzer blinking logic (runs continuously while relay is OFF)

if (buzzerState == LOW && (currentMillis - previousBuzzerMillis >=


buzzerOffDuration)) {

digitalWrite(buzzer_pin, HIGH);

previousBuzzerMillis = currentMillis;

buzzerState = HIGH;

} else if (buzzerState == HIGH && (currentMillis - previousBuzzerMillis >=


buzzerOnDuration)) {

digitalWrite(buzzer_pin, LOW);

previousBuzzerMillis = currentMillis;

buzzerState = LOW;

} else {

// If conditions are not met, ensure both are off

digitalWrite(buzzer_pin, LOW);

digitalWrite(relay_pin, HIGH);

}
#include <Wire.h>

#include <LiquidCrystal_I2C.h>

#include <DHT.h>

// Define pins

const int relay_pin = A1;

const int buzzer_pin = A2;

const int DHTPIN = 2; // Pin where the DHT sensor is connected

const int DHTTYPE = DHT11; // Can be DHT11 or DHT22

// Initialize DHT sensor

DHT dht(DHTPIN, DHTTYPE);

// Define LCD parameters

LiquidCrystal_I2C lcd(0x27, 16, 2);

// Non-blocking timer variables for sensor

unsigned long previousSensorMillis = 0;

const long sensorInterval = 2000; // DHT sensors require a 2-second interval

// Non-blocking timer variables for buzzer

unsigned long previousBuzzerMillis = 0;

const long buzzerOnDuration = 100; // Time the buzzer is ON (e.g., 100ms)

const long buzzerOffDuration = 3000; // Time the buzzer is OFF (3 seconds)

// Non-blocking timer variables for "FANMATIC" text

unsigned long previousTextMillis = 0;


const long textInterval = 30000; // 30 seconds

// Sensor data variables

float humidity = 0.0;

float temperature = 0.0;

// State variable for buzzer

bool buzzerState = LOW; // Tracks the current state of the buzzer

void setup() {

Serial.begin(9600);

Wire.begin();

lcd.init();

lcd.backlight();

dht.begin();

pinMode(relay_pin, OUTPUT);

pinMode(buzzer_pin, OUTPUT);

// Initial read to populate values

humidity = dht.readHumidity();

temperature = dht.readTemperature();

void loop() {

unsigned long currentMillis = millis();


// Sensor reading logic (runs every 2 seconds)

if (currentMillis - previousSensorMillis >= sensorInterval) {

previousSensorMillis = currentMillis;

float newHumidity = dht.readHumidity();

float newTemperature = dht.readTemperature();

if (!isnan(newHumidity) && !isnan(newTemperature)) {

humidity = newHumidity;

temperature = newTemperature;

Serial.print("Temp: ");

Serial.print(temperature);

Serial.print(" °C");

Serial.print(" Hum: ");

Serial.print(humidity);

Serial.println(" %");

// "FANMATIC" text display logic (runs every 30 seconds)

if (currentMillis - previousTextMillis >= textInterval) {

previousTextMillis = currentMillis;

// Display "FANMATIC" and then clear after a short delay to be seen

lcd.clear();

lcd.setCursor(0, 0);
lcd.print("FANMATIC");

delay(3000); // Wait for 3 seconds to make the text visible

lcd.clear(); // Clear the screen before the main display loop resumes

// Main LCD display logic (runs continuously unless interrupted by


FANMATIC)

lcd.setCursor(0, 0);

lcd.print("Hum: ");

lcd.print(humidity);

lcd.print(" %");

lcd.setCursor(0, 1);

lcd.print("Temp: ");

lcd.print(temperature);

lcd.print("`C");

// Control buzzer and relay based on humidity OR temperature

if (humidity > 60.0 || temperature > 25.0) {

digitalWrite(relay_pin, LOW);

// Buzzer blinking logic (runs continuously while relay is OFF)

if (buzzerState == LOW && (currentMillis - previousBuzzerMillis >=


buzzerOffDuration)) {

digitalWrite(buzzer_pin, HIGH);

previousBuzzerMillis = currentMillis;

buzzerState = HIGH;
} else if (buzzerState == HIGH && (currentMillis - previousBuzzerMillis >=
buzzerOnDuration)) {

digitalWrite(buzzer_pin, LOW);

previousBuzzerMillis = currentMillis;

buzzerState = LOW;

} else {

// If conditions are not met, ensure both are off

digitalWrite(buzzer_pin, LOW);

digitalWrite(relay_pin, HIGH);

Final

You might also like