basic codes

This commit is contained in:
Siwat Sirichai 2024-06-25 16:13:11 +07:00
parent b537f4454d
commit f71ae18a7b
3 changed files with 125 additions and 9 deletions

View File

@ -12,3 +12,5 @@
platform = atmelavr
board = uno
framework = arduino
lib_deps = robtillaart/DHTNEW@^0.4.20
monitor_speed = 115200

View File

@ -1,18 +1,102 @@
#include <Arduino.h>
#include <main.hpp>
// 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);
}

30
src/main.hpp Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <Arduino.h>
#include <dhtnew.h>
#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();