-
Notifications
You must be signed in to change notification settings - Fork 0
/
osmolib.cpp
99 lines (84 loc) · 2.49 KB
/
osmolib.cpp
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
#include "Arduino.h"
#include "osmolib.h"
OsmoLib::OsmoLib(int level_pin, int pump_trigger_pin, int trigger_state){
levelPin = level_pin;
pumpTriggerPin = pump_trigger_pin;
levelTriggerState = trigger_state;
pumpBurnLevelPin = 255;
securityLevelPin = 255;
levelWaveBounce = 0;
pumpStartTime = 0;
pumpRepeat = 0;
pumpDisabled = false;
pinMode(levelPin, INPUT);
pinMode(pumpTriggerPin, OUTPUT);
digitalWrite(pumpTriggerPin, LOW);
}
void OsmoLib::setPumpBurnLevelPin(int pin, int trigger_state){
pumpBurnLevelPin = pin;
burnTriggerState = trigger_state;
pinMode(pumpBurnLevelPin, INPUT);
}
void OsmoLib::setSecurityLevelPin(int pin, int trigger_state){
securityLevelPin = pin;
secuTriggerState = trigger_state;
pinMode(securityLevelPin, INPUT);
}
void OsmoLib::run(){
level = digitalRead(levelPin);
if(level == HIGH)
Serial.println("Osmo HIGH");
else
Serial.println("Osmo LOW");
if(level == levelTriggerState){
if(levelWaveBounce == 0){
levelWaveBounce = millis();
}else if(((millis() - levelWaveBounce) > BOUNCE_TIME)&&(pumpStartTime == 0)&&(!pumpDisabled)){
digitalWrite(pumpTriggerPin, HIGH);
pumpStartTime = millis();
}
}
if((pumpStartTime != 0)&&(millis() - pumpStartTime > PUMP_TIME)&&(!pumpDisabled)){
level = digitalRead(levelPin);
if(level != levelTriggerState){
digitalWrite(pumpTriggerPin, LOW);
pumpStartTime = 0;
levelWaveBounce = 0;
pumpRepeat = 0;
}else if(pumpRepeat > 3){
digitalWrite(pumpTriggerPin, LOW);
pumpDisabled = true;
}else{
pumpRepeat++;
pumpStartTime = millis();
}
}
if((pumpDisabled)&&(millis() - pumpStartTime > ONE_HOUR)){
pumpStartTime = 0;
levelWaveBounce = 0;
pumpRepeat = 0;
}else if((pumpDisabled)&&(millis() - pumpStartTime < 0)){
pumpStartTime = 0;
levelWaveBounce = 0;
pumpRepeat = 0;
pumpDisabled = false;
}
if(pumpBurnLevelPin != 255){
int burn = digitalRead(pumpBurnLevelPin);
if(burn == burnTriggerState){
pumpDisabled = true;
digitalWrite(pumpTriggerPin, LOW);
}else{
pumpDisabled = false;
}
}
if(securityLevelPin != 255){
int secu = digitalRead(securityLevelPin);
if(secu == secuTriggerState){
pumpDisabled = true;
digitalWrite(pumpTriggerPin, LOW);
}else{
pumpDisabled = false;
}
}
}