-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaterPumpPWNv002.ino
184 lines (140 loc) · 4.38 KB
/
waterPumpPWNv002.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
#include <ArduinoJson.h>
#include <Servo.h>
Servo waterPumpServo;
#define speedControlMin 1000
#define speedControlMid 1500
#define speedControlMax 2000
// Output
#define hosePinButton 2
#define irrigationPinButton 3
#define hosePin 5
#define irrigationPin 6
#define pumpPin 4
#define tank0Level0 22
#define tank0Level1 23
#define tank0Level2 24
#define tank0Level3 25
#define tank0Level4 26
#define tank1Level0 27
#define tank1Level1 28
#define tank1Level2 29
#define tank1Level3 30
#define tank1Level4 31
#define tankLevelSensors 10
long tankLevelBuffer[tankLevelSensors];
long pumpPWM = speedControlMid;
long hoseButtonPressed = 0;
long irrigationButtonPressed = 0;
long hose = 0;
long irrigation = 0;
int loopCount = 0;
String lastSendBuffer = "";
boolean valuesUpdated = false;
void setup() {
// initialize both serial ports:
//Serial.begin(115200);
Serial1.begin(115200);
// Setup the PWM for the pump speed controller
waterPumpServo.attach(pumpPin);
waterPumpServo.writeMicroseconds(speedControlMid);
pinMode(hosePinButton, INPUT_PULLUP);
pinMode(irrigationPinButton, INPUT_PULLUP);
pinMode(hosePin, OUTPUT);
pinMode(irrigationPin, OUTPUT);
pinMode(tank0Level0, INPUT_PULLUP);
pinMode(tank0Level1, INPUT_PULLUP);
pinMode(tank0Level2, INPUT_PULLUP);
pinMode(tank0Level3, INPUT_PULLUP);
pinMode(tank0Level4, INPUT_PULLUP);
pinMode(tank1Level0, INPUT_PULLUP);
pinMode(tank1Level1, INPUT_PULLUP);
pinMode(tank1Level2, INPUT_PULLUP);
pinMode(tank1Level3, INPUT_PULLUP);
pinMode(tank1Level4, INPUT_PULLUP);
}
void loop() {
if (digitalRead(hosePinButton) == true) {
hoseButtonPressed = 0;
}
else {
hoseButtonPressed = 1;
}
if (digitalRead(irrigationPinButton) == true) {
irrigationButtonPressed = 0;
}
else {
irrigationButtonPressed = 1;
}
tankLevelBuffer[0] = digitalRead(tank0Level0);
tankLevelBuffer[1] = digitalRead(tank0Level1);
tankLevelBuffer[2] = digitalRead(tank0Level2);
tankLevelBuffer[3] = digitalRead(tank0Level3);
tankLevelBuffer[4] = digitalRead(tank0Level4);
tankLevelBuffer[5] = digitalRead(tank1Level0);
tankLevelBuffer[6] = digitalRead(tank1Level1);
tankLevelBuffer[7] = digitalRead(tank1Level2);
tankLevelBuffer[8] = digitalRead(tank1Level3);
tankLevelBuffer[9] = digitalRead(tank1Level4);
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["hoseButton"] = String(hoseButtonPressed);
root["irrigationButton"] = String(irrigationButtonPressed);
root["tank0Level0"] = String(tankLevelBuffer[0]);
root["tank0Level1"] = String(tankLevelBuffer[1]);
root["tank0Level2"] = String(tankLevelBuffer[2]);
root["tank0Level3"] = String(tankLevelBuffer[3]);
root["tank0Level4"] = String(tankLevelBuffer[4]);
root["tank1Level0"] = String(tankLevelBuffer[5]);
root["tank1Level1"] = String(tankLevelBuffer[6]);
root["tank1Level2"] = String(tankLevelBuffer[7]);
root["tank1Level3"] = String(tankLevelBuffer[8]);
root["tank1Level4"] = String(tankLevelBuffer[9]);
String newSendFuffer;
root.printTo(newSendFuffer);
//Serial.println("l: " + lastSendBuffer);
//Serial.println("n: " + newSendFuffer);
if (lastSendBuffer.equals(newSendFuffer) == false or loopCount > 60) {
root.printTo(Serial1);
Serial1.println();
lastSendBuffer = newSendFuffer;
loopCount = 0;
}
if (valuesUpdated == true) {
waterPumpServo.writeMicroseconds(pumpPWM);
if (hose == 0) {
digitalWrite(hosePin, true);
}
else {
digitalWrite(hosePin, false);
}
if (irrigation == 0) {
digitalWrite(irrigationPin, true);
}
else {
digitalWrite(irrigationPin, false);
}
valuesUpdated == false;
}
loopCount++;
delay(1000);
}
void serialEvent1() {
DynamicJsonBuffer jsonBuffer;
int bufferSize = 250;
// read from port 1, send to port 0:
if (Serial1.available()) {
//int inByte = Serial1.read();
char inByte[bufferSize];
Serial1.readBytesUntil(char(0), inByte, bufferSize);
String newBuffer = String(inByte);
JsonObject& root = jsonBuffer.parseObject(newBuffer);
pumpPWM = root["pumpPWM"];
hose = root["hose"];
irrigation = root["irrigation"];
//Serial.println("pumpPWM: " + String(pumpPWM));// & root["pumpPWM"));
//Serial.println("Hose: " + String(hose));
//Serial.println("Irrigation: " + String(irrigation));
//Serial.println(newBuffer);
valuesUpdated = true;
}
}