0% found this document useful (0 votes)
24 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)
24 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

#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() {

[Link](9600);

[Link]();

[Link]();

[Link]();

// SHT41 sensor initialization

if (! [Link]()) {

[Link]("Couldn't find SHT4x");

while (1) delay(1);

[Link]("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;

[Link](&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;

[Link](&humidity_event, &temp_event);

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

temperature = temp_event.temperature;

humidity = humidity_event.relative_humidity;

[Link]("Temp: ");

[Link](temperature);

[Link](" °C");

[Link](" Hum: ");

[Link](humidity);
[Link](" %");

// "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

[Link]();

[Link](0, 0);

[Link]("FANMATIC");

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

[Link](); // Clear the screen before the main display loop resumes

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


FANMATIC)

[Link](0, 0);

[Link]("Hum: ");

[Link](humidity);

[Link](" %");

[Link](0, 1);

[Link]("Temp: ");

[Link](temperature);

[Link]("`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() {

[Link](9600);

[Link]();

[Link]();

[Link]();

[Link]();

pinMode(relay_pin, OUTPUT);

pinMode(buzzer_pin, OUTPUT);

// Initial read to populate values

humidity = [Link]();

temperature = [Link]();

void loop() {

unsigned long currentMillis = millis();


// Sensor reading logic (runs every 2 seconds)

if (currentMillis - previousSensorMillis >= sensorInterval) {

previousSensorMillis = currentMillis;

float newHumidity = [Link]();

float newTemperature = [Link]();

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

humidity = newHumidity;

temperature = newTemperature;

[Link]("Temp: ");

[Link](temperature);

[Link](" °C");

[Link](" Hum: ");

[Link](humidity);

[Link](" %");

// "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

[Link]();

[Link](0, 0);
[Link]("FANMATIC");

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

[Link](); // Clear the screen before the main display loop resumes

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


FANMATIC)

[Link](0, 0);

[Link]("Hum: ");

[Link](humidity);

[Link](" %");

[Link](0, 1);

[Link]("Temp: ");

[Link](temperature);

[Link]("`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