-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureControl.ino
More file actions
125 lines (101 loc) · 2.99 KB
/
TemperatureControl.ino
File metadata and controls
125 lines (101 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Temperature Control
// Written by Romualdo Dasig for Elex Project
// Released under an MIT license.
// Depends on the following Arduino libraries:
// - LiquidCrystal Library: https://www.arduino.cc/en/Reference/LiquidCrystal
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
#include <LiquidCrystal.h>
#include <DHT.h>
// Define constants.
#define DEBUG 0 // Enable or disable debugging
#define DHTPIN A0 // Temperature Sense pin
#define DHTTYPE DHT11 // DHT 11
// Configure Temperature sensor.
DHT dht(DHTPIN, DHTTYPE);
float hum; // Humidity
float temp; // Temperature
unsigned long previousMillis = 0; // Variable to store current time.
const long interval = 2000; // Set interval to read temperature.
// Configure LCD display.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Configure aircon and fan motors.
const int airconPin = 9;
const int fanPin = 10;
// Configure speed controller.
const int speedControlPin = 8;
const int lowSpeed = 125;
const int highSpeed = 255;
int speedVal = lowSpeed;
// Set temperature settings.
int airconState = 0;
const float highTempSetting = 26.00;
const float lowTempSetting = 23.00;
void setup() {
if (DEBUG) {
// Initialize Serial monitor.
Serial.begin(9600);
}
// Initialize Temperature sensor.
dht.begin();
// Initialize a 16x2 LCD display.
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
// Initialize aircon and fan motors.
pinMode(airconPin, OUTPUT);
pinMode(fanPin, OUTPUT);
// Initialize speed controller.
pinMode(speedControlPin, INPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// Save the last time we've read the temperature.
previousMillis = currentMillis;
// Read data and store it to variables hum and temp.
hum = dht.readHumidity();
temp= dht.readTemperature();
if (DEBUG) {
// Print temp and humidity values to serial monitor.
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.print(" *C\n");
}
}
// Display temperature and humidity.
lcd.setCursor(6, 0);
lcd.print(temp);
lcd.setCursor(6, 1);
lcd.print(hum);
// Set speed of the motor and fan.
int speedState = digitalRead(speedControlPin);
if (speedState == HIGH) {
speedVal = highSpeed;
}
else {
speedVal = lowSpeed;
}
// Run fan motor.
analogWrite(fanPin, speedVal);
// Turn on the aircon when temperature is high.
if (airconState == 1) {
if (temp <= lowTempSetting) {
// Turn off the aircon.
analogWrite(airconPin, 0);
// Set aircon state to idle.
airconState = 0;
}
}
else {
if (temp >= highTempSetting) {
// Turn on the aircon.
analogWrite(airconPin, lowSpeed);
// Set aircon state to running.
airconState = 1;
}
}
}