104 lines
2.3 KiB
C++
104 lines
2.3 KiB
C++
#include <main.hpp>
|
|
|
|
DHTNEW tempSensor(DHT_PIN);
|
|
|
|
/**
|
|
* Initializes the necessary configurations and pin modes for the program.
|
|
* This function is called only once when the program starts.
|
|
*/
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
pinMode(LED1_PIN, OUTPUT);
|
|
pinMode(LED2_PIN, OUTPUT);
|
|
pinMode(LED3_PIN, OUTPUT);
|
|
pinMode(LED4_PIN, OUTPUT);
|
|
pinMode(FANA_PIN_PWM, OUTPUT);
|
|
pinMode(FANA_PIN_EN, OUTPUT);
|
|
pinMode(FANB_PIN_PWM, OUTPUT);
|
|
pinMode(FANB_PIN_EN, OUTPUT);
|
|
pinMode(BUZZER_PIN, OUTPUT);
|
|
}
|
|
|
|
/**
|
|
* The main loop function that runs repeatedly in the program.
|
|
* It reads the temperature, controls the fan and LED based on the temperature,
|
|
* and performs error handling if temperature reading fails.
|
|
*/
|
|
void loop() {
|
|
int temperature = readTemperature();
|
|
if (temperature == DHT_ERROR_VALUE) {
|
|
Serial.println("Error reading temperature");
|
|
delay(2000);
|
|
return;
|
|
}
|
|
Serial.print("Temperature: ");
|
|
Serial.println(temperature);
|
|
if (temperature > FAN_ON_TEMP) {
|
|
turnFanOn();
|
|
turnLedOn();
|
|
buzz();
|
|
} else {
|
|
turnFanOff();
|
|
turnLedOff();
|
|
}
|
|
delay(2000);
|
|
}
|
|
|
|
/**
|
|
* @brief Reads the temperature from the DHT sensor.
|
|
*
|
|
* @return The temperature value in Celsius, or DHT_ERROR_VALUE if there was an error reading the temperature.
|
|
*/
|
|
float readTemperature() {
|
|
int status = tempSensor.read();
|
|
return status == DHTLIB_OK ? tempSensor.getTemperature() : DHT_ERROR_VALUE;
|
|
}
|
|
|
|
/**
|
|
* @brief Turns on the fans.
|
|
*/
|
|
void turnFanOn() {
|
|
digitalWrite(FANA_PIN_EN, HIGH);
|
|
analogWrite(FANA_PIN_PWM, FAN_SPEED);
|
|
digitalWrite(FANB_PIN_EN, HIGH);
|
|
analogWrite(FANB_PIN_PWM, FAN_SPEED);
|
|
}
|
|
|
|
/**
|
|
* @brief Turns off the fans.
|
|
*/
|
|
void turnFanOff() {
|
|
digitalWrite(FANA_PIN_EN, LOW);
|
|
analogWrite(FANA_PIN_PWM, 0);
|
|
digitalWrite(FANB_PIN_EN, LOW);
|
|
analogWrite(FANB_PIN_PWM, 0);
|
|
}
|
|
|
|
/**
|
|
* @brief Turns on the LEDs.
|
|
*/
|
|
void turnLedOn() {
|
|
digitalWrite(LED1_PIN, HIGH);
|
|
digitalWrite(LED2_PIN, HIGH);
|
|
digitalWrite(LED3_PIN, HIGH);
|
|
digitalWrite(LED4_PIN, HIGH);
|
|
}
|
|
|
|
/**
|
|
* @brief Turns off the LEDs.
|
|
*/
|
|
void turnLedOff() {
|
|
digitalWrite(LED1_PIN, LOW);
|
|
digitalWrite(LED2_PIN, LOW);
|
|
digitalWrite(LED3_PIN, LOW);
|
|
digitalWrite(LED4_PIN, LOW);
|
|
}
|
|
|
|
/**
|
|
* @brief Activates the buzzer.
|
|
*/
|
|
void buzz() {
|
|
digitalWrite(BUZZER_PIN, HIGH);
|
|
delay(100);
|
|
digitalWrite(BUZZER_PIN, LOW);
|
|
} |