-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorTask.java
More file actions
48 lines (42 loc) · 1.7 KB
/
Copy pathSensorTask.java
File metadata and controls
48 lines (42 loc) · 1.7 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
import org.firmata4j.Pin;
import org.firmata4j.ssd1306.SSD1306;
import java.io.IOException;
import java.util.List;
import java.util.TimerTask;
public class SensorTask extends TimerTask {
private final Pin moistureSensor;
private final Pin waterPump;
private final SSD1306 theOledObject;
private final List<Integer> moistureData;
private boolean pumpState = false;
public SensorTask(Pin moistureSensor, Pin waterPump, SSD1306 display, List<Integer> moistureData) {
this.moistureSensor = moistureSensor;
this.waterPump = waterPump;
this.theOledObject = display;
this.moistureData = moistureData;
}
@Override
public void run() {
try {
int moistureValue = (int) moistureSensor.getValue();
moistureData.add(moistureValue);
theOledObject.getCanvas().clear();
theOledObject.getCanvas().drawString(0, 0, "Moisture: " + moistureValue);
// State machine logic
if (moistureValue > 650 && !pumpState) {
waterPump.setValue(1);
pumpState = true;
theOledObject.getCanvas().drawString(0, 9, "Pump is on.");
theOledObject.getCanvas().drawString(0, 18, "SOIL IS DRY");
} else if (moistureValue <= 650 && pumpState) {
waterPump.setValue(0);
pumpState = false;
theOledObject.getCanvas().drawString(0, 9, "Pump is off.");
theOledObject.getCanvas().drawString(0, 18, "SOIL IS WET");
}
theOledObject.display();
} catch (IOException e) {
e.printStackTrace();
}
}
}