-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoilMoisture.ino
57 lines (49 loc) · 1.84 KB
/
soilMoisture.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
/*
Authors: Brenotn Mizell and Eric Martin
EE 474 Lab4: Final Project
06/07/2023
GREENHOUSE: WATERING SYSTEM
*/
void SoilMoistureTaskInit() {
xTaskCreate(TaskSoilMoisture, "SoilMoisture", 1000, NULL, 2, &SoilMoistureTaskHandle);
}
void TaskSoilMoisture(void *pvParameters) {
pinMode(moisturePump, OUTPUT);
pinMode(moistureIn, INPUT);
digitalWrite(moisturePump, HIGH);
for (;;) // A Task shall never return or exit.
{
dt = clock.getDateTime();
// Run this task every day at 0800
if ((dt.hour == 8 && dt.minute == 0) || manualRun) {
waterLevelGet();
float moistureValue = analogRead(moistureIn);
if (moistureValue < 600 && !waterLevelLow) {
// If it's dry and the water level is not low, start the pump
lcd.print("!!");
digitalWrite(moisturePump, LOW);
pumpStartMillis = millis(); // Remember when the pump was started
pumpIsRunning = true; // Remember that the pump is running
setDisplay(SOIL);
} else {
digitalWrite(moisturePump, HIGH);
pumpIsRunning = false; // The pump is not running
manualRun = false;
}
if (displaySoil) {
printToLCD("Moisture Level", String(moistureValue));
}
// Check if the pump has been running for 5 seconds
if (pumpIsRunning && millis() - pumpStartMillis >= 1500) {
// If the pump has been running for 3 seconds, stop it
digitalWrite(moisturePump, LOW);
pumpIsRunning = false;
manualRun = false;
}
// delay task for one minute after executing to avoid repetition during the 8th hour
vTaskDelay(60 * 1000 / portTICK_PERIOD_MS);
}
// small delay to prevent the task from occupying the CPU all the time
vTaskDelay(pdMS_TO_TICKS(100)); // delay for 1 second
}
}