-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlanting_System_V1.java
More file actions
295 lines (262 loc) · 10.9 KB
/
Planting_System_V1.java
File metadata and controls
295 lines (262 loc) · 10.9 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package Watering_System_V1;
import org.firmata4j.*;
import org.firmata4j.firmata.FirmataDevice;
import org.firmata4j.ssd1306.*;
import java.io.IOException;
import java.util.*;
import static Watering_System_V1.PORT.*;
public class Mayank_Minor {
public static class BUTTON_LISTERNER implements IODeviceEventListener {
private final Pin BUTTON;
private final IODevice BOARD;
private final SSD1306 theOledObject;
private final Pin LED;
private final Pin MOTOR;
public BUTTON_LISTERNER(Pin BUTTON, IODevice BOARD, SSD1306 aDisplayObject) throws IOException {
this.BUTTON = BUTTON;
this.BOARD = BOARD;
this.LED = BOARD.getPin(D4);
LED.setMode(Pin.Mode.OUTPUT);
this.MOTOR = BOARD.getPin(D6);
MOTOR.setMode(Pin.Mode.OUTPUT);
theOledObject = aDisplayObject;
}
@Override
public void onPinChange(IOEvent event) {
if (event.getPin().getIndex() != BUTTON.getIndex()) {
return;
}
if (event.getValue() == 0) {
try {
LED.setValue(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
MOTOR.setValue(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
System.out.println("Event closed");
System.exit(0);
BOARD.stop();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
public void onStart(IOEvent event) {
}
@Override
public void onStop(IOEvent event) {
}
@Override
public void onMessageReceive(IOEvent event, String message) {
}
}
public static class CountTask extends TimerTask {
private long UNFILTERED_MOISTURE;
private final SSD1306 theOledObject;
private final Pin MOISTURE;
private final Pin MOTER;
private int SAMPLE;
private IODevice BOARD;
private final Pin LED;
private final Pin BOTTON;
public CountTask(SSD1306 aDisplayObject, Pin MOISTURE, Pin MOTER, Pin LED, Pin BOTTON, int SAMPLE, IODevice BOARD) throws IOException {
this.BOARD = BOARD;
theOledObject = aDisplayObject;
this.SAMPLE = SAMPLE;
this.MOISTURE = BOARD.getPin(A0);
this.BOTTON = BOTTON;
BOTTON.setMode(Pin.Mode.INPUT);
try {
MOISTURE.setMode(Pin.Mode.ANALOG);
} catch (IOException e) {
throw new RuntimeException(e);
}
this.MOTER = BOARD.getPin(D6);
try {
MOTER.setMode(Pin.Mode.OUTPUT);
} catch (IOException e) {
throw new RuntimeException(e);
}
this.LED = BOARD.getPin(D4);
try {
MOTER.setMode(Pin.Mode.OUTPUT);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public long getMoistureValues() {
return UNFILTERED_MOISTURE;
}
@Override
public void run() {
UNFILTERED_MOISTURE = MOISTURE.getValue();
double MV = ((double) 5 /1023);
double MOISTURE_VALUE = MV * UNFILTERED_MOISTURE;
System.out.println("--------------------------------------------------------------------");
System.out.println("Moisture Value in Volts: "+String.format("%.2f", MOISTURE_VALUE)+ "V");
System.out.println("Moisture Value in Sensor: "+UNFILTERED_MOISTURE);
StdDraw.setXscale(-10, 100);
StdDraw.setYscale(-30, 1100);
StdDraw.setPenRadius(0.005);
StdDraw.setPenColor(StdDraw.DARK_GRAY);
StdDraw.line(0, 0, 0, 1030);
StdDraw.line(100, 0, 100, 1030);
StdDraw.line(0, 0, 100, 0);
StdDraw.line(0, 1030, 100, 1030);
StdDraw.text(50, -30, "Sample Size");
StdDraw.text(-7, -8, "0 Volts");
StdDraw.text(-7, 1000, "5 Volts");
StdDraw.text(-5, 500, "Moisture Value [V]", 90);
StdDraw.text(50, 1100, "Time Vs Moisture");
HashMap<Integer, Integer> mypairs = new HashMap<>();
mypairs.put(SAMPLE, Integer.valueOf((int) UNFILTERED_MOISTURE));
mypairs.forEach((xValue, yValue) -> StdDraw.text(xValue, yValue, "ø"));
if (SAMPLE < 100) {
SAMPLE++;
} else {
SAMPLE = 1;
StdDraw.clear();
StdDraw.setXscale(-10, 100);
StdDraw.setYscale(-30, 1100);
StdDraw.setPenRadius(0.005);
StdDraw.setPenColor(StdDraw.DARK_GRAY);
StdDraw.line(0, 0, 0, 1030);
StdDraw.line(100, 0, 100, 1030);
StdDraw.line(0, 0, 100, 0);
StdDraw.line(0, 1030, 100, 1030);
StdDraw.text(50, -30, "Sample Size");
StdDraw.text(-7, -8, "0 Volts");
StdDraw.text(-7, 1000, "5 Volts");
StdDraw.text(-5, 500, "Moisture Value [V]", 90);
StdDraw.text(50, 1100, "Time Vs Moisture");
}
if (UNFILTERED_MOISTURE > WET_VALUE) {
theOledObject.getCanvas().drawString(0, 0, "Moisture in");
theOledObject.getCanvas().drawString(0, 10, "Volts:");
theOledObject.getCanvas().drawString(0, 20, "Sensor:");
theOledObject.getCanvas().drawString(60, 10, "V");
theOledObject.getCanvas().drawString(42, 20, String.valueOf(UNFILTERED_MOISTURE));
theOledObject.getCanvas().drawString(35, 10, String.valueOf(String.format("%.2f", MOISTURE_VALUE)));
theOledObject.getCanvas().drawHorizontalLine(0, 30, 100, MonochromeCanvas.Color.BRIGHT);
theOledObject.getCanvas().drawString(0, 35, "Status:");
theOledObject.getCanvas().drawString(0, 42,"Needs more water");
theOledObject.getCanvas().drawString(0, 52, "Status of motor: ON");
theOledObject.display();
try {
LED.setValue(1);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
MOTER.setValue(1);
} catch (IOException e) {
throw new RuntimeException(e);
}
theOledObject.clear();
} else {
theOledObject.getCanvas().drawString(0, 0, "Moisture in");
theOledObject.getCanvas().drawString(0, 10, "Volts:");
theOledObject.getCanvas().drawString(0, 20, "Sensor:");
theOledObject.getCanvas().drawString(60, 10, "V");
theOledObject.getCanvas().drawString(42, 20, String.valueOf(UNFILTERED_MOISTURE));
theOledObject.getCanvas().drawString(35, 10, String.valueOf(String.format("%.2f", MOISTURE_VALUE)));
theOledObject.getCanvas().drawHorizontalLine(0, 30, 100, MonochromeCanvas.Color.BRIGHT);
theOledObject.getCanvas().drawString(0, 35, "Status:");
theOledObject.getCanvas().drawString(0, 42,"Soil is wet");
theOledObject.getCanvas().drawString(0, 52, "Status of motor: OFF");
theOledObject.display();
try {
LED.setValue(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
MOTER.setValue(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
theOledObject.clear();
}
}
}
public static void main(String[] args) throws IOException, InterruptedException {
boolean test = true; // Testing purpose
if (test) {
testGraphSimulation();
} else {
IODevice board = new FirmataDevice(BOARD);
board.start();
board.ensureInitializationIsDone();
I2CDevice screen_object = board.getI2CDevice((byte) 0x3C);
SSD1306 SCREEN = new SSD1306(screen_object, SSD1306.Size.SSD1306_128_64);
SCREEN.init();
Pin Moisture = board.getPin(A0);
Moisture.setMode(Pin.Mode.ANALOG);
Pin Moter = board.getPin(D6);
Moter.setMode(Pin.Mode.OUTPUT);
Pin LED = board.getPin(D4);
LED.setMode(Pin.Mode.OUTPUT);
Pin BOTTON = board.getPin(D5);
BOTTON.setMode(Pin.Mode.INPUT);
CountTask MINOR_TASK = new CountTask(SCREEN, Moisture, Moter, LED, BOTTON, 0, board);
Timer MINOR_TASK_TIMER = new Timer();
MINOR_TASK_TIMER.schedule(MINOR_TASK, 0, DELAY);
board.addEventListener(new BUTTON_LISTERNER(BOTTON, board, SCREEN));
}
}
public static void testGraphSimulation() { //Testing class
List<Integer> simulatedMoisture = new ArrayList<>();
Random rand = new Random();
int sample = 0;
while (true) {
// Simulate reading
int fakeValue = 400 + rand.nextInt(300);
simulatedMoisture.add(fakeValue);
if (simulatedMoisture.size() > 100) {
simulatedMoisture.remove(0);
}
StdDraw.clear();
drawGraphAxes();
for (int i = 1; i < simulatedMoisture.size(); i++) {
int x1 = i - 1;
int y1 = simulatedMoisture.get(i - 1);
int x2 = i;
int y2 = simulatedMoisture.get(i);
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.line(x1, y1, x2, y2);
}
StdDraw.show();
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
sample++;
}
}
public static void drawGraphAxes() {
StdDraw.setXscale(-20, 100);
StdDraw.setYscale(-50, 1200);
StdDraw.setPenRadius(0.005);
StdDraw.setPenColor(StdDraw.DARK_GRAY);
StdDraw.line(0, 0, 0, 1030);
StdDraw.line(100, 0, 100, 1030);
StdDraw.line(0, 0, 100, 0);
StdDraw.line(0, 1030, 100, 1030);
StdDraw.text(50, -30, "Sample Size");
StdDraw.text(-7, -8, "0 Volts");
StdDraw.text(-7, 1000, "5 Volts");
StdDraw.text(-5, 500, "Moisture Value [V]", 90);
StdDraw.text(50, 1100, "Time Vs Moisture");
}
}
/*
Citation:
JavaTpoint."Two Decimal Places in Java."JavaTpoint,n.d.,www.javatpoint.com/two-decimal-places-java.
*/