-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoWater.ino
More file actions
97 lines (74 loc) · 2.38 KB
/
Copy pathAutoWater.ino
File metadata and controls
97 lines (74 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
const int sensor_pin = A0; // sensor input. analog.
const int sensor_power_pin = 7; // power up this pin to wake the sensor. digital.
const int relay_pin = 8; // signal pin for relay. digital.
const int sensor_high_mark = 90; // calibrated "wet" value for the sensor
const int sensor_low_mark = 860; // calibrated "dry" value for the sensor
const int relay_enable_threshold = 45; // percent
const int cycle_delay = 1000; // poll sensor every x millisseconds
const int EventType_Reading = 1;
const int EventType_Low = 2;
const int EventType_High = 3;
const int EventType_Information = 4;
int sensor_value;
bool relay_enabled = false;
void setup() {
pinMode(sensor_pin, INPUT);
pinMode(relay_pin, OUTPUT);
pinMode(sensor_power_pin, OUTPUT);
relay_off();
sensor_off();
Serial.begin(9600);
output_event_xml(EventType_Information, "Reading from sensor...");
}
void loop() {
sensor_on();
sensor_value = analogRead(sensor_pin);
sensor_off();
// convert the analog value to a percentage
sensor_value = map(sensor_value, sensor_low_mark, sensor_high_mark, 0, 100);
output_event_xml(EventType_Reading, String(sensor_value));
if (sensor_value < relay_enable_threshold) {
if (!relay_enabled) {
output_event_xml(EventType_Low, "Deactivating pump.");
relay_on();
}
} else {
if (relay_enabled) {
output_event_xml(EventType_Low, "Activating pump.");
relay_off();
}
}
if (relay_enabled) {
output_event_xml(EventType_Information, "Relay is active.");
}
delay(cycle_delay);
}
void sensor_off() {
digitalWrite(sensor_power_pin, LOW);
delay(50);
}
void sensor_on() {
digitalWrite(sensor_power_pin, HIGH);
delay(100); // give module time to start.
}
void relay_off() {
output_event_xml(EventType_Information, "Turning relay off.");
relay_enabled = false;
digitalWrite(relay_pin, LOW);
}
void relay_on() {
output_event_xml(EventType_Information, "Turning relay on.");
relay_enabled = true;
digitalWrite(relay_pin, HIGH);
}
void output_event_xml(int type, String message) {
Serial.print("<event>");
Serial.print("<type>");
Serial.print(type);
Serial.print("</type>");
Serial.print("<message>");
Serial.print(message);
Serial.print("</message>");
Serial.print("</event>");
Serial.println();
}