From f71ae18a7bf575dc91ad1fc4aa87c68312d14404 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 25 Jun 2024 16:13:11 +0700 Subject: [PATCH] basic codes --- platformio.ini | 2 + src/main.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++----- src/main.hpp | 30 +++++++++++++++ 3 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 src/main.hpp diff --git a/platformio.ini b/platformio.ini index ea23b77..8947044 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,3 +12,5 @@ platform = atmelavr board = uno framework = arduino +lib_deps = robtillaart/DHTNEW@^0.4.20 +monitor_speed = 115200 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cb9fbba..9bc3414 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,102 @@ -#include +#include -// put function declarations here: -int myFunction(int, int); +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() { - // put your setup code here, to run once: - int result = myFunction(2, 3); + 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() { - // put your main code here, to run repeatedly: + int temperature = readTemperature(); + if (temperature == DHT_ERROR_VALUE) { + Serial.println("Error reading temperature"); + return; + } + Serial.print("Temperature: "); + Serial.println(temperature); + if (temperature > FAN_ON_TEMP) { + turnFanOn(); + turnLedOn(); + buzz(); + } else { + turnFanOff(); + turnLedOff(); + } } -// put function definitions here: -int myFunction(int x, int y) { - return x + y; +/** + * @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); } \ No newline at end of file diff --git a/src/main.hpp b/src/main.hpp new file mode 100644 index 0000000..8f07dd4 --- /dev/null +++ b/src/main.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +#define LED1_PIN 9 +#define LED2_PIN 6 +#define LED3_PIN 5 +#define LED4_PIN 3 +#define FANA_PIN_PWM 10 +#define FANA_PIN_EN 12 +#define FANB_PIN_PWM 11 +#define FANB_PIN_EN 13 +#define BUZZER_PIN 4 +#define DHT_PIN 2 + +#define FAN_ON_TEMP 35 +#define FAN_SPEED 120 + +#define DHT_ERROR_VALUE -999 + +void setup(); +void loop(); + +float readTemperature(); +void turnFanOn(); +void turnFanOff(); +void turnLedOn(); +void turnLedOff(); +void buzz(); \ No newline at end of file