-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.ino
78 lines (59 loc) · 1.7 KB
/
code.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
#define MotorA 7
#define MotorB 8
const short periodA = 2360; /* Period of watering+wait groups (in SECONDS) */
const short periodB = 5790;
const short timeBetweenChecks = 10; /* How much time should arduino wait until it checks again if it is time to water (in SECONDS)[This also sets one watering duration] */
short lastTillA = periodA; /* A is the one with 2 plants and B is with 4. These variables count how many seconds have passed since the beginning of last watering */
short lastTillB = periodB;
void setup()
{
pinMode(MotorA, OUTPUT); /* 7 is A, 8 is B*/
pinMode(MotorB, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(MotorA, HIGH); /* Relay used in the system is LOW trigger, so pins idle as HIGH*/
digitalWrite(MotorB, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop()
{
if (lastTillA >= periodA && lastTillB >= periodB)
{
lastTillA = 0;
lastTillB = -timeBetweenChecks; /* -timeBetweenChecks so that it will get watered next iteration */
}
else if (lastTillA >= periodA)
{
lastTillA = 0;
}
else if (lastTillB >= periodB)
{
lastTillB = 0;
}
if (lastTillA == timeBetweenChecks)
{
stopWater(MotorA);
}
else if (lastTillB == timeBetweenChecks)
{
stopWater(MotorB);
}
if (!lastTillA)
{
startWater(MotorA);
}
else if (!lastTillB)
{
startWater(MotorB);
}
lastTillA += timeBetweenChecks;
lastTillB += timeBetweenChecks;
delay(timeBetweenChecks*1000);
}
void startWater(short motorNo)
{
digitalWrite(motorNo, LOW);
}
void stopWater(short motorNo)
{
digitalWrite(motorNo, HIGH);
}