//filename:
//Arduino UNO test and Alarm 2 in 1 out (2023.10.31) v2
#include <DHT.h>
#define DHTTYPE DHT22 //Ser DHT 22 (AM2302), AM2321 to DHT22
#define EnablePIN 2 //Set Enable flag to pin 2
#define DHTPIN 4 //Set DHT sensor to pin 4
#define AlarmPin 12 //Set alarm sensor to pin 12
//dht DHT;
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
int HumidityTh = 80; //Set Humidity threshold
int HighTemperatureTh = 36; //Set high temperature threshold
int LowTemperatureTh = 12; //Set low temperature threshold
void setup() {
// put your setup code here, to run once:
pinMode(EnablePIN, INPUT); //set alert switch input
pinMode(DHTPIN, INPUT); //set DHT22, i.e, tempature, humidity sensoe input
pinMode(12, OUTPUT); //set buzzer output when alert
pinMode(13, OUTPUT); //set LED output
Serial.begin(19200);
Serial.println("DHT22_test.ino");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
int UncomfotableFlag = ( (h > HumidityTh ) || ( t > HighTemperatureTh) )
|| ( t < LowTemperatureTh);
if ( UncomfotableFlag == 1) {
Serial.println("Uncomfotable enviroment!");
tone(AlarmPin, 300, 250); // Plays 300Hz tone for 0.250 seconds
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
}