-
Notifications
You must be signed in to change notification settings - Fork 0
/
Smart Rainwater Harvesting System
67 lines (56 loc) · 1.87 KB
/
Smart Rainwater Harvesting System
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
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// Constants for sensor pins
const int rainSensorPin = A0;
const int soilMoisturePin = A1;
const int relayPin = 5;
// Thresholds
const int moistureThreshold = 300; // Adjust as needed
const int rainThreshold = 500; // Adjust as needed
// Initialize the OLED display (change the dimensions if necessary)
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure pump is off initially
// Initialize OLED display
if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("Rainwater Harvesting System"));
display.display();
}
void loop() {
int rainValue = analogRead(rainSensorPin);
int soilMoistureValue = analogRead(soilMoisturePin);
// Check rain sensor and activate pump if necessary
if (rainValue > rainThreshold) {
digitalWrite(relayPin, HIGH); // Turn on the pump
} else {
digitalWrite(relayPin, LOW); // Turn off the pump
}
// Check soil moisture and activate pump if necessary
if (soilMoistureValue < moistureThreshold) {
digitalWrite(relayPin, HIGH); // Turn on the pump
delay(5000); // Run the pump for 5 seconds
digitalWrite(relayPin, LOW); // Turn off the pump
}
// Display sensor values on OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print(F("Rain: "));
display.println(rainValue);
display.print(F("Soil Moisture: "));
display.println(soilMoistureValue);
display.display();
delay(60000); // Wait for 1 minute before the next loop
}