-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelfWateringPlanter.ino
217 lines (211 loc) · 6.04 KB
/
SelfWateringPlanter.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Includes temperature sensor library (DHT11) //
#include "dht.h"
// Includes library needed for the I2C backpack of LCD and other LCD
libraries\
#include "Wire.h"
#include "LCD.h"
#include "LiquidCrystal_I2C.h"
// Defines the pins used by the temperature sensor\
#define DHTTYPE DHT11
#define DHTPIN 6\
DHT dht(DHTPIN, DHTTYPE);
//Set the pins on the I2C chip used for LCD connections
//ADDR,EN,R/W,RS,D4,D5,D6,D7
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7);
// pH sensor //
#include <SoftwareSerial.h>
//we have to include the SoftwareSerial library, or else we can't use it #define rx 8
//define what pin rx is going to be
#define tx 9
//define what pin tx is going to be
SoftwareSerial myserial(rx, tx);
//define how the soft serial port is going to work
String inputstring = "";
//a string to hold incoming data from the PC
String sensorstring = "";
//a string to hold the data from the Atlas Scientific product boolean input_string_complete = false;
//have we received all the data from the PC
boolean sensor_string_complete = false;
//have we received all the data from the Atlas Scientific product
// These two variables will be used to hold the value generated by the pH sensor
float pH;
float ultimatepH = 0;
// Defines the pin numbers for the sensors in the arduino
const int conductivityPin = A0;
const int waterSensorPin = A1;
const int pumpOnePin = 11;
const int pumpTwoPin = 10;
boolean phIsOff = true;
boolean lowWater = false;
const float desiredPh = 4.5;
int counter = 0;
int frontScreen = 1;
void setup(){
//Debugging
Serial.begin(9600); // Air pump //
pinMode(7, OUTPUT);
// Pump One //
pinMode(pumpOnePin, OUTPUT);
// Pump Two //
pinMode(pumpTwoPin, OUTPUT);
// Button //
pinMode(13, INPUT);
AirPumpOn();
// initializes pH sensor //
myserial.begin(9600);
//set baud rate for the software serial port to 9600
inputstring.reserve(10);
//set aside some bytes for receiving data from the PC
sensorstring.reserve(30);
//set aside some bytes for receiving data from Atlas Scientific product
// initializes LCD module
lcd.begin (16,2); // 16 x 2 LCD module
lcd.setBacklightPin(3,POSITIVE); // BL, BL_POL
lcd.setBacklight(HIGH);
//digitalWrite(2, HIGH);
//turnPumpTwo(true);
}
void loop() {
ultimatepH = digitalRead(13);
// Get pH level
if (input_string_complete) { //if a string from the PC has been received in its entirety
myserial.print(inputstring);
//send that string to the Atlas Scientific product
myserial.print('\\r');
//add a <CR> to the end of the string
inputstring = "";
//clear the string
input_string_complete = false;
//reset the flag used to tell if we have received a completed string from the PC
}
if (myserial.available() > 0) {
//if we see that the Atlas Scientific product has sent a character
char inchar = (char)myserial.read();
//get the char we just received
sensorstring += inchar;
//add the char to the var called sensorstring
if (inchar == '\\r') {
//if the incoming character is a <CR>\
sensor_string_complete = true;
//set the flag
} }
if (sensor_string_complete == true) {
//if a string from the Atlas Scientific product has been received in its entirety
pH = sensorstring.toFloat();
//send that string to the PC's serial monitor
sensorstring = "";
//clear the string
sensor_string_complete = false;
//reset the flag used to tell if we have received a completed string from the Atlas Scientific product
} Serial.println(pH);
// Checks if the pH sensor has started to record a value
if(pH != 0) {
phIsOff = false;
}
// If not, then it won't start the loop
if(!pH) {
return; }
// If the water is too low, it will load water
if(loadWaterByLevel()) {
delay(500);
return;
}
// If the pH level is above/below threshold, it will replace the water in the tank
if(!checkPh()) {
if(counter == 6) {
counter = 0;
frontScreen = -frontScreen;
}
// Debug value Serial.println(frontScreen);
// This if/else block will alternate what is displayed on the
// LCD screen. In the first state, it will display the temperature,
// and the humidity. In the second state, it will display the pH value
// and the H2O EC.
if(frontScreen == 1) {
lcd.clear();\
lcd.print("- Temp: ");
lcd.print(GetTemperature());
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("- Humidity: ");
lcd.print(GetHumidity());
lcd.print("%");\
} else {
lcd.clear();
lcd.print("- pH level: ");
lcd.print(pH);
lcd.setCursor(0,1);
lcd.print("- H2O EC: ");
lcd.print(GetConductivityValue());
}
counter++;
delay(500);
} }
// Checks if the pH level is above or below the threshold\
bool checkPh() {
if(ultimatepH == 1) {
turnPumpOne(true);
AirPumpOff();
lcd.clear();
lcd.print("- low pH lvl");
lcd.setCursor(0,1);
lcd.print("Removing H2O");
delay(30000);
AirPumpOn();
}
}
// Loads water into the tank if the level is too low
bool loadWaterByLevel() {
if(GetWaterLevel() <= 480) {
turnPumpTwo(true);
turnPumpOne(false);
lcd.clear();
lcd.print("- low water lvl");
lcd.setCursor(0,1);
lcd.print("- Adding water");
return true;
} else {
turnPumpOne(false);
turnPumpTwo(false);
return false;
} }
// Gets conductivity value
int GetConductivityValue() {
return analogRead(conductivityPin);
}
// Gets water sensor level
int GetWaterLevel() {
return analogRead(waterSensorPin);
}
// Turns air pump on
void AirPumpOn() {
digitalWrite(7, HIGH);
}
// Turns air pump off
void AirPumpOff() {
digitalWrite(7, LOW);
}
// Gets the humidity from the DHT11 sensor
float GetHumidity() {
return dht.readHumidity();
}
// Gets the temperature from the DHT11 sensor
float GetTemperature() {
return dht.readTemperature();
}
// Turns pump one on or off //
void turnPumpOne(boolean state) {
if(state) {
digitalWrite(pumpOnePin, HIGH);
} else {
digitalWrite(pumpOnePin, LOW);
}
}
// Turns pump two on or off //
void turnPumpTwo(boolean state) {
if(state) {
digitalWrite(pumpTwoPin, HIGH);
} else {
digitalWrite(pumpTwoPin, LOW);
}
}