Repository navigation
Expand file tree
/
Copy pathWateringControl.cpp
More file actions
106 lines (80 loc) · 2.38 KB
/
Copy pathWateringControl.cpp
File metadata and controls
106 lines (80 loc) · 2.38 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
The Source Code:
// Libraries
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>;
#include "pitches.h"
// Constants
// DHT - Humidity Temperature Sensor
#define waterPump A3 //3.3V waterPump
#define DHTPIN 2
#define DHTTYPE DHT21 //DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE); //Initialize DHT sensor for normal 16mhz Arduino
#define OLED_RESET 4 //Oled Screen Reset Pin
Adafruit_SSD1306 display(OLED_RESET);
//Variables
float hum; //Stores humidity value
float temp; //Stores temperature value
int moistureRaw; //Stores raw moisture value
int moistureReal;//Stores converted moisture value
String tmp; // Temporary variable
void setup() {
Serial.begin(9600);
dht.begin();
//Initialize display by providing the display type and its I2C address.
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
//Set the text size and color.
display.setTextSize(2);
display.setTextColor(WHITE);
pinMode(waterPump, OUTPUT);
pinMode(9, OUTPUT); // passive Buzzer
}
void loop() {
// Read data and store it to variables
hum = dht.readHumidity();
temp= dht.readTemperature();
moistureRaw = analogRead(A0);
moistureReal = map(moistureRaw, 0, 400, 0, 100);
// Clear previous image + Set to display Moisture
display.clearDisplay();
display.setCursor(0, 0);
tmp = String(moistureReal);
display.print("Moisture: " + tmp + "%");
display.display();
delay(1500);
// Clear previous image + Set to display Humidity
display.clearDisplay();
display.setCursor(0, 1);
tmp = String(hum);
display.print("Humidity: " + tmp + "%");
display.display();
delay(1500);
// Clear previous image + Set to display Temperature
display.clearDisplay();
display.setCursor(0, 1);
tmp = String(temp);
display.print("Temperature: " + tmp + "C");
display.display();
delay(1500);
// Print variables to serial monitor
Serial.print("Moisture level: ");
Serial.println(moistureReal);
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print("%, Temperature: ");
Serial.print(temp);
Serial.println(" Celsius");
delay(500);// Delay 0.5s
// Pumping water if moisture level is low
if (moistureReal > 90) {
display.clearDisplay();
display.display();
digitalWrite(waterPump, HIGH);
}
else {
display.clearDisplay();
display.display();
digitalWrite(waterPump, LOW);
}
delay(2000); //Delay 2s
}