-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_water_softener.R
76 lines (46 loc) · 1.57 KB
/
find_water_softener.R
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
function(pulses) {
# This function calculate if a water softener has been used
# return TRUE if water softener detected (and then stop)
# return FALSE if no water softener is detected
p <- 1
while (p <= length(pulses)) { # loop that check pulses one to one
# print(p)
first_peak <- 0
second_peak <- 0
if (pulses[p] >= 2){ # detect first peak
if (pulses[p + 1] < 2){ # if the peak is finished
first_peak <- p
# cat('first peak detected at: ',p, '\n')
}
}
if (first_peak != 0 & first_peak + 10 < length(pulses)) { # detect second peak
i <- 6
while (i <= 10) { # 36-60 minutes after first peak
if (pulses[first_peak + i] >= 2) { # second peak found
second_peak <- p + i
# cat('second peak detected at: ',second_peak,'\n')
i = i+10
}else {
i = i + 1
}
}
}
if (second_peak != 0) { # check continuous consumption
no_pulse <- 0 # sometimes not pulse during regeneration
for (j in (first_peak : second_peak)) {
if (pulses[j] == 0) {
# cat('pulse en ',j,'equals',pulses[j],'\n')
no_pulse = no_pulse + 1
}
}
if (no_pulse < 3) {
return(TRUE)
}else{
p = second_peak
# cat('p set to: ',p)
}
} # else -> next pulse
p = p + 1
}
return(FALSE)
}