-
Notifications
You must be signed in to change notification settings - Fork 0
/
GACS.ino
140 lines (134 loc) · 4.56 KB
/
GACS.ino
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*DOCUMENTATION
*Title: GACS.ino
*Authors:
- Fernando Franco
- María F. Alvarado
- Lauro J. Pérez
- Carlos A. Ruiz
*Version: 2.5
*Initial date: 03/03/2023
*Version date: 09/5/2023
*Description: Arduino program to read environmental variables
for a plant and care it (watering or light).
*/
//Libraries:
#include "DHT.h" //DHT sensors library.
#include <SD.h> //Archive communication library.
//Define DHT method:
DHT dht(10, DHT11);
/*FUNCTION
*Name: Settings()
*Description: Initializing IN Sensors, OUT actuators and serial console.
*Return value: NONE.
*/
void Settings() {
dht.begin(); //Humidity and temperature sensors.
pinMode(A0, INPUT); //Light sensor.
pinMode(13, OUTPUT); //Light actuator.
pinMode(8, OUTPUT); //Water actuator.
Serial.begin(9600); //Serial console.
Serial.println("START"); //Show serial console.
}
/*FUNCTION
*Name: Print_Results()
*Description: Prints the results of the lectures.
*Return value: NONE.
*/
void Print_Results(float h, float t, float hi, int li, int c) {
Serial.print("----Lecture-----------------");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("%\n");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("°C\n");
Serial.print("Heat index: ");
Serial.print(hi);
Serial.print("°C\n");
Serial.print("Light intensity: ");
Serial.println(li);
Serial.print("Times the plant was cared: ");
Serial.println(c);
Serial.println("----------------------------");
}
/*FUNCTION
*Name: Automatic_Care()
*Description: Reads the sensors and applies the automatic care algorithm, then
it prints the results obtained.
*Return value: NONE.
*/
void Automatic_Care(float il_temperature, int il_light_intensity) {
//Define variables:
float humidity; //Humidity percentage.
float temperature; //Temperature in C°.
float heat_index; //Heat index in C°.
int light_intensity; //Light intensity (From MIN:0 to MAX:1024).
//Plant automatic care algorithm:
int cared = 0; //Tracks how many times the plant needed to be cared.
int caring = 1; //Finishes when plant does´nt need to be cared.
while (caring == 1) {
caring = 0;
//Read IN sensors:
humidity = dht.readHumidity(); //Read humidity.
temperature = dht.readTemperature(); //Read temperature.
light_intensity = analogRead(A1); //Read light intensity.
//Verify if values are not NULL:
if (isnan(humidity) || isnan(temperature) || isnan(light_intensity)) {
Serial.println("Failed to read from DHT sensor! Please check sensors");
delay(5000);
}
heat_index = dht.computeHeatIndex(temperature, humidity, false); //Compute heat index.
//If the plant needs water:
if (heat_index < il_temperature) { //Waters the plant.
Serial.println("Plant needs water");
delay(2500);
Serial.println("Watering...");
digitalWrite(8, LOW);
delay(10000);
digitalWrite(8, HIGH);
caring = 1;
cared += 1;
}
//If the plant needs light:
if (light_intensity < il_light_intensity) { //Provides light to the plant.
Serial.println("Plant needs light");
delay(2500);
Serial.println("Providing light...");
digitalWrite(13, HIGH);
delay(10000);
digitalWrite(13, LOW);
caring = 1;
cared += 1;
}
delay(10000); //Wait 10 seconds for getting new results.
}
Print_Results(humidity, temperature, heat_index, light_intensity, cared); //Print results when ended the algorithm.
}
/*FUNCTION
*Name: setup()
*Description: Executes the Settings() and Autmaitc_Care() functions and stab
and applies values usign a configuration file.
*Return value: NONE.
*/
void setup() {
digitalWrite(8, HIGH); //Turns off the water actuator.
Settings(); //Apply settings.
//Opening and reading GACS_config.txt file:
File config;
if (!SD.begin(10)) {
Serial.println("Couldn't start de SD");
return;
}
config = SD.open("GACS_config.txt");
float ilt; //Inferior limit of temperature.
float ill; //Inferior limit of light intensity.
if (config.available()) {
ilt = config.read(); //Read the temperature limit configuration.
ill = config.read(); //Read the light intensity limit configuration.
}
config.close(); //Closes the configuration file.
Automatic_Care(ilt, ill); //Set limits for water and light sensors.
}
void loop () {
//Nothing here.
}